Files
tasklist/main.go
2023-10-05 19:10:37 +02:00

245 lines
6.3 KiB
Go

package main
import (
"database/sql"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"time"
_ "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: "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: "WeeklyToiletAndKitchen",
Description: `(whiping the floor in kitchen and bathroom, clean toilet, clean sink, check soap and menstruation supplies)`,
IsOptional: false,
},
}
}
func InitDB(db_name string) *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 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
}
return Tasklist{
Title: title,
Tasks: tasks,
Updated: false,
}
}
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)
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)
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)
if err != nil {
fmt.Println("Error during update: ", err)
}
}
}
type Task struct {
Name string
Description string
Value string
IsOptional bool
}
type Tasklist struct {
Title string
Tasks []Task
Updated bool
}
type Tasklists struct {
Tasklists []Tasklist
}
type QueryResult struct {
Tasklists []Tasklist
}
func (n *Tasklist) Print() {
fmt.Println(n.Title)
for _, task := range n.Tasks {
fmt.Println("\t Name: ", task.Name)
fmt.Println("\t Descr: ", task.Description)
fmt.Println("\t Value: ", task.Value)
}
}
func GetNextNDaysOfName(n int, name time.Weekday, current time.Time) []time.Time {
result := make([]time.Time, n)
for index := 0; index < n; {
if current.Weekday() == name {
result[index] = current
index++
}
current = current.AddDate(0, 0, 1)
}
return result
}
const DB_NAME = "./tasklist.db"
const FORMS_NAME = "./forms.html"
const TASK_DAY = time.Tuesday
const AMOUNT_DAYS = 4
func main() {
dbName := flag.String("d", DB_NAME, "path to db file")
formsName := flag.String("f", FORMS_NAME, "path to form file")
flag.Parse()
db := InitDB(*dbName)
defer db.Close()
if db == nil {
fmt.Println("Creating db failed. exiting")
return
}
tmpl := template.Must(template.ParseFiles(*formsName))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
days := GetNextNDaysOfName(AMOUNT_DAYS, TASK_DAY, time.Now())
tasklist_arr := QueryResult{make([]Tasklist, AMOUNT_DAYS)}
for idx, day := range days {
tasklist_arr.Tasklists[idx] = CreateTasklist(day.Format("Jan 2, 2006"), db)
}
if r.Method != http.MethodPost {
tmpl.Execute(w, tasklist_arr)
return
}
for idx, task := range tasklist_arr.Tasklists {
if task.Title == r.FormValue("Name") {
for idx2 := range task.Tasks {
task.Tasks[idx2].Value = r.FormValue(task.Tasks[idx2].Name)
}
tasklist_arr.Tasklists[idx].Updated = true
InsertToDB(tasklist_arr.Tasklists[idx], db)
}
}
tmpl.Execute(w, tasklist_arr)
})
http.ListenAndServe(":8080", nil)
}