-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_showtime.go
415 lines (393 loc) · 8.39 KB
/
repository_showtime.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
)
func NewShowtimeRepository(tx pgx.Tx) *ShowtimeRepository {
return &ShowtimeRepository{
tx: tx,
}
}
type ShowtimeRepository struct {
tx pgx.Tx
}
func (r *ShowtimeRepository) Create(ctx context.Context, showtime *Showtime) (int64, error) {
sql := `
insert into public.showtimes (movie_id, room_id, start_at, end_at, price)
values (@movie_id, @room_id, @start_at, @end_at, @price)
returning id
`
var ID int64
err := r.tx.QueryRow(
ctx,
sql,
pgx.NamedArgs{
"movie_id": showtime.MovieID,
"room_id": showtime.RoomID,
"start_at": showtime.StartAt,
"end_at": showtime.EndAt,
"price": showtime.Price,
},
).Scan(&ID)
if err != nil {
return 0, NewSQLErr(err)
}
return ID, nil
}
func (r *ShowtimeRepository) UpdateByID(ctx context.Context, ID int64, input ShowtimeInput) error {
sql := `
update
public.showtimes
set
updated_at = now(),
movie_id = @movie_id,
room_id = @room_id,
start_at = @start_at,
end_at = @end_at,
price = @price
where
id = @id
`
_, err := r.tx.Exec(ctx, sql, pgx.NamedArgs{
"id": ID,
"movie_id": input.MovieID,
"room_id": input.RoomID,
"start_at": input.StartAt,
"end_at": input.EndAt,
"price": input.Price,
})
if err != nil {
return NewSQLErr(err)
}
return nil
}
func (r *ShowtimeRepository) DeleteByID(ctx context.Context, ID int64) error {
sql := `delete from showtimes where id = @id`
_, err := r.tx.Exec(ctx, sql, pgx.NamedArgs{"id": ID})
if err != nil {
return NewSQLErr(err)
}
return nil
}
func (r *ShowtimeRepository) FindOne(ctx context.Context, filter ShowtimeFilter) (*Showtime, error) {
showtimes, err := r.Find(ctx, filter)
if err != nil {
return nil, err
}
if len(showtimes) == 0 {
return nil, NewErr(ErrNotFound, nil, "showtime not found")
}
return &showtimes[0], nil
}
func (r *ShowtimeRepository) Find(ctx context.Context, filter ShowtimeFilter) ([]Showtime, error) {
filterSQL, filterArgs := r.getFilterSQL(ctx, filter)
sql := fmt.Sprintf(
`
with reserved_count as (
select
ri.showtime_id,
count(ri.*) as total
from
reservation_items ri
join reservations r on
ri.reservation_id = r.id
where
r.status = 'paid'::reservation_status
or (r.status = 'unpaid'::reservation_status
and r.created_at > now() - interval '30 minutes')
group by
ri.showtime_id
),
seat_count as (
select
room_id,
count(*) as total
from
seats
group by
room_id
)
select
s.id,
s.movie_id,
s.room_id,
s.start_at,
s.end_at,
s.price,
s.created_at,
s.updated_at,
m.title as movie_title,
r.name as room_name,
coalesce(sc.total, 0) as total_seat,
coalesce(sc.total, 0) - coalesce(rc.total, 0) as available_seat
from
showtimes s
left join reserved_count rc on
s.id = rc.showtime_id
left join seat_count sc on
sc.room_id = s.room_id
join movies m on
m.id = s.movie_id
join rooms r on
r.id = s.room_id
where s.id in (%s)
order by
s.start_at asc,
m.title asc
`,
filterSQL,
)
rows, err := r.tx.Query(ctx, sql, filterArgs)
if err != nil {
return nil, NewSQLErr(err)
}
defer rows.Close()
var showtimes []Showtime
for rows.Next() {
var showtime Showtime
err := rows.Scan(
&showtime.ID,
&showtime.MovieID,
&showtime.RoomID,
&showtime.StartAt,
&showtime.EndAt,
&showtime.Price,
&showtime.CreatedAt,
&showtime.UpdatedAt,
&showtime.MovieTitle,
&showtime.RoomName,
&showtime.TotalSeat,
&showtime.AvailableSeat,
)
if err != nil {
return nil, NewSQLErr(err)
}
showtimes = append(showtimes, showtime)
}
err = rows.Err()
if err != nil {
return nil, NewSQLErr(err)
}
return showtimes, nil
}
func (r *ShowtimeRepository) Pagination(ctx context.Context, filter ShowtimeFilter, page PaginateInput) (*Paginate[Showtime], error) {
filterSQL, filterArgs := r.getFilterSQL(ctx, filter)
var totalItems int64
sql := fmt.Sprintf(`select count(*) from (%s)`, filterSQL)
err := r.tx.QueryRow(ctx, sql, filterArgs).Scan(&totalItems)
if err != nil {
return nil, NewSQLErr(err)
}
p := NewPaginate([]Showtime{}, totalItems, page.Page, page.Size)
if totalItems == 0 {
return p, nil
}
if page.Page > p.TotalPage {
page.Page = p.TotalPage
p.CurrentPage = page.Page
}
sql = fmt.Sprintf(
`
with reserved_count as (
select
ri.showtime_id,
count(ri.*) as total
from
reservation_items ri
join reservations r on
ri.reservation_id = r.id
where
r.status = 'paid'::reservation_status
or (r.status = 'unpaid'::reservation_status
and r.created_at > now() - interval '30 minutes')
group by
ri.showtime_id
),
seat_count as (
select
room_id,
count(*) as total
from
seats
group by
room_id
)
select
s.id,
s.movie_id,
s.room_id,
s.start_at,
s.end_at,
s.price,
s.created_at,
s.updated_at,
m.title as movie_title,
r.name as room_name,
coalesce(sc.total, 0) as total_seat,
coalesce(sc.total, 0) - coalesce(rc.total, 0) as available_seat
from
showtimes s
left join reserved_count rc on
s.id = rc.showtime_id
left join seat_count sc on
sc.room_id = s.room_id
join movies m on
m.id = s.movie_id
join rooms r on
r.id = s.room_id
where s.id in (%s)
order by
s.start_at asc,
m.title asc
limit @page_size offset (@page - 1) * @page_size;
`,
filterSQL,
)
rows, err := r.tx.Query(ctx, sql, mergeNamedArgs(
filterArgs,
pgx.NamedArgs{
"page": page.Page,
"page_size": page.Size,
}),
)
if err != nil {
return nil, NewSQLErr(err)
}
defer rows.Close()
var showtimes []Showtime
for rows.Next() {
var showtime Showtime
err := rows.Scan(
&showtime.ID,
&showtime.MovieID,
&showtime.RoomID,
&showtime.StartAt,
&showtime.EndAt,
&showtime.Price,
&showtime.CreatedAt,
&showtime.UpdatedAt,
&showtime.MovieTitle,
&showtime.RoomName,
&showtime.TotalSeat,
&showtime.AvailableSeat,
)
if err != nil {
return nil, NewSQLErr(err)
}
showtimes = append(showtimes, showtime)
}
err = rows.Err()
if err != nil {
return nil, NewSQLErr(err)
}
p.Items = showtimes
return p, nil
}
func (r *ShowtimeRepository) getFilterSQL(_ context.Context, filter ShowtimeFilter) (sql string, args pgx.NamedArgs) {
sql = `
select _s.id
from public.showtimes _s
where
case
when array_length(@_ids::int[], 1) > 0 then
_s.id = any(@_ids)
else
true
end
and
case
when array_length(@_movie_ids::int[], 1) > 0 then
_s.movie_id = any(@_movie_ids)
else
true
end
and
case
when array_length(@_room_ids::int[], 1) > 0 then
_s.room_id = any(@_room_ids)
else
true
end
and
case
when @_after::timestamptz is not null then
@_after <= _s.start_at
else
true
end
`
args = pgx.NamedArgs{
"_ids": filter.IDs,
"_movie_ids": filter.MovieIDs,
"_room_ids": filter.RoomIDs,
}
if !filter.After.IsZero() {
args["_after"] = filter.After
}
return sql, args
}
// showtime seats
func (r *ShowtimeRepository) GetShowtimeSeats(ctx context.Context, showtimeID int64) ([]Seat, error) {
sql := `
with reserved_seat as (
select
ri.*
from
reservation_items ri
join reservations r on
ri.reservation_id = r.id
where
r.status = 'paid'::reservation_status
or (r.status = 'unpaid'::reservation_status
and r.created_at > now() - interval '30 minutes')
)
select
st.id as seat_id,
s.room_id,
st.additional_price,
st."name",
st.created_at,
st.updated_at,
rt.id is null as is_available
from
showtimes s
join seats st on
s.room_id = st.room_id
left join reserved_seat rt on
rt.seat_id = st.id
and rt.showtime_id = s.id
where s.id = @showtime_id
order by
s.id,
st.room_id,
st."name"
`
rows, err := r.tx.Query(ctx, sql, pgx.NamedArgs{"showtime_id": showtimeID})
if err != nil {
return nil, NewSQLErr(err)
}
defer rows.Close()
var seats []Seat
for rows.Next() {
var seat Seat
err := rows.Scan(
&seat.ID,
&seat.RoomID,
&seat.AdditionalPrice,
&seat.Name,
&seat.CreatedAt,
&seat.UpdatedAt,
&seat.IsAvailable,
)
if err != nil {
return nil, NewSQLErr(err)
}
seats = append(seats, seat)
}
err = rows.Err()
if err != nil {
return nil, NewSQLErr(err)
}
return seats, nil
}