add api call

This commit is contained in:
2024-06-17 21:33:03 +02:00
parent b9f8fe84d9
commit c6ae5019dd

26
main.go
View File

@@ -10,7 +10,6 @@ import (
"time"
"encoding/json"
//"github.com/tidwall/gjson"
_ "github.com/mattn/go-sqlite3"
)
@@ -73,15 +72,6 @@ func InitDB(db_name string) *sql.DB {
}
sqlStmt := "create table if not exists tasklists (name test not null primary key, Data text);"
// promotion text, opening text, entering text, cleanup text, weeklytoilet text);
//for _, task := range GetDefaultTasks() {
// sqlStmt += ", " + task.Name + " text"
//}
//sqlStmt += ");"
fmt.Println(sqlStmt)
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
@@ -138,8 +128,8 @@ func CreateTasklist(date string, db *sql.DB) Tasklist {
}
}
func InsertToDB(tasklist Tasklist, db *sql.DB) { stmt, err :=
db.Prepare("select name from tasklists where name = ?")
func InsertToDB(tasklist Tasklist, db *sql.DB) {
stmt, err := db.Prepare("select name from tasklists where name = ?")
if err != nil {
log.Fatal(err)
@@ -257,6 +247,18 @@ func main() {
}
tmpl := template.Must(template.ParseFiles(*formsName))
http.HandleFunc("/api/next", func(w http.ResponseWriter, r *http.Request) {
days := GetNextNDaysOfName(1, TASK_DAY, time.Now())
tasklist_arr := QueryResult{make([]Tasklist, 1)}
for idx, day := range days {
tasklist_arr.Tasklists[idx] = CreateTasklist(day.Format("Jan 2, 2006"), db)
}
fmt.Fprintf(w, TasksToJson(tasklist_arr.Tasklists[0].Tasks))
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
days := GetNextNDaysOfName(AMOUNT_DAYS, TASK_DAY, time.Now())
tasklist_arr := QueryResult{make([]Tasklist, AMOUNT_DAYS)}