-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.go
183 lines (159 loc) · 3.59 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package graphql
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/google/uuid"
)
// A Log is a journal entry by an individual.
type Log struct {
ID string `json:"id"`
Sector Sector `json:"sector"`
Description string `json:"description"`
Project string `json:"project"`
User User `json:"user"`
Started time.Time `json:"started"`
Stopped time.Time `json:"stopped"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
// IsLinkable exists to show that this method implements the Linkable type in
// graphql.
func (l *Log) IsLinkable() {}
// URI returns the URI for this log.
func (l *Log) URI() *URI {
url := fmt.Sprintf("https://etu.natwelch.com/log/%s", l.ID)
return NewURI(url)
}
func (l *Log) GetURI() URI {
return *l.URI()
}
func (l *Log) Duration() (Duration, error) {
return ParseDurationFromDuration(l.Stopped.Sub(l.Started)), nil
}
// Save inserts or updates a log into the database.
func (l *Log) Save(ctx context.Context) error {
if l.ID == "" {
uuid, err := uuid.NewRandom()
if err != nil {
return err
}
l.ID = uuid.String()
}
if l.Started.IsZero() {
return fmt.Errorf("started cannot be nil")
}
if l.Stopped.IsZero() {
return fmt.Errorf("stopped cannot be nil")
}
if l.Created.IsZero() {
l.Created = time.Now()
}
l.Modified = time.Now()
if l.User.Empty() {
return fmt.Errorf("no user specified")
}
if _, err := db.ExecContext(
ctx,
`
INSERT INTO logs(id, description, project, sector, started, stopped, user_id, created_at, modified_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (id) DO UPDATE
SET (description, project, sector, started, stopped, user_id, modified_at) = ($2, $3, $4, $5, $6, $7, $9)
WHERE logs.id = $1;
`,
l.ID,
l.Description,
l.Project,
l.Sector,
l.Started,
l.Stopped,
l.User.ID,
l.Created,
l.Modified,
); err != nil {
return fmt.Errorf("upsert log: %w", err)
}
return nil
}
// SetUser looks up a user by ID and then sets it for this log.
func (l *Log) SetUser(ctx context.Context, id string) error {
u, err := GetUser(ctx, id)
if err != nil {
return err
}
if u != nil {
l.User = *u
}
return nil
}
// UserLogs gets all logs for a User.
func UserLogs(ctx context.Context, u *User, limit int, offset int) ([]*Log, error) {
if u == nil {
return nil, fmt.Errorf("no user specified")
}
rows, err := db.QueryContext(
ctx, `
SELECT id, description, project, sector, started, stopped, user_id, created_at, modified_at
FROM logs
WHERE user_id = $1
ORDER BY datetime DESC
LIMIT $2 OFFSET $3
`,
u.ID, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
logs := make([]*Log, 0)
for rows.Next() {
l := &Log{}
if err := rows.Scan(
&l.ID,
&l.Description,
&l.Project,
&l.Sector,
&l.Started,
&l.Stopped,
&l.User.ID,
&l.Created,
&l.Modified,
); err != nil {
return nil, err
}
logs = append(logs, l)
}
if err = rows.Err(); err != nil {
return nil, err
}
return logs, nil
}
// GetLog gets a single Log by ID.
func GetLog(ctx context.Context, id string) (*Log, error) {
l := &Log{}
row := db.QueryRowContext(ctx, `
SELECT id, description, project, sector, started, stopped, user_id, created_at, modified_at
FROM logs
WHERE id = $1
`, id)
err := row.Scan(
&l.ID,
&l.Description,
&l.Project,
&l.Sector,
&l.Started,
&l.Stopped,
&l.User.ID,
&l.Created,
&l.Modified,
)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf("error with get: %w", err)
default:
return l, nil
}
}