Skip to content

Commit

Permalink
fix: showtime seat query
Browse files Browse the repository at this point in the history
  • Loading branch information
labasubagia committed Dec 5, 2024
1 parent 346b224 commit 3b9fe7c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 27 deletions.
2 changes: 1 addition & 1 deletion model_movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Movie struct {
func (m *Movie) ValidateDuration(startAt, endAt time.Time) error {
duration := endAt.Sub(startAt).Minutes()
if duration < float64(m.Duration) {
return NewErr(ErrInput, nil, "showtime duration less than movie duration")
return NewErr(ErrInput, nil, "showtime duration less than movie duration, minimum %d minutes", m.Duration)
}
return nil
}
Expand Down
102 changes: 76 additions & 26 deletions repository_showtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,32 @@ func (r *ShowtimeRepository) FindOne(ctx context.Context, filter ShowtimeFilter)
func (r *ShowtimeRepository) Find(ctx context.Context, filter ShowtimeFilter) ([]Showtime, error) {
filterSQL, filterArgs := r.getFilterSQL(ctx, filter)

// TODO: fix this query
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,
Expand All @@ -106,20 +129,22 @@ func (r *ShowtimeRepository) Find(ctx context.Context, filter ShowtimeFilter) ([
s.updated_at,
m.title as movie_title,
r.name as room_name,
count(st.*) as total_seat,
count(st.*)-count(rv.*) as available_seat
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
left join seats st on st.room_id = r.id
left join reservation_items rvi on rvi.showtime_id = s.id and rvi.seat_id = st.id
left join reservations rv on rv.id = rvi.reservation_id and rv.status != 'cancelled'
where s.id in (%s)
group by s.id, m.title , r."name"
order BY s.start_at asc, m.title asc
order by
s.start_at asc,
m.title asc
`,
filterSQL,
)
Expand Down Expand Up @@ -178,9 +203,32 @@ func (r *ShowtimeRepository) Pagination(ctx context.Context, filter ShowtimeFilt
p.CurrentPage = page.Page
}

// TODO: fix this query
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,
Expand All @@ -192,20 +240,22 @@ func (r *ShowtimeRepository) Pagination(ctx context.Context, filter ShowtimeFilt
s.updated_at,
m.title as movie_title,
r.name as room_name,
count(st.*) as total_seat,
count(st.*)-count(rv.*) as available_seat
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
left join seats st on st.room_id = r.id
left join reservation_items rvi on rvi.showtime_id = s.id and rvi.seat_id = st.id
left join reservations rv on rv.id = rvi.reservation_id and rv.status != 'cancelled'
where s.id in (%s)
group by s.id, m.title , r."name"
order BY s.start_at asc, m.title asc
order by
s.start_at asc,
m.title asc
limit @page_size offset (@page - 1) * @page_size;
`,
filterSQL,
Expand Down Expand Up @@ -301,16 +351,16 @@ func (r *ShowtimeRepository) getFilterSQL(_ context.Context, filter ShowtimeFilt
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
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,
Expand Down

0 comments on commit 3b9fe7c

Please sign in to comment.