|
1
|
|
|
package sqlstorage |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"database/sql" |
|
6
|
|
|
"errors" |
|
7
|
|
|
"fmt" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/cronnoss/tk-api/internal/model" |
|
10
|
|
|
"github.com/cronnoss/tk-api/internal/storage/models" |
|
11
|
|
|
_ "github.com/jackc/pgx/stdlib" // nolint: revive |
|
12
|
|
|
"github.com/jmoiron/sqlx" |
|
13
|
|
|
) |
|
14
|
|
|
|
|
15
|
|
|
type Storage struct { |
|
16
|
|
|
dsn string |
|
17
|
|
|
db *sqlx.DB |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
type ShowSQL struct { |
|
21
|
|
|
ID sql.NullInt64 |
|
22
|
|
|
Name sql.NullString |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
var ErrShowNotFound = errors.New("show not found") |
|
26
|
|
|
|
|
27
|
|
|
func ConvertSQLShowToStorageShow(s ShowSQL) (show model.ShowResponse) { |
|
28
|
|
|
if s.ID.Valid { |
|
29
|
|
|
show.ID = s.ID.Int64 |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if s.Name.Valid { |
|
33
|
|
|
show.Name = s.Name.String |
|
34
|
|
|
} |
|
35
|
|
|
return show |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
func New(dsn string) *Storage { |
|
39
|
|
|
return &Storage{dsn: dsn} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
func (s *Storage) Connect(ctx context.Context) error { |
|
43
|
|
|
db, err := sqlx.Open("pgx", s.dsn) |
|
44
|
|
|
if err != nil { |
|
45
|
|
|
return fmt.Errorf("failed to load driver: %w", err) |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
s.db = db |
|
48
|
|
|
err = s.db.PingContext(ctx) |
|
49
|
|
|
if err != nil { |
|
50
|
|
|
return fmt.Errorf("failed to connect to db: %w", err) |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
return nil |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
func (s *Storage) Close(ctx context.Context) error { |
|
56
|
|
|
s.db.Close() |
|
57
|
|
|
ctx.Done() |
|
58
|
|
|
return nil |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
func stringNull(s string) sql.NullString { // nolint: unused |
|
62
|
|
|
if len(s) == 0 { |
|
63
|
|
|
return sql.NullString{} |
|
64
|
|
|
} |
|
65
|
|
|
return sql.NullString{String: s, Valid: true} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// GetShows returns shows. |
|
69
|
|
|
func (s *Storage) GetShows(ctx context.Context) ([]models.Show, error) { |
|
70
|
|
|
var shows []models.Show |
|
71
|
|
|
query := `SELECT id, name FROM shows` |
|
72
|
|
|
if err := s.db.SelectContext(ctx, &shows, query); err != nil { |
|
73
|
|
|
return nil, fmt.Errorf("failed to get shows: %w", err) |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
return shows, nil |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// CreateShows creates shows. |
|
79
|
|
|
func (s *Storage) CreateShows(ctx context.Context, shows []models.Show) ([]models.Show, error) { |
|
80
|
|
|
insertedShows := make([]models.Show, 0, len(shows)) |
|
81
|
|
|
for _, show := range shows { |
|
82
|
|
|
var newShow models.Show |
|
83
|
|
|
err := s.db.GetContext(ctx, &newShow, |
|
84
|
|
|
`INSERT INTO shows (name) VALUES ($1) |
|
85
|
|
|
ON CONFLICT (name) DO UPDATE SET updated_at = now() |
|
86
|
|
|
RETURNING *`, |
|
87
|
|
|
show.Name) |
|
88
|
|
|
if err != nil { |
|
89
|
|
|
return insertedShows, nil // nolint: nilerr |
|
90
|
|
|
} |
|
91
|
|
|
insertedShows = append(insertedShows, newShow) |
|
92
|
|
|
} |
|
93
|
|
|
return insertedShows, nil |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// CreateShow creates a show. |
|
97
|
|
|
func (s *Storage) CreateShow(ctx context.Context, show models.Show) (models.Show, error) { |
|
98
|
|
|
var insertedShow models.Show |
|
99
|
|
|
err := s.db.GetContext(ctx, &insertedShow, |
|
100
|
|
|
`INSERT INTO shows (name) VALUES ($1) |
|
101
|
|
|
ON CONFLICT (name) DO UPDATE SET updated_at = now() |
|
102
|
|
|
RETURNING *`, |
|
103
|
|
|
show.Name) |
|
104
|
|
|
if err != nil { |
|
105
|
|
|
return insertedShow, nil // nolint: nilerr |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return insertedShow, nil |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// GetEvents returns events. |
|
112
|
|
|
func (s *Storage) GetEvents(ctx context.Context) ([]models.Event, error) { |
|
113
|
|
|
var events []models.Event |
|
114
|
|
|
query := `SELECT id, show_id, date FROM events` |
|
115
|
|
|
if err := s.db.SelectContext(ctx, &events, query); err != nil { |
|
116
|
|
|
return nil, fmt.Errorf("failed to get events: %w", err) |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
return events, nil |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// CreateEvents creates events. |
|
122
|
|
|
func (s *Storage) CreateEvents(ctx context.Context, events []models.Event) ([]models.Event, error) { |
|
123
|
|
|
insertedEvents := make([]models.Event, 0, len(events)) |
|
124
|
|
|
for _, event := range events { |
|
125
|
|
|
var newEvent models.Event |
|
126
|
|
|
err := s.db.GetContext(ctx, &newEvent, |
|
127
|
|
|
`INSERT INTO events (show_id, date) VALUES ($1, $2) |
|
128
|
|
|
ON CONFLICT (show_id, date) DO UPDATE SET updated_at = now() |
|
129
|
|
|
RETURNING *`, |
|
130
|
|
|
event.ShowID, event.Date) |
|
131
|
|
|
if err != nil { |
|
132
|
|
|
return insertedEvents, nil // nolint: nilerr |
|
133
|
|
|
} |
|
134
|
|
|
insertedEvents = append(insertedEvents, newEvent) |
|
135
|
|
|
} |
|
136
|
|
|
return insertedEvents, nil |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// CreateEvent creates a event. |
|
140
|
|
|
func (s *Storage) CreateEvent(ctx context.Context, event models.Event) (models.Event, error) { |
|
141
|
|
|
var insertedEvent models.Event |
|
142
|
|
|
err := s.db.GetContext(ctx, &insertedEvent, |
|
143
|
|
|
`INSERT INTO events (id, show_id, date) VALUES ($1, $2, $3) |
|
144
|
|
|
ON CONFLICT (id) DO UPDATE SET updated_at = now() |
|
145
|
|
|
RETURNING *`, |
|
146
|
|
|
event.ID, event.ShowID, event.Date) |
|
147
|
|
|
if err != nil { |
|
148
|
|
|
return insertedEvent, nil // nolint: nilerr |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return insertedEvent, nil |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// GetPlaces returns places. |
|
155
|
|
|
func (s *Storage) GetPlaces(ctx context.Context) ([]models.Place, error) { |
|
156
|
|
|
var places []models.Place |
|
157
|
|
|
query := `SELECT id, x, y, width, height, is_available FROM places` |
|
158
|
|
|
if err := s.db.SelectContext(ctx, &places, query); err != nil { |
|
159
|
|
|
return nil, fmt.Errorf("failed to get places: %w", err) |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
return places, nil |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// CreatePlaces creates places. |
|
165
|
|
|
func (s *Storage) CreatePlaces(ctx context.Context, places []models.Place) ([]models.Place, error) { |
|
166
|
|
|
insertedPlaces := make([]models.Place, 0, len(places)) |
|
167
|
|
|
for _, place := range places { |
|
168
|
|
|
var newPlace models.Place |
|
169
|
|
|
err := s.db.GetContext(ctx, &newPlace, |
|
170
|
|
|
`INSERT INTO places (x, y, width, height, is_available) VALUES ($1, $2, $3, $4, $5) |
|
171
|
|
|
ON CONFLICT (x, y, width, height) DO UPDATE SET updated_at = now() |
|
172
|
|
|
RETURNING *`, |
|
173
|
|
|
place.X, place.Y, place.Width, place.Height, place.IsAvailable) |
|
174
|
|
|
if err != nil { |
|
175
|
|
|
return insertedPlaces, nil // nolint: nilerr |
|
176
|
|
|
} |
|
177
|
|
|
insertedPlaces = append(insertedPlaces, newPlace) |
|
178
|
|
|
} |
|
179
|
|
|
return insertedPlaces, nil |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// CreatePlace creates a place. |
|
183
|
|
|
func (s *Storage) CreatePlace(ctx context.Context, place models.Place) (models.Place, error) { |
|
184
|
|
|
var insertedPlace models.Place |
|
185
|
|
|
err := s.db.GetContext(ctx, &insertedPlace, |
|
186
|
|
|
`INSERT INTO places (id, x, y, width, height, is_available) VALUES ($1, $2, $3, $4, $5, $6) |
|
187
|
|
|
ON CONFLICT (id) DO UPDATE SET updated_at = now() |
|
188
|
|
|
RETURNING *`, |
|
189
|
|
|
place.ID, place.X, place.Y, place.Width, place.Height, place.IsAvailable) |
|
190
|
|
|
if err != nil { |
|
191
|
|
|
return insertedPlace, nil // nolint: nilerr |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return insertedPlace, nil |
|
195
|
|
|
} |
|
196
|
|
|
|