/
models.go
132 lines (120 loc) · 4.55 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"time"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/user"
)
// T2JPUser is the kind which stores data of users who comment and have favourite list
type T2JPUser struct {
KeyName string `json:"key_name" datastore:"KeyName"`
Name string `json:"name" datastore:"Name"`
Email string `json:"email" datastore:"Email"`
IconURL string `json:"icon_url" datastore:"IconURL"`
Favourites []string `json:"favourites" datastore:"Favourites"` // list of SpotCode
IsEditor bool `json:"is_editor" datastore:"IsEditor"`
IsAdmin bool `json:"is_admin" datastore:"IsAdmin"`
UpdatedAt time.Time `json:"updated_at" datastore:"UpdatedAt"`
CreatedAt time.Time `json:"created_at" datastore:"CreatedAt"`
}
func (u *T2JPUser) key(ctx context.Context) *datastore.Key {
return datastore.NewKey(ctx, "T2JPUser", u.KeyName, 0, nil)
}
//Create new T2JPUser entity
func (u *T2JPUser) Create(ctx context.Context) (*T2JPUser, error) {
currentUser := user.Current(ctx)
u.KeyName = currentUser.ID
u.Email = currentUser.Email
u.CreatedAt = time.Now()
u.UpdatedAt = time.Now()
_, err := datastore.Put(ctx, u.key(ctx), u)
if err != nil {
return nil, err
}
return u, nil
}
//Update existing T2JPUser entity
func (u *T2JPUser) Update(ctx context.Context) (*T2JPUser, error) {
u.UpdatedAt = time.Now()
_, err := datastore.Put(ctx, u.key(ctx), u)
if err != nil {
return nil, err
}
return u, nil
}
// Spot is the kind which stores sightseeing spot information
// Set SpotCode as KeyName
type Spot struct {
SpotCode int64 `json:"-" datastore:"SpotCode"`
RevisionNumber int `json:"-" datastore:"Revision"` // increment on update
Status string `json:"status" datastore:"Status"` // 'post' or 'revision' or 'draft'
SpotName string `json:"spot_name" datastore:"SpotName"`
Body string `json:"body" datastore:"Body,noindex"`
Photos []string `json:"photos" datastore:"Photos"`
GeoInfo appengine.GeoPoint `json:"geo_info" datastore:"GeoInfo"`
Phone string `json:"phone" datastore:"Phone"`
Editor string `json:"-" datastore:"Editor"`
UpdatedAt time.Time `json:"-" datastore:"UpdatedAt"`
CreatedAt time.Time `json:"-" datastore:"CreatedAt"`
}
// Spot Struct for Listing
type SpotGet struct {
SpotCode int64 `json:"spot_code" datastore:"SpotCode"`
RevisionNumber int `json:"revision_number" datastore:"Revision"` // increment on update
Status string `json:"status" datastore:"Status"` // 'post' or 'revision' or 'draft'
SpotName string `json:"spot_name" datastore:"SpotName"`
Body string `json:"body" datastore:"Body,noindex"`
Photos []string `json:"photos" datastore:"Photos"`
GeoInfo appengine.GeoPoint `json:"geo_info" datastore:"GeoInfo"`
Phone string `json:"phone" datastore:"Phone"`
Editor string `json:"editor" datastore:"Editor"`
UpdatedAt time.Time `json:"updated_at" datastore:"UpdatedAt"`
CreatedAt time.Time `json:"created_at" datastore:"CreatedAt"`
}
func (s *Spot) key(ctx context.Context) *datastore.Key {
if s.SpotCode == 0 {
low, _, err := datastore.AllocateIDs(ctx, "Spot", nil, 1)
if err != nil {
return nil
}
return datastore.NewKey(ctx, "Spot", "", low, nil)
}
return datastore.NewKey(ctx, "Spot", "", s.SpotCode, nil)
}
func (s *SpotGet) key(ctx context.Context) *datastore.Key {
if s.SpotCode == 0 {
low, _, err := datastore.AllocateIDs(ctx, "Spot", nil, 1)
if err != nil {
return nil
}
return datastore.NewKey(ctx, "Spot", "", low, nil)
}
return datastore.NewKey(ctx, "Spot", "", s.SpotCode, nil)
}
//Create new Spot Entity
func (s *Spot) Create(ctx context.Context) (*Spot, error) {
currentUser := user.Current(ctx)
s.Editor = currentUser.ID
s.Status = "draft"
s.CreatedAt = time.Now()
s.UpdatedAt = time.Now()
newKey := s.key(ctx)
s.SpotCode = newKey.IntID()
_, err := datastore.Put(ctx, newKey, s)
if err != nil {
return nil, err
}
return s, nil
}
//Update existing Spot Entity
func (s *Spot) Update(ctx context.Context, spotCode int64) (*Spot, error) {
s.SpotCode = spotCode
s.RevisionNumber = s.RevisionNumber + 1
s.UpdatedAt = time.Now()
_, err := datastore.Put(ctx, s.key(ctx), s)
if err != nil {
return nil, err
}
return s, nil
}