-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
86 lines (68 loc) · 1.52 KB
/
log.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
package controller
import (
"context"
"errors"
"time"
"cloud.google.com/go/datastore"
"github.com/tdu-logcation/api/database"
"github.com/tdu-logcation/api/utils"
)
type Log struct {
userId string
userIdKey string
database *database.Database
}
func NewLog(ctx *context.Context, userId string) (*Log, error) {
if len(userId) == 0 {
return nil, errors.New("user id is none")
}
database, err := database.New(ctx)
if err != nil {
return nil, err
}
userIdKey := utils.ConvertUserIdKey(userId)
return &Log{
userId: userId,
userIdKey: userIdKey,
database: database,
}, nil
}
// ログの追加
func (c *Log) Add(campus string, date time.Time, logType string, label string, code string) error {
logId, err := utils.CreateId()
if err != nil {
return err
}
key := utils.CreateKey(c.userIdKey, logId)
entry := database.Log{
Id: logId,
Date: date,
Campus: campus,
LogType: logType,
Label: label,
Code: code,
}
return c.database.Put(key, &entry)
}
// 全ログ取得
func (c *Log) GetLogs() (*[]database.Log, error) {
query := datastore.NewQuery(c.userIdKey)
var posts []database.Log = []database.Log{}
_, err := c.database.GetAll(query, &posts)
if err != nil {
return nil, err
}
return &posts, nil
}
// 全ログ削除
func (c *Log) DeleteAll() error {
logs, err := c.GetLogs()
if err != nil {
return err
}
keys := []*datastore.Key{}
for _, log := range *logs {
keys = append(keys, utils.CreateKey(c.userIdKey, log.Id))
}
return c.database.DeleteMulti(keys)
}