2022-09-09 17:31:08 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-10 18:54:17 +02:00
|
|
|
"log"
|
2022-09-09 17:31:08 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Todo struct {
|
|
|
|
ID uint64
|
|
|
|
Desc string
|
|
|
|
DueDate time.Time
|
|
|
|
CreatedAt time.Time
|
2022-09-10 18:54:17 +02:00
|
|
|
IsDone bool
|
2022-09-09 17:31:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// errPanic exits program with msg and error details if there is an error.
|
2022-09-09 17:31:08 +02:00
|
|
|
func errPanic(err error, msg string) {
|
|
|
|
if err != nil {
|
2022-09-10 18:54:17 +02:00
|
|
|
log.Fatalf(msg, err)
|
2022-09-09 17:31:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// parseDateString returns parsed time from string or time.Time{}.
|
2022-09-09 17:31:08 +02:00
|
|
|
func parseDateString(date string) time.Time {
|
|
|
|
var (
|
|
|
|
t time.Time
|
|
|
|
err error
|
|
|
|
layouts = []string{
|
|
|
|
"02/01/2006",
|
|
|
|
"02/01/2006T15:04",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
for _, layout := range layouts {
|
|
|
|
t, err = time.Parse(layout, date)
|
|
|
|
if err == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 18:54:17 +02:00
|
|
|
|
2022-09-09 17:31:08 +02:00
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// openDB returns gorm database.
|
2022-09-09 17:31:08 +02:00
|
|
|
func openDB() *gorm.DB {
|
|
|
|
db, err := gorm.Open(sqlite.Open("todo.db"), &gorm.Config{})
|
|
|
|
errPanic(err, "failed to connect database")
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// createTodo creates todo entry in the database.
|
2022-09-10 20:34:52 +02:00
|
|
|
func createTodo(desc string, date string, comp bool) {
|
2022-09-09 17:31:08 +02:00
|
|
|
db := openDB()
|
|
|
|
err := db.AutoMigrate(&Todo{})
|
|
|
|
errPanic(err, "failed to migrate database")
|
|
|
|
d := parseDateString(date)
|
|
|
|
|
2022-09-10 20:34:52 +02:00
|
|
|
t := Todo{Desc: desc, DueDate: d, CreatedAt: time.Now(), IsDone: comp}
|
2022-09-09 17:31:08 +02:00
|
|
|
db.Create(&t)
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// updateTodo updates an entry of id todo in the database.
|
2022-09-10 18:54:17 +02:00
|
|
|
func updateTodo(id uint64, desc string, date string, comp bool) {
|
2022-09-09 17:31:08 +02:00
|
|
|
db := openDB()
|
|
|
|
var t Todo
|
|
|
|
res := db.First(&t, id)
|
|
|
|
errPanic(res.Error, "failed to find id")
|
2022-09-10 18:54:17 +02:00
|
|
|
if desc != "" {
|
|
|
|
t.Desc = desc
|
|
|
|
}
|
|
|
|
if date != "" {
|
|
|
|
d := parseDateString(date)
|
|
|
|
t.DueDate = d
|
|
|
|
}
|
|
|
|
if comp {
|
|
|
|
t.IsDone = !t.IsDone
|
|
|
|
}
|
|
|
|
db.Model(&t).Updates(t)
|
2022-09-09 17:31:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// deleteTodo deletes an entry of id todo in the database.
|
2022-09-10 16:48:23 +02:00
|
|
|
func deleteTodo(id uint64) {
|
2022-09-09 17:31:08 +02:00
|
|
|
db := openDB()
|
|
|
|
db.Delete(&Todo{}, id)
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:42:36 +02:00
|
|
|
// getAllTodos return all todos from the database.
|
2022-09-09 17:31:08 +02:00
|
|
|
func getAllTodos() []Todo {
|
|
|
|
db := openDB()
|
|
|
|
var todos []Todo
|
|
|
|
res := db.Find(&todos)
|
|
|
|
errPanic(res.Error, "failed to get all todos")
|
|
|
|
|
|
|
|
return todos
|
|
|
|
}
|
2022-09-10 18:54:17 +02:00
|
|
|
|
2022-09-10 21:09:13 +02:00
|
|
|
// getDoneTodos returns all completed todos.
|
2022-09-10 18:54:17 +02:00
|
|
|
func getDoneTodos() []Todo {
|
|
|
|
db := openDB()
|
|
|
|
var todos []Todo
|
|
|
|
res := db.Where(&Todo{IsDone: true}).Find(&todos)
|
|
|
|
errPanic(res.Error, "failed to get all todos")
|
|
|
|
|
|
|
|
return todos
|
|
|
|
}
|
|
|
|
|
2022-09-10 21:09:13 +02:00
|
|
|
// getDoneTodos returns all uncompleted todos.
|
2022-09-10 18:54:17 +02:00
|
|
|
func getUnDoneTodos() []Todo {
|
|
|
|
db := openDB()
|
|
|
|
var todos []Todo
|
2022-09-10 21:09:13 +02:00
|
|
|
res := db.Where("is_done = ?", false).Find(&todos)
|
2022-09-10 18:54:17 +02:00
|
|
|
errPanic(res.Error, "failed to get all todos")
|
|
|
|
|
|
|
|
return todos
|
|
|
|
}
|