-
Notifications
You must be signed in to change notification settings - Fork 33
/
category.go
188 lines (166 loc) · 4.11 KB
/
category.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
184
185
186
187
188
package postgres
import (
"database/sql"
"errors"
"strings"
sq "github.com/Masterminds/squirrel"
"github.com/ncarlier/readflow/pkg/model"
)
var categoryColumns = []string{
"id",
"user_id",
"title",
"created_at",
"updated_at",
}
func mapRowToCategory(row *sql.Row) (*model.Category, error) {
cat := &model.Category{}
err := row.Scan(
&cat.ID,
&cat.UserID,
&cat.Title,
&cat.CreatedAt,
&cat.UpdatedAt,
)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, mapError(err)
}
return cat, nil
}
// CreateCategoryForUser create a category for an user
func (pg *DB) CreateCategoryForUser(uid uint, form model.CategoryCreateForm) (*model.Category, error) {
query, args, _ := pg.psql.Insert(
"categories",
).Columns(
"user_id", "title",
).Values(
uid,
strings.TrimSpace(form.Title),
).Suffix(
"RETURNING " + strings.Join(categoryColumns, ","),
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToCategory(row)
}
// UpdateCategoryForUser update a category for an user
func (pg *DB) UpdateCategoryForUser(uid uint, form model.CategoryUpdateForm) (*model.Category, error) {
update := map[string]interface{}{
"updated_at": "NOW()",
}
if form.Title != nil {
update["title"] = strings.TrimSpace(*form.Title)
}
query, args, _ := pg.psql.Update(
"categories",
).SetMap(update).Where(
sq.Eq{"id": form.ID},
).Where(
sq.Eq{"user_id": uid},
).Suffix(
"RETURNING " + strings.Join(categoryColumns, ","),
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToCategory(row)
}
// GetCategoryByID returns a category from the DB
func (pg *DB) GetCategoryByID(id uint) (*model.Category, error) {
query, args, _ := pg.psql.Select(categoryColumns...).From(
"categories",
).Where(
sq.Eq{"id": id},
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToCategory(row)
}
// GetCategoryByUserAndTitle returns a category of an user form the DB
func (pg *DB) GetCategoryByUserAndTitle(uid uint, title string) (*model.Category, error) {
query, args, _ := pg.psql.Select(categoryColumns...).From(
"categories",
).Where(
sq.Eq{"user_id": uid},
).Where(
sq.Eq{"title": title},
).ToSql()
row := pg.db.QueryRow(query, args...)
return mapRowToCategory(row)
}
// GetCategoriesByUser returns categories of an user from DB
func (pg *DB) GetCategoriesByUser(uid uint) ([]model.Category, error) {
query, args, _ := pg.psql.Select(categoryColumns...).From(
"categories",
).Where(
sq.Eq{"user_id": uid},
).OrderBy("title ASC").ToSql()
rows, err := pg.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var result []model.Category
for rows.Next() {
cat := model.Category{}
err = rows.Scan(
&cat.ID,
&cat.UserID,
&cat.Title,
&cat.CreatedAt,
&cat.UpdatedAt,
)
if err != nil {
return nil, err
}
result = append(result, cat)
}
err = rows.Err()
if err != nil {
return nil, err
}
return result, nil
}
// CountCategoriesByUser returns total nb of categories of an user from the DB
func (pg *DB) CountCategoriesByUser(uid uint) (uint, error) {
counter := pg.psql.Select("count(*)").From(
"categories",
).Where(sq.Eq{"user_id": uid})
query, args, _ := counter.ToSql()
var count uint
if err := pg.db.QueryRow(query, args...).Scan(&count); err != nil {
return 0, err
}
return count, nil
}
// DeleteCategoryByUser removes an category from the DB
func (pg *DB) DeleteCategoryByUser(uid, id uint) error {
query, args, _ := pg.psql.Delete("categories").Where(
sq.Eq{"id": id},
).Where(
sq.Eq{"user_id": uid},
).ToSql()
result, err := pg.db.Exec(query, args...)
if err != nil {
return err
}
count, err := result.RowsAffected()
if err != nil {
return err
}
if count == 0 {
return errors.New("no category has been removed")
}
return nil
}
// DeleteCategoriesByUser removes categories from the DB
func (pg *DB) DeleteCategoriesByUser(uid uint, ids []uint) (int64, error) {
query, args, _ := pg.psql.Delete("categories").Where(
sq.Eq{"user_id": uid},
).Where(
sq.Eq{"id": ids},
).ToSql()
result, err := pg.db.Exec(query, args...)
if err != nil {
return 0, err
}
return result.RowsAffected()
}