diff --git a/pages/category.html b/pages/category.html index 39c178c..14f78e5 100644 --- a/pages/category.html +++ b/pages/category.html @@ -20,14 +20,34 @@ Are you sure you want to delete this ToDo? + + + + - + +
- +
- - + +
- + +
+
+
@@ -84,7 +108,7 @@ {{ range .Todos }} {{ if not .IsDone }} - {{ .Text }} + {{ .Text }} {{ .TimeCreated }} {{ .Due }} {{ .DueUnix }} @@ -128,32 +152,38 @@
- + +{{ end }} \ No newline at end of file diff --git a/src/db/db.go b/src/db/db.go index 130f337..a869589 100644 --- a/src/db/db.go +++ b/src/db/db.go @@ -65,6 +65,7 @@ func setUpTables(db *DB) error { owner_login TEXT NOT NULL, is_done INTEGER, completion_time_unix INTEGER, + image BLOB, FOREIGN KEY(group_id) REFERENCES todo_groups(id), FOREIGN KEY(owner_login) REFERENCES users(login))`, ) diff --git a/src/db/todo.go b/src/db/todo.go index 6c72421..b25728c 100644 --- a/src/db/todo.go +++ b/src/db/todo.go @@ -33,11 +33,21 @@ type Todo struct { OwnerLogin string `json:"ownerLogin"` IsDone bool `json:"isDone"` CompletionTimeUnix uint64 `json:"completionTimeUnix"` + Image []byte `json:"image"` TimeCreated string CompletionTime string Due string } +func unixToTimeStr(unixTimeSec uint64) string { + timeUnix := time.Unix(int64(unixTimeSec), 0) + if timeUnix.Year() == 1970 { + return "None" + } else { + return timeUnix.Format(time.DateOnly) + } +} + func scanTodo(rows *sql.Rows) (*Todo, error) { var newTodo Todo err := rows.Scan( @@ -49,32 +59,16 @@ func scanTodo(rows *sql.Rows) (*Todo, error) { &newTodo.OwnerLogin, &newTodo.IsDone, &newTodo.CompletionTimeUnix, + &newTodo.Image, ) if err != nil { return nil, err } // Convert to Basic time - timeCreated := time.Unix(int64(newTodo.TimeCreatedUnix), 0) - if timeCreated.Year() == 1970 { - newTodo.TimeCreated = "None" - } else { - newTodo.TimeCreated = timeCreated.Format(time.DateOnly) - } - - due := time.Unix(int64(newTodo.DueUnix), 0) - if due.Year() == 1970 { - newTodo.Due = "None" - } else { - newTodo.Due = due.Format(time.DateOnly) - } - - completionTime := time.Unix(int64(newTodo.CompletionTimeUnix), 0) - if completionTime.Year() == 1970 { - newTodo.CompletionTime = "None" - } else { - newTodo.CompletionTime = completionTime.Format(time.DateOnly) - } + newTodo.TimeCreated = unixToTimeStr(newTodo.TimeCreatedUnix) + newTodo.Due = unixToTimeStr(newTodo.DueUnix) + newTodo.CompletionTime = unixToTimeStr(newTodo.CompletionTimeUnix) return &newTodo, nil } @@ -123,7 +117,7 @@ func (db *DB) GetTodos() ([]*Todo, error) { // Creates a new TODO in the database func (db *DB) CreateTodo(todo Todo) error { _, err := db.Exec( - "INSERT INTO todos(group_id, text, time_created_unix, due_unix, owner_login, is_done, completion_time_unix) VALUES(?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO todos(group_id, text, time_created_unix, due_unix, owner_login, is_done, completion_time_unix, image) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", todo.GroupID, todo.Text, todo.TimeCreatedUnix, @@ -131,6 +125,7 @@ func (db *DB) CreateTodo(todo Todo) error { todo.OwnerLogin, todo.IsDone, todo.CompletionTimeUnix, + todo.Image, ) return err @@ -146,15 +141,16 @@ func (db *DB) DeleteTodo(id uint64) error { return err } -// Updates TODO's due date, text, done state, completion time and group id +// Updates TODO's due date, text, done state, completion time and group id with image func (db *DB) UpdateTodo(todoID uint64, updatedTodo Todo) error { _, err := db.Exec( - "UPDATE todos SET group_id=?, due_unix=?, text=?, is_done=?, completion_time_unix=? WHERE id=?", + "UPDATE todos SET group_id=?, due_unix=?, text=?, is_done=?, completion_time_unix=?, image=? WHERE id=?", updatedTodo.GroupID, updatedTodo.DueUnix, updatedTodo.Text, updatedTodo.IsDone, updatedTodo.CompletionTimeUnix, + updatedTodo.Image, todoID, ) diff --git a/src/server/page.go b/src/server/page.go index 081c371..05d55da 100644 --- a/src/server/page.go +++ b/src/server/page.go @@ -20,23 +20,8 @@ package server import ( "Unbewohnte/dela/db" - "html/template" - "path/filepath" ) -// Constructs a pageName template via inserting basePageName in pagesDir -func getPage(pagesDir string, basePageName string, pageName string) (*template.Template, error) { - page, err := template.ParseFiles( - filepath.Join(pagesDir, basePageName), - filepath.Join(pagesDir, pageName), - ) - if err != nil { - return nil, err - } - - return page, nil -} - type IndexPageData struct { Groups []*db.TodoGroup `json:"groups"` } diff --git a/src/server/server.go b/src/server/server.go index b3cbb58..5a3c0e5 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -30,6 +30,7 @@ import ( "path" "path/filepath" "strconv" + "text/template" "time" ) @@ -105,6 +106,7 @@ func New(config conf.Conf) (*Server, error) { ) // handle page requests + pagesDirPath := filepath.Join(server.config.BaseContentDir, PagesDirName) mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { if req.Method != "GET" { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) @@ -118,8 +120,9 @@ func New(config conf.Conf) (*Server, error) { return } - requestedPage, err := getPage( - filepath.Join(server.config.BaseContentDir, PagesDirName), "base.html", "index.html", + requestedPage, err := template.ParseFiles( + filepath.Join(pagesDirPath, "base.html"), + filepath.Join(pagesDirPath, "index.html"), ) if err != nil { http.Redirect(w, req, "/error", http.StatusTemporaryRedirect) @@ -166,8 +169,10 @@ func New(config conf.Conf) (*Server, error) { return } - requestedPage, err := getPage( - filepath.Join(server.config.BaseContentDir, PagesDirName), "base.html", "category.html", + requestedPage, err := template.ParseFiles( + filepath.Join(pagesDirPath, "base.html"), + filepath.Join(pagesDirPath, "paint.html"), + filepath.Join(pagesDirPath, "category.html"), ) if err != nil { http.Redirect(w, req, "/error", http.StatusTemporaryRedirect) @@ -192,10 +197,9 @@ func New(config conf.Conf) (*Server, error) { } else { // default - requestedPage, err := getPage( - filepath.Join(server.config.BaseContentDir, PagesDirName), - "base.html", - req.URL.Path[1:]+".html", + requestedPage, err := template.ParseFiles( + filepath.Join(pagesDirPath, "base.html"), + filepath.Join(pagesDirPath, req.URL.Path[1:]+".html"), ) if err == nil { err = requestedPage.ExecuteTemplate(w, req.URL.Path[1:]+".html", nil)