/
todo_list_postgres.go
143 lines (112 loc) · 3.21 KB
/
todo_list_postgres.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
package repository
import (
"errors"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"github.com/Inexpediency/todo-rest-api/pkg/dto"
"github.com/Inexpediency/todo-rest-api/pkg/models"
)
type TodoListPostgres struct {
db *sqlx.DB
}
func NewTodoListPostgres(db *sqlx.DB) *TodoListPostgres {
return &TodoListPostgres{db: db}
}
func (r *TodoListPostgres) Create(userId int, list models.TodoList) (int, error) {
tx, err := r.db.Begin()
if err != nil {
return 0, err
}
var listId int
createListQuery := fmt.Sprintf(
"INSERT INTO %s (title, description) VALUES ($1, $2) RETURNING id",
todoListsTable,
)
row := r.db.QueryRow(createListQuery, list.Title, list.Description)
if err := row.Scan(&listId); err != nil {
tx.Rollback()
return 0, err
}
createUserListRelationQuery := fmt.Sprintf(
"INSERT INTO %s (user_id, list_id) VALUES ($1, $2)",
usersListsTable,
)
_, err = r.db.Exec(createUserListRelationQuery, userId, listId)
if err != nil {
tx.Rollback()
return 0, err
}
if err := tx.Commit(); err != nil {
tx.Rollback()
return 0, err
}
return listId, nil
}
func (r *TodoListPostgres) GetAll(userId int) ([]models.TodoList, error) {
var lists []models.TodoList
query := fmt.Sprintf(
"SELECT tl.id, tl.title, tl.description FROM %s tl INNER JOIN %s ul on tl.id = ul.list_id WHERE ul.user_id = $1",
todoListsTable, usersListsTable,
)
err := r.db.Select(&lists, query, userId)
return lists, err
}
func (r *TodoListPostgres) GetById(userId, listId int) (models.TodoList, error) {
var list models.TodoList
query := fmt.Sprintf(
`SELECT tl.id, tl.title, tl.description FROM %s tl
INNER JOIN %s ul on tl.id = ul.list_id
WHERE ul.user_id = $1 AND ul.list_id = $2`,
todoListsTable, usersListsTable,
)
err := r.db.Get(&list, query, userId, listId)
return list, err
}
func (r *TodoListPostgres) Delete(userId, listId int) error {
query := fmt.Sprintf(
`DELETE FROM %s tl USING %s ul
WHERE tl.id = ul.list_id AND ul.user_id = $1 AND ul.list_id = $2`,
todoListsTable, usersListsTable,
)
result, err := r.db.Exec(query, userId, listId)
if err != nil {
return err
}
if n, _ := result.RowsAffected(); n == 0 {
return errors.New("there is no todo list with such id")
}
return nil
}
func (r *TodoListPostgres) Update(userId, listId int, input dto.UpdateList) error {
setValues := make([]string, 0)
args := make([]interface{}, 0)
argId := 1
if input.Title != nil {
setValues = append(setValues, fmt.Sprintf("title=$%d", argId))
args = append(args, *input.Title)
argId++
}
if input.Description != nil {
setValues = append(setValues, fmt.Sprintf("description=$%d", argId))
args = append(args, *input.Description)
argId++
}
setQuery := strings.Join(setValues, ", ")
query := fmt.Sprintf(`
UPDATE %s tl SET %s FROM %s ul
WHERE tl.id=ul.list_id AND ul.list_id=$%d AND ul.user_id=$%d`,
todoListsTable, setQuery, usersListsTable, argId, argId+1,
)
args = append(args, listId, userId)
logrus.Debugf("updateQuery: %s\n, args: %s\n", setQuery, args)
result, err := r.db.Exec(query, args...)
if err != nil {
return err
}
if n, _ := result.RowsAffected(); n == 0 {
return errors.New("there is no todo list with such id")
}
return nil
}