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
|
package repos
import "apjournal/internal/models"
type ActionRepo interface {
DBActionCreate(req *models.Action) error
DBActionList(username string) ([]models.Action, error)
DBActionGetByName(name string) (*models.Action, error)
DBActionDone(name string) error
DBActionsToReset() error
DBActionRecommend(username string) ([]models.Action, error)
}
func (p *Provider) DBActionRecommend(
username string,
) ([]models.Action, error) {
resp := []models.Action{}
query := `SELECT * FROM action WHERE name NOT IN (select name from action where username == $1) GROUP BY name LIMIT 100;`
if err := p.db.Select(&resp, query, username); err != nil {
return nil, err
}
return resp, nil
}
func (p *Provider) DBActionCreate(req *models.Action) error {
_, err := p.db.NamedExec(`
INSERT INTO action(username, name, magnitude, repeatable, type, done)
VALUES (:username, :name, :magnitude, :repeatable, :type, :done);`, req)
return err
}
func (p *Provider) DBActionList(username string) ([]models.Action, error) {
resp := []models.Action{}
query := "SELECT * FROM action WHERE username=$1;"
if err := p.db.Select(&resp, query, username); err != nil {
return nil, err
}
return resp, nil
}
func (p *Provider) DBActionGetByName(name string) (*models.Action, error) {
resp := models.Action{}
query := "SELECT * FROM action WHERE name=$1;"
if err := p.db.Get(&resp, query, name); err != nil {
return nil, err
}
return &resp, nil
}
func (p *Provider) DBActionDone(name string) error {
// should reset at burn time
stmt := "UPDATE action SET done=true WHERE name=$1;"
_, err := p.db.Exec(stmt, name)
return err
}
func (p *Provider) DBActionsToReset() error {
stmts := []string{`UPDATE action SET done=false WHERE username IN (SELECT username FROM user_score WHERE burn_time < NOW());`,
`UPDATE user_score SET burn_time=burn_time + INTERVAL '24 hours' WHERE burn_time < NOW();`}
for _, s := range stmts {
if _, err := p.db.Exec(s); err != nil {
// rollback
return err
}
}
return nil
}
|