-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (120 loc) · 2.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"database/sql"
"embed"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/mattn/go-sqlite3"
)
var (
//go:embed templates
templateFS embed.FS
//go:embed static
staticFS embed.FS
)
type Task struct {
ID int
Title string
}
type TaskRepo struct {
db *sql.DB
//tasks []Task
}
func initDB() (*sql.DB, error) {
db, err := sql.Open("sqlite3", "tasks.db")
if err != nil {
return nil, err
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS tasks (
title TEXT NOT NULL
);
`)
if err != nil {
db.Close()
return nil, err
}
return db, nil
}
func initMux(db *sql.DB) *chi.Mux {
repo := TaskRepo{db: db}
tpl := template.Must(template.ParseFS(templateFS, "templates/*.html"))
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Handle("/static/*", http.FileServer(http.FS(staticFS)))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
tasks, err := repo.GetTasks()
if err != nil {
log.Fatal(err)
}
tpl.ExecuteTemplate(w, "index.html",tasks)
})
r.Post("/add", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
return
}
repo.AddTask(r.FormValue("title"))
tasks, err := repo.GetTasks()
if err != nil {
log.Fatal(err)
}
tpl.ExecuteTemplate(w, "task-list.html", tasks)
})
r.Delete("/{id}", func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
log.Fatal(err)
return
}
repo.DeleteTask(id)
tasks, err := repo.GetTasks()
if err != nil {
log.Fatal(err)
}
tpl.ExecuteTemplate(w, "task-list.html", tasks)
})
return r
}
func (tr *TaskRepo) AddTask(title string) error {
_, err := tr.db.Exec("INSERT INTO tasks(title) VALUES (?)", title)
if err != nil {
return err
}
return nil
}
func (tr *TaskRepo) GetTasks() ([]Task, error) {
rows, err := tr.db.Query("SELECT rowid,title FROM tasks")
if err != nil {
return nil, err
}
var tasks []Task
for rows.Next() {
var task Task
err = rows.Scan(&task.ID, &task.Title)
if err != nil {
return nil, err
}
tasks = append(tasks, task)
}
return tasks, nil
}
func (tr *TaskRepo) DeleteTask(id int) error {
_, err := tr.db.Exec("DELETE FROM tasks WHERE rowid = ?", id)
return err
}
func main() {
db, err := initDB()
if err != nil {
log.Fatal(err)
}
defer db.Close()
r := initMux(db)
fmt.Println("server started on :3000")
http.ListenAndServe(":3000", r)
}