From 77c0c815e0826d647aa96e07e0f7fef3cd7a1483 Mon Sep 17 00:00:00 2001 From: kalipso Date: Tue, 9 May 2023 17:41:27 +0200 Subject: [PATCH] extend tasks --- forms.html | 2 +- main.go | 97 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/forms.html b/forms.html index 4f799be..dc60825 100644 --- a/forms.html +++ b/forms.html @@ -131,7 +131,7 @@ input {
{{range .Tasks}}
- +

{{.Description}}
diff --git a/main.go b/main.go index c9811bd..ea2e6a0 100644 --- a/main.go +++ b/main.go @@ -11,46 +11,106 @@ import ( _ "github.com/mattn/go-sqlite3" ) -func CreateTasklist(title string, db *sql.DB) Tasklist { - tasks := []Task{ +func GetDefaultTasks() []Task { + return []Task{ { 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: "CleanUp", Description: `(includes cleaning up, tidying up, rearranging chairs, closing or arranging somebody to lock the front and back doors)`, + IsOptional: false, }, { - Name: "Weekly Toilet & Kitchen", + Name: "WeeklyToiletAndKitchen", Description: `(whiping the floor in kitchen and bathroom, clean toilet, clean sink, check soap and menstruation supplies)`, + IsOptional: false, }, } +} - stmt, err := db.Prepare("select promotion, opening, entering, cleanup, weeklytoilet from tasklists where name = ?") +func InitDB() *sql.DB { + db, err := sql.Open("sqlite3", DB_NAME) + if err != nil { + log.Fatal(err) + } + + sqlStmt := "create table if not exists tasklists (name test not null primary key" + // 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) + return nil + } + + return db +} + +func GetTaskNameString(FillString string) string { + result := "" + IsFirst := true + for _, task := range GetDefaultTasks() { + if !IsFirst { + result += FillString + } + result += task.Name + IsFirst = false + } + + return result +} + +func CreateTasklist(title string, db *sql.DB) Tasklist { + tasks := GetDefaultTasks() + + stmt, err := db.Prepare("select " + GetTaskNameString(", ") + " from tasklists where name = ?") if err != nil { fmt.Println(err) } defer stmt.Close() - var promotion, opening, entering, cleanup, weeklytoilet string - err = stmt.QueryRow(title).Scan(&promotion, &opening, &entering, &cleanup, &weeklytoilet) + var promotion, opening, entering, preparekuefa, cookkuefa, cleanup, weeklytoilet string + err = stmt.QueryRow(title).Scan(&promotion, &opening, &entering, &preparekuefa, &cookkuefa, &cleanup, &weeklytoilet) if err == nil { tasks[0].Value = promotion tasks[1].Value = opening tasks[2].Value = entering - tasks[3].Value = cleanup - tasks[4].Value = weeklytoilet + tasks[3].Value = preparekuefa + tasks[4].Value = cookkuefa + tasks[5].Value = cleanup + tasks[6].Value = weeklytoilet } return Tasklist{ @@ -69,9 +129,12 @@ func InsertToDB(tasklist Tasklist, db *sql.DB) { var name string err = stmt.QueryRow(tasklist.Title).Scan(&name) if err != nil { - db.Exec("insert into tasklists(name, promotion, opening, entering, cleanup, weeklytoilet) values(?, ?, ?, ?, ?, ?)", tasklist.Title, tasklist.Tasks[0].Value, tasklist.Tasks[1].Value, tasklist.Tasks[2].Value, tasklist.Tasks[3].Value, tasklist.Tasks[4].Value) + 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) + if err != nil { + fmt.Println("Error during insert: ", err) + } } else { - _, err := db.Exec("update tasklists set promotion = ?,opening = ?,entering = ?,cleanup = ?,weeklytoilet = ? where name = ?", tasklist.Tasks[0].Value, tasklist.Tasks[1].Value, tasklist.Tasks[2].Value, tasklist.Tasks[3].Value, tasklist.Tasks[4].Value, tasklist.Title) + _, 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.Title) if err != nil { fmt.Println("Error during update: ", err) } @@ -82,6 +145,7 @@ type Task struct { Name string Description string Value string + IsOptional bool } type Tasklist struct { @@ -128,18 +192,11 @@ const TASK_DAY = time.Tuesday const AMOUNT_DAYS = 4 func main() { - db, err := sql.Open("sqlite3", DB_NAME) - if err != nil { - log.Fatal(err) - } + db := InitDB() defer db.Close() - sqlStmt := ` - create table if not exists tasklists (name test not null primary key, promotion text, opening text, entering text, cleanup text, weeklytoilet text); - ` - _, err = db.Exec(sqlStmt) - if err != nil { - log.Printf("%q: %s\n", err, sqlStmt) + if db == nil { + fmt.Println("Creating db failed. exiting") return }