/
todo_item_postgres.go
108 lines (88 loc) · 3.11 KB
/
todo_item_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
package repository
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/oneils/todo-app"
"strings"
)
type TodoItemPostgres struct {
db *sqlx.DB
}
func NewTodoItemPostgres(db *sqlx.DB) *TodoItemPostgres {
return &TodoItemPostgres{db: db}
}
func (r *TodoItemPostgres) Create(listId int, item todo.TodoItem) (int, error) {
tx, err := r.db.Begin()
if err != nil {
return 0, err
}
var itemId int
createItemQuery := fmt.Sprintf("INSERT INTO %s (title, description) values ($1, $2) RETURNING id", todoItemsTable)
row := tx.QueryRow(createItemQuery, item.Title, item.Description)
err = row.Scan(&itemId)
if err != nil {
tx.Rollback()
return 0, err
}
createListItemsQuery := fmt.Sprintf("INSERT INTO %s (list_id, item_id) values ($1, $2)", listsItemsTable)
_, err = tx.Exec(createListItemsQuery, listId, itemId)
if err != nil {
tx.Rollback()
return 0, err
}
return itemId, tx.Commit()
}
func (r *TodoItemPostgres) GetAll(userId, listId int) ([]todo.TodoItem, error) {
var items []todo.TodoItem
query := fmt.Sprintf(`SELECT ti.id, ti.title, ti.description, ti.done FROM %s ti INNER JOIN %s li on li.item_id = ti.id
INNER JOIN %s ul on ul.list_id = li.list_id WHERE li.list_id = $1 AND ul.user_id = $2`,
todoItemsTable, listsItemsTable, usersListTable)
if err := r.db.Select(&items, query, listId, userId); err != nil {
return nil, err
}
return items, nil
}
func (r *TodoItemPostgres) GetById(userId, itemId int) (todo.TodoItem, error) {
var item todo.TodoItem
query := fmt.Sprintf(`SELECT ti.id, ti.title, ti.description, ti.done FROM %s ti INNER JOIN %s li on li.item_id = ti.id
INNER JOIN %s ul on ul.list_id = li.list_id WHERE ti.id = $1 AND ul.user_id = $2`,
todoItemsTable, listsItemsTable, usersListTable)
if err := r.db.Get(&item, query, itemId, userId); err != nil {
return item, err
}
return item, nil
}
func (r *TodoItemPostgres) Delete(userId, itemId int) error {
query := fmt.Sprintf(`DELETE FROM %s ti USING %s li, %s ul
WHERE ti.id = li.item_id AND li.list_id = ul.list_id AND ul.user_id = $1 AND ti.id = $2`,
todoItemsTable, listsItemsTable, usersListTable)
_, err := r.db.Exec(query, userId, itemId)
return err
}
func (r *TodoItemPostgres) Update(userId, itemId int, input todo.UpdateItemInput) 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++
}
if input.Done != nil {
setValues = append(setValues, fmt.Sprintf("done=$%d", argId))
args = append(args, *input.Done)
argId++
}
setQuery := strings.Join(setValues, ", ")
query := fmt.Sprintf(`UPDATE %s ti SET %s FROM %s li, %s ul
WHERE ti.id = li.item_id AND li.list_id = ul.list_id AND ul.user_id = $%d AND ti.id = $%d`,
todoItemsTable, setQuery, listsItemsTable, usersListTable, argId, argId+1)
args = append(args, userId, itemId)
_, err := r.db.Exec(query, args...)
return err
}