todo app
This commit is contained in:
125
main.go
Normal file
125
main.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user