-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres_societies.go
135 lines (124 loc) · 4.29 KB
/
postgres_societies.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
package persist
import (
"context"
"database/sql"
"log"
"strconv"
"github.com/lib/pq"
"github.com/ourrootsorg/cms-server/model"
"github.com/ourrootsorg/cms-server/utils"
)
// SelectSocietySummariesByID selects multiple society summaries
func (p PostgresPersister) SelectSocietySummariesByID(ctx context.Context, ids []uint32) ([]model.SocietySummary, error) {
societySummaries := make([]model.SocietySummary, 0)
rows, err := p.db.QueryContext(ctx, "SELECT id, body, insert_time, last_update_time FROM society "+
"WHERE id = ANY($1)", pq.Array(ids))
if err != nil {
return nil, translateError(err, nil, nil, "")
}
defer rows.Close()
for rows.Next() {
var society model.Society
err := rows.Scan(
&society.ID,
&society.SocietyBody,
&society.InsertTime,
&society.LastUpdateTime,
)
if err != nil {
return nil, translateError(err, nil, nil, "")
}
societySummary := createSocietySummary(&society)
societySummaries = append(societySummaries, *societySummary)
}
return societySummaries, nil
}
// SelectOneSociety loads a SocietySummary from the database
func (p PostgresPersister) SelectSocietySummary(ctx context.Context, id uint32) (*model.SocietySummary, error) {
var society model.Society
log.Printf("[DEBUG] id: %d", id)
err := p.db.QueryRowContext(ctx, "SELECT id, body, insert_time, last_update_time FROM society "+
"WHERE id=$1", id).Scan(
&society.ID,
&society.SocietyBody,
&society.InsertTime,
&society.LastUpdateTime,
)
if err != nil {
return nil, translateError(err, &id, nil, "")
}
return createSocietySummary(&society), nil
}
// SelectOneSociety loads the current Society from the database
func (p PostgresPersister) SelectSociety(ctx context.Context, id uint32) (*model.Society, error) {
var society model.Society
log.Printf("[DEBUG] id: %d", id)
err := p.db.QueryRowContext(ctx, "SELECT id, body, insert_time, last_update_time FROM society "+
"WHERE id=$1", id).Scan(
&society.ID,
&society.SocietyBody,
&society.InsertTime,
&society.LastUpdateTime,
)
if err != nil {
return nil, translateError(err, &id, nil, "")
}
return &society, nil
}
// InsertSociety inserts a SocietyBody into the database and returns the inserted Society
func (p PostgresPersister) InsertSociety(ctx context.Context, in model.SocietyIn) (*model.Society, error) {
var society model.Society
row := p.db.QueryRowContext(ctx, "INSERT INTO society (body) VALUES ($1) "+
"RETURNING id, body, insert_time, last_update_time", in)
err := row.Scan(
&society.ID,
&society.SocietyBody,
&society.InsertTime,
&society.LastUpdateTime,
)
return &society, translateError(err, nil, nil, "")
}
// UpdateSociety updates a Society in the database and returns the updated Society
func (p PostgresPersister) UpdateSociety(ctx context.Context, in model.Society) (*model.Society, error) {
societyID, err := utils.GetSocietyIDFromContext(ctx)
if err != nil {
return nil, err
}
var society model.Society
err = p.db.QueryRowContext(ctx, "UPDATE society SET body = $1, last_update_time = CURRENT_TIMESTAMP "+
"WHERE id = $2 AND last_update_time = $3 RETURNING id, body, insert_time, last_update_time",
in.SocietyBody, societyID, in.LastUpdateTime).
Scan(
&society.ID,
&society.SocietyBody,
&society.InsertTime,
&society.LastUpdateTime,
)
if err != nil && err == sql.ErrNoRows {
// Either non-existent or last_update_time didn't match
c, _ := p.SelectSociety(ctx, societyID)
if c != nil && c.ID == societyID {
// Row exists, so it must be a non-matching update time
return nil, model.NewError(model.ErrConcurrentUpdate, c.LastUpdateTime.String(), in.LastUpdateTime.String())
}
return nil, model.NewError(model.ErrNotFound, strconv.Itoa(int(societyID)))
}
return &society, translateError(err, &societyID, nil, "")
}
// DeleteSociety deletes a society
func (p PostgresPersister) DeleteSociety(ctx context.Context) error {
societyID, err := utils.GetSocietyIDFromContext(ctx)
if err != nil {
return err
}
_, err = p.db.ExecContext(ctx, "DELETE FROM society WHERE id = $1", societyID)
return translateError(err, &societyID, nil, "")
}
func createSocietySummary(society *model.Society) *model.SocietySummary {
return &model.SocietySummary{
ID: society.ID,
Name: society.Name,
InsertTime: society.InsertTime,
PostMetadata: society.PostMetadata,
}
}