diff --git a/forms.html b/forms.html index dc60825..7854dd4 100644 --- a/forms.html +++ b/forms.html @@ -124,7 +124,7 @@ input { - {{.Title}} + {{.Date}} {{ if .Updated }} Updated {{end}} @@ -136,7 +136,7 @@ input { {{.Description}} {{end}} - + diff --git a/go.mod b/go.mod index b355076..6aea505 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,12 @@ module tasklist go 1.20 -require github.com/mattn/go-sqlite3 v1.14.16 // indirect +require ( + github.com/mattn/go-sqlite3 v1.14.16 + github.com/tidwall/gjson v1.17.1 +) + +require ( + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect +) diff --git a/go.sum b/go.sum index 7878efc..c757d8b 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,8 @@ github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= +github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= diff --git a/main.go b/main.go index 4467690..63ea790 100644 --- a/main.go +++ b/main.go @@ -8,52 +8,59 @@ import ( "log" "net/http" "time" + "encoding/json" + //"github.com/tidwall/gjson" _ "github.com/mattn/go-sqlite3" ) func GetDefaultTasks() []Task { return []Task{ - { - Name: "Title", - Description: ``, - IsOptional: true, - }, - { - Name: "Promotion", - Description: ` - (make a post on the website to promote openings times and for each film event, posting description and announcement on telegram channel, forwarding it to telegram groups, making a fb event, making a post about fb event on the fb page, sharing the event to fb groups) - `, - IsOptional: false, - }, + //{ + // Name: "Title", + // Description: ``, + // IsOptional: true, + //}, + //{ + // Name: "Promotion", + // Description: ` + // (make a post on the website to promote openings times and for each film event, posting description and announcement on telegram channel, forwarding it to telegram groups, making a fb event, making a post about fb event on the fb page, sharing the event to fb groups) + // `, + // IsOptional: false, + //}, { Name: "Opening", Description: `(checking toilet if there is toilet paper and fresh towel, refill fridge, sorting deposit bottles, quickly tidy up the place)`, IsOptional: false, }, - { - Name: "Entering", - Description: `(checking if there are new books to be entered into the library catalogue, categorizing them, stamping them, putting them into the book shelf)`, - IsOptional: true, - }, - { - Name: "PrepareKuefa", - Description: `(includes preparing the cutting boards and knives and all devices needed to prepare food. Including potential visitors in this action, preparing chairs, tables and benches outside of malo for people to eat and chill outside)`, - IsOptional: true, - }, - { - Name: "CookingKuefa", - Description: `(choosing a dish/recipe, doing shopping or arranging somebody to do shopping, cooking, serving)`, - IsOptional: true, - }, + //{ + // Name: "Entering", + // Description: `(checking if there are new books to be entered into the library catalogue, categorizing them, stamping them, putting them into the book shelf)`, + // IsOptional: true, + //}, + //{ + // Name: "PrepareKuefa", + // Description: `(includes preparing the cutting boards and knives and all devices needed to prepare food. Including potential visitors in this action, preparing chairs, tables and benches outside of malo for people to eat and chill outside)`, + // IsOptional: true, + //}, + //{ + // Name: "CookingKuefa", + // Description: `(choosing a dish/recipe, doing shopping or arranging somebody to do shopping, cooking, serving)`, + // IsOptional: true, + //}, { Name: "CleanUp", Description: `(includes cleaning up, tidying up, rearranging chairs, closing or arranging somebody to lock the front and back doors)`, IsOptional: false, }, { - Name: "WeeklyToiletAndKitchen", - Description: `(whiping the floor in kitchen and bathroom, clean toilet, clean sink, check soap and menstruation supplies)`, + Name: "WeeklyToilet", + Description: `(whiping the floor in the bathroom, clean toilet, clean sink, check soap and menstruation supplies)`, + IsOptional: false, + }, + { + Name: "WeeklyKitchen", + Description: `(whiping the floor in kitchen, clean sink, empty trash, check surfaces)`, IsOptional: false, }, } @@ -65,13 +72,13 @@ func InitDB(db_name string) *sql.DB { log.Fatal(err) } - sqlStmt := "create table if not exists tasklists (name test not null primary key" + 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" - } + //for _, task := range GetDefaultTasks() { + // sqlStmt += ", " + task.Name + " text" + //} - sqlStmt += ");" + //sqlStmt += ");" fmt.Println(sqlStmt) @@ -85,7 +92,7 @@ func InitDB(db_name string) *sql.DB { } func GetTaskNameString(FillString string) string { - result := "" + result := "Data" IsFirst := true for _, task := range GetDefaultTasks() { if !IsFirst { @@ -98,50 +105,62 @@ func GetTaskNameString(FillString string) string { return result } -func CreateTasklist(title string, db *sql.DB) Tasklist { - tasks := GetDefaultTasks() +func CreateTasklist(date string, db *sql.DB) Tasklist { + stmt, err := db.Prepare("select " + "Data" + " from tasklists where name = ?") - stmt, err := db.Prepare("select " + GetTaskNameString(", ") + " from tasklists where name = ?") if err != nil { fmt.Println(err) } - defer stmt.Close() - var eventtitle, promotion, opening, entering, preparekuefa, cookkuefa, cleanup, weeklytoilet string - err = stmt.QueryRow(title).Scan(&eventtitle, &promotion, &opening, &entering, &preparekuefa, &cookkuefa, &cleanup, &weeklytoilet) - if err == nil { - tasks[0].Value = eventtitle - tasks[1].Value = promotion - tasks[2].Value = opening - tasks[3].Value = entering - tasks[4].Value = preparekuefa - tasks[5].Value = cookkuefa - tasks[6].Value = cleanup - tasks[7].Value = weeklytoilet + defer stmt.Close() + + var data string + err = stmt.QueryRow(date).Scan(&data) + + tasks := GetDefaultTasks() + if err != nil { + if err != sql.ErrNoRows { + fmt.Printf("Error in CreateTasklist during QueryRow, Date %s\n", date) + fmt.Println(err) + } + } else { + tasks = TasksFromJson(data) + if tasks == nil { + fmt.Println("Error in CreateTasklist") + fmt.Println(err) + } } return Tasklist{ - Title: title, + Date: date, Tasks: tasks, Updated: false, } } -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) } + defer stmt.Close() + var name string - err = stmt.QueryRow(tasklist.Title).Scan(&name) + err = stmt.QueryRow(tasklist.Date).Scan(&name) + if err != nil { - db.Exec("insert into tasklists(name, "+GetTaskNameString(", ")+") values(?, ?, ?, ?, ?, ?, ?, ?, ?)", tasklist.Title, tasklist.Tasks[0].Value, tasklist.Tasks[1].Value, tasklist.Tasks[2].Value, tasklist.Tasks[3].Value, tasklist.Tasks[4].Value, tasklist.Tasks[5].Value, tasklist.Tasks[6].Value, tasklist.Tasks[7].Value) + _, err := db.Exec("insert into tasklists(name, "+ "Data" +") values(?, ?)", tasklist.Date, TasksToJson(tasklist.Tasks)) + if err != nil { fmt.Println("Error during insert: ", err) } + } else { - _, err := db.Exec("update tasklists set "+GetTaskNameString(" = ?,")+" = ? where name = ?", tasklist.Tasks[0].Value, tasklist.Tasks[1].Value, tasklist.Tasks[2].Value, tasklist.Tasks[3].Value, tasklist.Tasks[4].Value, tasklist.Tasks[5].Value, tasklist.Tasks[6].Value, tasklist.Tasks[7].Value, tasklist.Title) + _, err := db.Exec("update tasklists set " + "Data" +" = ? where name = ?", TasksToJson(tasklist.Tasks), tasklist.Date) + fmt.Println(TasksToJson(tasklist.Tasks)) + if err != nil { fmt.Println("Error during update: ", err) } @@ -149,18 +168,43 @@ func InsertToDB(tasklist Tasklist, db *sql.DB) { } type Task struct { - Name string - Description string - Value string - IsOptional bool + Name string `json:"Name"` + Description string `json:"Description"` + Value string `json:"Value"` + IsOptional bool `json:"IsOptional"` } type Tasklist struct { - Title string + Date string Tasks []Task Updated bool } +func TasksFromJson(tasks string) []Task { + var result []Task + err := json.Unmarshal([]byte(tasks), &result) + + if err != nil { + fmt.Println("Error during TasksFromJson") + fmt.Println(err) + return nil + } + + return result +} + +func TasksToJson(tasks []Task) string { + result, err := json.Marshal(tasks) + + if err != nil { + fmt.Println("Error during TasksToJson") + fmt.Println(err) + return "" + } + + return string(result) +} + type Tasklists struct { Tasklists []Tasklist } @@ -170,7 +214,7 @@ type QueryResult struct { } func (n *Tasklist) Print() { - fmt.Println(n.Title) + fmt.Println(n.Date) for _, task := range n.Tasks { fmt.Println("\t Name: ", task.Name) @@ -227,7 +271,7 @@ func main() { } for idx, task := range tasklist_arr.Tasklists { - if task.Title == r.FormValue("Name") { + if task.Date == r.FormValue("Name") { for idx2 := range task.Tasks { task.Tasks[idx2].Value = r.FormValue(task.Tasks[idx2].Name) } @@ -236,9 +280,14 @@ func main() { } } - tmpl.Execute(w, tasklist_arr) + err := tmpl.Execute(w, tasklist_arr) + if err != nil { + fmt.Println("Error during tmpl.Execute") + fmt.Println(err) + } }) + fmt.Println("Start listening on port 8080") http.ListenAndServe(":8080", nil) }