todo app
This commit is contained in:
commit
c0e345d169
|
@ -0,0 +1,47 @@
|
|||
package services
|
||||
|
||||
import "fmt"
|
||||
|
||||
type taskService struct {
|
||||
tasks map[int]string
|
||||
}
|
||||
|
||||
func NewTaskService() *taskService {
|
||||
return &taskService{
|
||||
tasks: make(map[int]string, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *taskService) Insert(key int, v string) error {
|
||||
if _, found := r.tasks[key]; found {
|
||||
return fmt.Errorf("already exist %d", key)
|
||||
}
|
||||
r.tasks[key] = v
|
||||
return nil
|
||||
}
|
||||
func (r *taskService) List() map[int]string {
|
||||
return r.tasks
|
||||
}
|
||||
|
||||
func (r *taskService) Show(key int) string {
|
||||
if task, found := r.tasks[key]; found {
|
||||
return task
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *taskService) Delete(key int) error {
|
||||
if _, found := r.tasks[key]; !found {
|
||||
return fmt.Errorf("task with key %d not found", key)
|
||||
}
|
||||
delete(r.tasks, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *taskService) Update(key int, newTask string) error {
|
||||
if _, found := r.tasks[key]; !found {
|
||||
return fmt.Errorf("task with key %d not found", key)
|
||||
}
|
||||
r.tasks[key] = newTask
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"znga/Desktop/go/todo/internal/services"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ts := services.NewTaskService()
|
||||
|
||||
options := map[string]string{
|
||||
"i": "insert",
|
||||
"u": "update",
|
||||
"l": "list",
|
||||
"s": "show",
|
||||
"d": "delete",
|
||||
}
|
||||
for {
|
||||
|
||||
fmt.Println("Options: ")
|
||||
for k, v := range options {
|
||||
fmt.Printf("%s to %s\t", k, v)
|
||||
}
|
||||
fmt.Println("")
|
||||
fmt.Print("Select an option: ")
|
||||
var scan string
|
||||
_, err := fmt.Scanln(&scan)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//value, bool
|
||||
if _, found := options[strings.ToLower(scan)]; !found {
|
||||
fmt.Printf("option %s is not allowed\n", scan)
|
||||
continue
|
||||
}
|
||||
switch scan {
|
||||
case "i":
|
||||
fmt.Print("type key, task(only integers allowed): ")
|
||||
_, err := fmt.Scanln(&scan)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
taskID, err := strconv.Atoi(scan)
|
||||
if err != nil {
|
||||
fmt.Println("invalid ID")
|
||||
continue
|
||||
}
|
||||
fmt.Print("write your task: ")
|
||||
_, err = fmt.Scanln(&scan)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := ts.Insert(taskID, scan); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
break
|
||||
case "l":
|
||||
fmt.Println(strings.Repeat("*", 20))
|
||||
for k, v := range ts.List() {
|
||||
fmt.Printf("ID: %d - %s\n", k, v)
|
||||
}
|
||||
fmt.Println(strings.Repeat("*", 20))
|
||||
break
|
||||
case "s":
|
||||
fmt.Print("type key you want to retrieve (only integers allowed): ")
|
||||
var key int
|
||||
_, err := fmt.Scanln(&key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Scanln()
|
||||
|
||||
fmt.Println(strings.Repeat("*", 20))
|
||||
task := ts.Show(key)
|
||||
if task != "" {
|
||||
fmt.Println("Task:", task)
|
||||
} else {
|
||||
fmt.Println("Task not found.")
|
||||
}
|
||||
fmt.Println(strings.Repeat("*", 20))
|
||||
break
|
||||
case "d":
|
||||
fmt.Print("type key you want to delete (only integers allowed): ")
|
||||
var key int
|
||||
_, err := fmt.Scanln(&key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Scanln()
|
||||
|
||||
if err := ts.Delete(key); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("Task deleted successfully.")
|
||||
}
|
||||
break
|
||||
case "u":
|
||||
fmt.Print("type key you want to update (only integers allowed): ")
|
||||
var key int
|
||||
_, err := fmt.Scanln(&key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Scanln()
|
||||
|
||||
fmt.Print("Write the new task description: ")
|
||||
var newTask string
|
||||
_, err = fmt.Scanln(&newTask)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := ts.Update(key, newTask); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println("Task updated successfully")
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue