This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
/
affiliation.go
93 lines (75 loc) · 1.72 KB
/
affiliation.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
package db
import (
"context"
"gopkg.in/guregu/null.v4"
"github.com/pkg/errors"
"github.com/BenJetson/CPSC491-project/go/app"
)
func (db *database) AddPersonAffiliation(
ctx context.Context,
personID, orgID int,
role app.Role,
) error {
var points null.Int
if role == app.RoleDriver {
points = null.IntFrom(0)
}
_, err := db.ExecContext(ctx, `
INSERT INTO affiliation (
person_id,
organization_id,
points
) VALUES ($1, $2, $3)
ON CONFLICT (person_id, organization_id)
DO UPDATE SET points = $3
`, personID, orgID, points)
return errors.Wrap(err, "failed to add affiliation")
}
func (db *database) RemovePersonAffiliation(
ctx context.Context,
personID, orgID int,
) error {
_, err := db.ExecContext(ctx, `
DELETE FROM affiliation
WHERE
person_id = $1
AND organization_id = $2
`, personID, orgID)
return errors.Wrap(err, "failed to delete affiliation")
}
func (db *database) SetPointsForAffiliation(
ctx context.Context,
personID, orgID int,
points null.Int,
) error {
_, err := db.ExecContext(ctx, `
UPDATE affiliation SET
points = $1
WHERE
person_id = $2
AND organization
`, points, personID, orgID)
return errors.Wrap(err, "failed to update points for affiliation")
}
func (db *database) GetBalancesForPerson(
ctx context.Context,
personID int,
) ([]app.Balance, error) {
var bs []app.Balance
err := db.SelectContext(ctx, &bs, `
SELECT
p.person_id,
p.first_name,
p.last_name,
o.organization_id,
o.name,
a.points
FROM affiliation a
JOIN person p
ON a.person_id = p.person_id
JOIN organization o
ON a.organization_id = o.organization_id
WHERE a.person_id = $1
`, personID)
return bs, errors.Wrap(err, "failed to retrieve balances")
}