-
Notifications
You must be signed in to change notification settings - Fork 2
/
todo.go
55 lines (46 loc) · 1.39 KB
/
todo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/Fs02/grimoire/changeset"
"github.com/Fs02/grimoire/params"
)
// TodoTable definse table name for stroing todos in database.
const TodoTable = "todos"
// Todo respresent a record stored in todos table.
type Todo struct {
ID uint
Title string
Order int
Completed bool
}
// MarshalJSON implement custom marshaller to marshal url.
func (todo Todo) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ID uint `json:"id"`
Title string `json:"title"`
Order int `json:"order"`
Completed bool `json:"completed"`
URL string `json:"url"`
}{
ID: todo.ID,
Title: todo.Title,
Order: todo.Order,
Completed: todo.Completed,
URL: fmt.Sprint(os.Getenv("URL"), todo.ID),
})
}
// ChangeTodo prepares data before database operation.
func ChangeTodo(todo interface{}, params params.Params) *changeset.Changeset {
ch := changeset.Cast(todo, params, []string{"title", "order", "completed"})
changeset.ValidateRequired(ch, []string{"title"})
changeset.ValidateRange(ch, "title", 1, 255)
return ch
}
// CreateTodo is similar to ChangeTodo, except it also fills some default values and used before insert operation.
func CreateTodo(params params.Params) *changeset.Changeset {
ch := ChangeTodo(Todo{}, params)
changeset.PutChange(ch, "completed", false)
return ch
}