Submit a task

This commit is contained in:
Bel LaPointe
2019-11-13 13:17:56 -07:00
parent ee77d9d3b7
commit cf1fd1dfed
13 changed files with 259 additions and 49 deletions

View File

@@ -1,7 +1,9 @@
package task
import (
"encoding/json"
"errors"
"fmt"
"local/todo-server/server/ajax/form"
"net/http"
"regexp"
@@ -16,7 +18,7 @@ type Task struct {
UUID string
Title string
Priority int
Tags []string
Tags StrList
Created time.Time
Edited time.Time
@@ -26,12 +28,19 @@ type Task struct {
Due time.Time
}
type StrList []string
func (sl StrList) MarshalJSON() ([]byte, error) {
s := strings.Join(sl, ", ")
return json.Marshal(s)
}
func New(r *http.Request) (*Task, error) {
task := &Task{
UUID: uuid.New().String(),
Title: form.Get(r, "title"),
Priority: form.ToInt(form.Get(r, "prio")),
Tags: form.ToStrArr(form.Get(r, "tags")),
Tags: StrList(form.ToStrArr(form.Get(r, "tags"))),
Created: time.Now(),
Edited: time.Now(),
@@ -42,6 +51,71 @@ func New(r *http.Request) (*Task, error) {
return task, task.validate()
}
func (t *Task) MarshalJSON() ([]byte, error) {
// {"total":4,"list":[
// {"id":"3455",
// "title":"redo qvolution",
// "listId":"18",
// "date":"14 Oct 2019 12:56 PM",
// "dateInt":1571079392,
// "dateInline":"14 Oct",
// "dateInlineTitle":"created at 14 Oct 2019 12:56 PM",
// "dateEditedInt":1571079401,
// "dateCompleted":"",
// "dateCompletedInline":"",
// "dateCompletedInlineTitle":"Completed at ",
// "compl":0,
// "prio":"0",
// "note":"",
// "noteText":"",
// "ow":4,
// "tags":"work",
// "tags_ids":"138",
// "duedate":"",
// "dueClass":"",
// "dueStr":"",
// "dueInt":33330000,
// "dueTitle":"Due "}
// ]}
fullFormat := "02 Jan 2006 03:04 PM"
shortFormat := "02 Jan"
compl := 0
if t.Complete {
compl = 1
}
m := map[string]interface{}{
"id": t.UUID,
"title": t.Title,
"listId": "list",
"date": t.Created.Format(fullFormat),
"dateInt": t.Created.Unix(),
"dateInline": t.Created.Format(shortFormat),
"dateInlineTitle": fmt.Sprintf("created at %s", t.Created.Format(fullFormat)),
"dateEditedInt": t.Edited.Unix(),
"dateCompleted": "",
"dateCompletedInline": "",
"dateCompletedInlineTitle": "",
"compl": compl,
"prio": t.Priority,
"note": strings.Join(t.Note, "\n"),
"noteText": strings.Join(t.Note, "\n"),
"ow": 0,
"tags": strings.Join([]string(t.Tags), ", "),
"tags_ids": "",
"duedate": t.Due.Format(fullFormat),
"dueClass": "",
"dueStr": t.Due.Format(shortFormat),
"dueInt": t.Due.Unix(),
"dueTitle": "Due ",
}
if t.Complete {
m["dateCompleted"] = t.Completed.Format(fullFormat)
m["dateCompletedInline"] = t.Completed.Format(shortFormat)
m["dateCompletedInlineTitle"] = "Completed at "
}
return json.Marshal(m)
}
func (t *Task) AppendTags(tags []string) {
t.touch()
t.Tags = append(t.Tags, tags...)