forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
60 lines (50 loc) · 1.43 KB
/
update.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
// Copyright © 2019 Martin Tournoij <[email protected]>
// This file is part of GoatCounter and published under the terms of the EUPL
// v1.2, which can be found in the LICENSE file or at http://eupl12.zgo.at
package goatcounter
import (
"context"
"database/sql"
"time"
"github.com/pkg/errors"
"zgo.at/zdb"
)
/* Adding a new update:
insert into updates(created_at, show_at, subject, body) values (now(), now(),
"...", "...");
update users set unseen_updates=1;
*/
type Update struct {
ID int64 `db:"id"`
Subject string `db:"subject"`
Body string `db:"body"`
CreatedAt time.Time `db:"created_at"`
ShowAt time.Time `db:"show_at"`
Seen bool `db:"-"`
}
type Updates []Update
// HasSince reports if there are any updates siocne the goven date.
func (u *Updates) HasSince(ctx context.Context, since time.Time) (bool, error) {
var has bool
err := zdb.MustGet(ctx).GetContext(ctx, &has,
`select 1 from updates where show_at >= $1`, since)
if err == sql.ErrNoRows {
err = nil
}
return has, errors.Wrap(err, "Updates.ListUnseen")
}
// Lists all updates.
func (u *Updates) List(ctx context.Context, since time.Time) error {
err := zdb.MustGet(ctx).SelectContext(ctx, u, `select * from updates order by show_at desc`)
if err != nil {
return errors.Wrap(err, "Updates.ListUnseen")
}
uu := *u
for i := range uu {
if since.After(uu[i].ShowAt) {
break
}
uu[i].Seen = true
}
return nil
}