working sqlite
This commit is contained in:
parent
e24728f878
commit
2c52966b5c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
squeal.db
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module git.noctra.dev/catman/gotodo
|
||||
|
||||
go 1.22.0
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
require github.com/mattn/go-sqlite3 v1.14.22
|
||||
|
87
main.go
87
main.go
@ -1,28 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
|
||||
type Todo struct {
|
||||
Title string
|
||||
Desc string
|
||||
Done bool
|
||||
}
|
||||
|
||||
var todos = map[string][]Todo {
|
||||
"Todos": {
|
||||
{Title: "Wash dishes", Desc: "", Done: true},
|
||||
{Title: "Clean living room", Desc: "overdue", Done: false},
|
||||
{Title: "Do some shopping", Desc: "bacon pancakes, making bacon pancakes", Done: false},
|
||||
},
|
||||
}
|
||||
|
||||
// Takes
|
||||
func get_requestee(requestee string) string {
|
||||
var addr string
|
||||
var addr_list []string
|
||||
@ -40,13 +39,71 @@ func get_requestee(requestee string) string {
|
||||
return addr
|
||||
}
|
||||
|
||||
func init_db() {
|
||||
os.Remove("./squeal.db")
|
||||
|
||||
database, err := sql.Open("sqlite3", "./squeal.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
db = database
|
||||
|
||||
table := `create table todos (title text not null, description text, done integer)`
|
||||
_, err = db.Exec(table)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func todo_add(title string, description string, done bool) {
|
||||
done_int := 0
|
||||
if done {
|
||||
done_int = 1
|
||||
}
|
||||
stmt := fmt.Sprintf("insert into todos (title, description, done) values (\"%s\", \"%s\", \"%d\")", title, description, done_int)
|
||||
|
||||
_, err := db.Exec(stmt)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
|
||||
func todo_get_all() []Todo {
|
||||
stmt := `select * from todos`
|
||||
row, err := db.Query(stmt)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
res := []Todo{}
|
||||
for row.Next() {
|
||||
var todo Todo
|
||||
var done_tmp int
|
||||
|
||||
row.Scan(&todo.Title, &todo.Desc, &done_tmp)
|
||||
|
||||
todo.Done = false
|
||||
if done_tmp != 0 {
|
||||
todo.Done = true
|
||||
}
|
||||
|
||||
res = append(res, todo)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Print("Listening")
|
||||
init_db()
|
||||
|
||||
home := func (w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Print(get_requestee(r.RemoteAddr) + " " + r.Method + " home")
|
||||
|
||||
todos := map[string][]Todo{}
|
||||
for _, todo := range todo_get_all() {
|
||||
todos["Todos"] = append(todos["Todos"], todo)
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFiles("tmpl/home.html"))
|
||||
tmpl.Execute(w, todos)
|
||||
}
|
||||
@ -54,19 +111,19 @@ func main() {
|
||||
|
||||
add_todo := func (w http.ResponseWriter, r *http.Request) {
|
||||
log.Print(get_requestee(r.RemoteAddr) + " " + r.Method + " add-todo")
|
||||
|
||||
r.ParseForm()
|
||||
title := r.Form.Get("title")
|
||||
desc := r.Form.Get("desc")
|
||||
done := strings.Contains(r.Form.Get("selected"), "done")
|
||||
todos["Todos"] = append(todos["Todos"], Todo{
|
||||
Title: title,
|
||||
Desc: desc,
|
||||
Done: done,
|
||||
})
|
||||
|
||||
todo_add(title, desc, done)
|
||||
|
||||
html := fmt.Sprintf("<tr><td>%s</td><td>%s</td><td>%t</td></tr>", title, desc, done)
|
||||
template.Must(template.New("t").Parse(html)).Execute(w, nil)
|
||||
}
|
||||
http.HandleFunc("/add-todo", add_todo)
|
||||
|
||||
log.Print("Listening")
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", nil))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user