299 lines
6.6 KiB
Go
299 lines
6.6 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func GetDefaultTasks() []Task {
|
|
return []Task{
|
|
{
|
|
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: "Closing",
|
|
Description: `(includes cleaning up, tidying up, rearranging chairs, closing or arranging somebody to lock the front and back doors)`,
|
|
IsOptional: false,
|
|
},
|
|
{
|
|
Name: "WeeklyToilet",
|
|
Description: `(whiping the floor in the bathroom, empty trash, 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,
|
|
},
|
|
{
|
|
Name: "Trash",
|
|
Description: `(empty the trash and take care the boxes are not ultra disgusting)`,
|
|
IsOptional: false,
|
|
},
|
|
{
|
|
Name: "Plants",
|
|
Description: `(take care of the plants. they need water, but not too much!)`,
|
|
IsOptional: false,
|
|
},
|
|
{
|
|
Name: "Fridge",
|
|
Description: `(Check the fridges (kitchen and main room) for nasty stuff and try to remove it.)`,
|
|
IsOptional: false,
|
|
},
|
|
{
|
|
Name: "Check Mailbox",
|
|
Description: `(Go to mailbox, open it, look if there are letters inside, if yes bring them into malo)`,
|
|
IsOptional: false,
|
|
},
|
|
{
|
|
Name: "Poster Check",
|
|
Description: `(Check current Posters in the main window, make sure they look nice and are not outdated)`,
|
|
IsOptional: false,
|
|
},
|
|
{
|
|
Name: "Weekly Insta Post",
|
|
Description: `(Post something on instagram, like a newly printed zine, kuefa or whatever)`,
|
|
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, Data text);"
|
|
_, err = db.Exec(sqlStmt)
|
|
if err != nil {
|
|
log.Printf("%q: %s\n", err, sqlStmt)
|
|
return nil
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
func GetTaskNameString(FillString string) string {
|
|
result := "Data"
|
|
IsFirst := true
|
|
for _, task := range GetDefaultTasks() {
|
|
if !IsFirst {
|
|
result += FillString
|
|
}
|
|
result += task.Name
|
|
IsFirst = false
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func CreateTasklist(date string, db *sql.DB) Tasklist {
|
|
stmt, err := db.Prepare("select " + "Data" + " from tasklists where name = ?")
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
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{
|
|
Date: date,
|
|
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.Date).Scan(&name)
|
|
|
|
if err != nil {
|
|
_, 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 "+"Data"+" = ? where name = ?", TasksToJson(tasklist.Tasks), tasklist.Date)
|
|
fmt.Println(TasksToJson(tasklist.Tasks))
|
|
|
|
if err != nil {
|
|
fmt.Println("Error during update: ", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type Task struct {
|
|
Name string `json:"Name"`
|
|
Description string `json:"Description"`
|
|
Value string `json:"Value"`
|
|
IsOptional bool `json:"IsOptional"`
|
|
}
|
|
|
|
type Tasklist struct {
|
|
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
|
|
}
|
|
|
|
type QueryResult struct {
|
|
Tasklists []Tasklist
|
|
}
|
|
|
|
func (n *Tasklist) Print() {
|
|
fmt.Println(n.Date)
|
|
|
|
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 = "./tasklistJson.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("/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)}
|
|
|
|
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.Date == 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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|