This commit is contained in:
Emanuel Turis
2023-10-20 07:46:32 +03:00
parent ccb0c914e5
commit d1f37d7b0e
12 changed files with 607 additions and 11 deletions

20
task.go
View File

@@ -16,7 +16,7 @@ type Tasks struct {
func fetchTasks() ([]Item, error) {
var items []Item
rows, err := DB.Query("select id, title, completed from tasks order by postition;")
rows, err := DB.Query("select id, title, completed from tasks order by position;")
if err != nil {
return []Item{}, err
}
@@ -59,6 +59,15 @@ func fetchCount() (int, error) {
return count, nil
}
func fetchCompletedCount() (int, error) {
var count int
err := DB.QueryRow("select count(*) from tasks where completed = 1;").Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func insertTask(title string) (Item, error) {
count, err := fetchCount()
if err != nil {
@@ -126,3 +135,12 @@ func orderTasks(ctx context.Context, values []int) error {
}
return nil
}
func toggleTask(ID int) (Item, error) {
var item Item
err := DB.QueryRow("update tasks set completed = case when completed = 1 then 0 else 1 end where id = (?) returning id, title, completed", ID).Scan(&item.ID, &item.Title, &item.Completed)
if err != nil {
return Item{}, err
}
return item, nil
}