mirror of
https://github.com/ducky-labs/htmx-go-todo.git
synced 2026-02-04 06:31:00 +00:00
Changes
This commit is contained in:
20
task.go
20
task.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user