-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
59 lines (53 loc) · 1.81 KB
/
models.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
56
57
58
59
package main
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"gorm.io/gorm"
)
// custom data type to store dates i.e. DueDate and CompletionDate
// format --> dd/mm/YYYY
type Date struct{
sql.NullTime
}
// marshal function for custom data type
func (date Date) MarshalJSON() ([]byte, error) {
if(date.Valid){
return json.Marshal(fmt.Sprintf("%02d-%02d-%d",date.Time.Day(),date.Time.Month(),date.Time.Year()))
}
return json.Marshal(nil)
}
// unmarshal function for custom data type
func (date *Date) UnmarshalJSON(data []byte) error {
s := strings.Trim(string(data), "\"")
if s!="null" {
t, err := time.Parse("02-01-2006",s)
if err != nil {
return err
}
date.Time = t
date.Valid = true
}else{
date.Time = time.Time{}
date.Valid = false
}
return nil
}
// model for mysql database
// constraints involve -->
// 1. Name, Priority and DueDate should not be null.
// 2. String size of Name should be atleast 4 chars and atmost 256 chars.
// 3. Priority can only be one of LOW, MED and HIGH
// 4. If TodoItem is completed (i.e. Completed is true) then Completion date should be also be present and similarly
// if it isn't completed (i.e. Completed is false) then CompletionDate can't be present
type TodoItem struct{
gorm.Model
Name string `json:"name" gorm:"not null;size:256;check:name_length_check,length(name)>3"`
Description string `json:"description"`
Priority string `json:"priority" gorm:"not null;default:'LOW';check:priority_check,priority in ('HIGH', 'LOW', 'MED')"`
DueDate Date `json:"dueDate" gorm:"not null"`
Completed bool `json:"completed" gorm:"default:false"`
CompletionDate Date `json:"completionDate" gorm:"check:completion_date_check,(completed = false AND completion_date IS NULL) OR (completed = true AND completion_date IS NOT NULL) "`
}