Skip to content

Commit

Permalink
Add endpoints, see comments
Browse files Browse the repository at this point in the history
Add endpoint for getting screening by id;
Add endpoint for getting screenings in city for specified movie;
  • Loading branch information
Falokut committed Feb 11, 2024
1 parent 76738d7 commit 8fa44f3
Show file tree
Hide file tree
Showing 12 changed files with 1,409 additions and 188 deletions.
1 change: 0 additions & 1 deletion cinema_db/cinema_db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ services:
- 6432
networks:
- cinema_db_network
- databases_network
environment:
POSTGRESQL_HOST: cinema_db_master
PGBOUNCER_AUTH_TYPE: md5
Expand Down
62 changes: 57 additions & 5 deletions internal/repository/cinemaRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,34 @@ func (r *cinemaRepository) GetMoviesScreeningsInCities(ctx context.Context,

return res, nil
}

func (r *cinemaRepository) GetCityScreenings(ctx context.Context,
cityId, movieId int32, startPeriod, endPeriod time.Time) ([]CityScreening, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "cinemaRepository.GetCityScreenings")
defer span.Finish()
var err error
defer span.SetTag("error", err != nil)

query := fmt.Sprintf(`
SELECT %[1]s.id, %[2]s.name AS screening_type, hall_id, ticket_price,start_time, cinema_id
FROM %[1]s JOIN %[2]s ON screening_type_id=%[2]s.id
JOIN %[3]s ON hall_id = %[3]s.id
JOIN %[4]s ON cinema_id = %[4]s.id
WHERE city_id=$1 AND movie_id=$2 AND start_time>=$3 AND start_time<=$4
ORDER BY start_time;`,
screeningsTableName, screeningTypeTableName, hallsTableName, cinemasTableName)

var screenings []CityScreening
err = r.db.SelectContext(ctx, &screenings, query, cityId, movieId, startPeriod, endPeriod)
if err != nil {
r.logger.Errorf("err: %v query: %s", err.Error(), query)
return []CityScreening{}, err
}

return screenings, nil
}
func (r *cinemaRepository) GetScreenings(ctx context.Context,
cinemaID, movieID int32, startPeriod, endPeriod time.Time) ([]Screening, error) {
cinemaID, movieID int32, startPeriod, endPeriod time.Time) (Screenings, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "cinemaRepository.GetScreenings")
defer span.Finish()
var err error
Expand All @@ -197,18 +223,44 @@ func (r *cinemaRepository) GetScreenings(ctx context.Context,
SELECT %[1]s.id, movie_id, %[2]s.name AS screening_type, hall_id, ticket_price,start_time
FROM %[1]s JOIN %[2]s ON screening_type_id=%[2]s.id
WHERE hall_id=ANY(SELECT id FROM %[3]s WHERE cinema_id=$1) AND movie_id=$2 AND start_time>=$3 AND start_time<=$4
ORDER BY start_time`,
ORDER BY start_time;`,
screeningsTableName, screeningTypeTableName, hallsTableName)

var screenings []Screening
var screenings []screening
err = r.db.SelectContext(ctx, &screenings, query, cinemaID, movieID, startPeriod, endPeriod)
if err != nil {
r.logger.Errorf("err: %v query: %s", err.Error(), query)
return []Screening{}, err
return Screenings{}, err
}

return screenings, nil
return Screenings{Screenings: screenings}, nil
}

func (r *cinemaRepository) GetScreening(ctx context.Context, id int32) (Screening, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "cinemaRepository.GetScreening")
defer span.Finish()
var err error
defer span.SetTag("error", err != nil)

query := fmt.Sprintf(`
SELECT %[2]s.name AS screening_type,hall_id,ticket_price,start_time,cinema_id,movie_id
FROM %[1]s JOIN %[2]s ON screening_type_id=%[2]s.id
JOIN %[3]s ON hall_id = %[3]s.id
WHERE %[1]s.id=$1;`, screeningsTableName, screeningTypeTableName, hallsTableName)

var screening Screening
err = r.db.GetContext(ctx, &screening, query, id)
if errors.Is(err, sql.ErrNoRows) {
return Screening{}, ErrNotFound
}
if err != nil {
r.logger.Errorf("err: %v query: %s", err.Error(), query)
return Screening{}, err
}

return screening, nil
}

func (r *cinemaRepository) GetCinema(ctx context.Context, id int32) (Cinema, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "cinemaRepository.GetCinema")
defer span.Finish()
Expand Down
34 changes: 30 additions & 4 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ type Screening struct {
ScreeningType string `json:"screening_type" db:"screening_type"`
TicketPrice string `json:"ticket_price" db:"ticket_price"`
StartTime time.Time `json:"start_time" db:"start_time"`
ScreeningID int64 `json:"id" db:"id"`
HallID int32 `json:"hall_id" db:"hall_id"`
MovieID int32 `json:"movie_id" db:"movie_id"`
HallId int32 `json:"hall_id" db:"hall_id"`
MovieId int32 `json:"movie_id" db:"movie_id"`
CinemaId int32 `json:"cinema_id" db:"cinema_id"`
}

type Place struct {
Expand All @@ -89,7 +89,30 @@ type Place struct {
GridPosY float32 `json:"grid_pos_y" db:"grid_pos_y"`
}

type CityScreening struct {
ScreeningType string `json:"screening_type" db:"screening_type"`
TicketPrice string `json:"ticket_price" db:"ticket_price"`
StartTime time.Time `json:"start_time" db:"start_time"`
ScreeningId int64 `json:"id" db:"id"`
HallId int32 `json:"hall_id" db:"hall_id"`
CinemaId int32 `json:"cinema_id" db:"cinema_id"`
}

type screening struct {
ScreeningType string `json:"screening_type" db:"screening_type"`
TicketPrice string `json:"ticket_price" db:"ticket_price"`
StartTime time.Time `json:"start_time" db:"start_time"`
ScreeningID int64 `json:"id" db:"id"`
HallID int32 `json:"hall_id" db:"hall_id"`
MovieID int32 `json:"movie_id" db:"movie_id"`
}

type Screenings struct {
Screenings []screening
}

type CinemaRepository interface {
GetScreening(ctx context.Context, id int32) (Screening, error)
// Returns cinemas in the city.
GetCinemasInCity(ctx context.Context, id int32) ([]Cinema, error)

Expand All @@ -99,14 +122,17 @@ type CinemaRepository interface {
// Returns all movies that are in the cinema screenings in a particular cinema.
GetMoviesScreenings(ctx context.Context, cinemaID int32, startPeriod, endPeriod time.Time) ([]MoviesScreenings, error)

// Returns all screenings for a movie in a specific city.
GetCityScreenings(ctx context.Context, cityId, movieId int32, startPeriod, endPeriod time.Time) ([]CityScreening, error)

// Returns all movies that are in the cinema screenings.
GetAllMoviesScreenings(ctx context.Context, startPeriod, endPeriod time.Time) ([]MoviesScreenings, error)

// Returns all movies that are in the cinema screenings in particular cities.
GetMoviesScreeningsInCities(ctx context.Context, citiesIds []int32, startPeriod, endPeriod time.Time) ([]MoviesScreenings, error)

//Returns all screenings for a movie in a specific cinema.
GetScreenings(ctx context.Context, cinemaID, movieID int32, startPeriod, endPeriod time.Time) ([]Screening, error)
GetScreenings(ctx context.Context, cinemaID, movieID int32, startPeriod, endPeriod time.Time) (Screenings, error)

// Returns the configuration of the hall.
GetHallConfiguraion(ctx context.Context, id int32) ([]Place, error)
Expand Down
66 changes: 63 additions & 3 deletions internal/service/repositoryWrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -169,13 +170,13 @@ func (w *cinemaRepositoryWrapper) GetScreenings(ctx context.Context, cinemaID, m
if err != nil {
return nil, w.createErrorResponceWithSpan(span, ErrInternal, err.Error())
}
if len(screenings) == 0 {
if len(screenings.Screenings) == 0 {
return nil, w.createErrorResponceWithSpan(span, ErrNotFound, "")
}

res := &cinema_service.Screenings{}
res.Screenings = make([]*cinema_service.Screening, len(screenings))
for i, screening := range screenings {
res.Screenings = make([]*cinema_service.Screening, len(screenings.Screenings))
for i, screening := range screenings.Screenings {
res.Screenings[i] = &cinema_service.Screening{
ScreeningID: screening.ScreeningID,
ScreeningType: screening.ScreeningType,
Expand All @@ -189,6 +190,65 @@ func (w *cinemaRepositoryWrapper) GetScreenings(ctx context.Context, cinemaID, m
return res, nil
}

func (w *cinemaRepositoryWrapper) GetCityScreenings(ctx context.Context, cityID, movieID int32,
startPeriod, endPeriod time.Time) (*cinema_service.CityScreenings, error) {
span, ctx := opentracing.StartSpanFromContext(ctx,
"cinemaRepositoryWrapper.GetCityScreenings")
defer span.Finish()

screenings, err := w.repo.GetCityScreenings(ctx, cityID, movieID,
startPeriod, endPeriod)
if err != nil {
return nil, w.createErrorResponceWithSpan(span, ErrInternal, err.Error())
}
if len(screenings) == 0 {
return nil, w.createErrorResponceWithSpan(span, ErrNotFound, "")
}

res := &cinema_service.CityScreenings{}
res.Screenings = make([]*cinema_service.CityScreening, len(screenings))
for i, screening := range screenings {
res.Screenings[i] = &cinema_service.CityScreening{
ScreeningId: screening.ScreeningId,
CinemaId: screening.CinemaId,
ScreeningType: screening.ScreeningType,
HallID: screening.HallId,
StartTime: &cinema_service.Timestamp{FormattedTimestamp: screening.StartTime.Format(time.RFC3339)},
TicketPrice: priceFromFloat(screening.TicketPrice),
}
}

return res, nil
}

func (w *cinemaRepositoryWrapper) GetScreening(ctx context.Context, id int32) (*cinema_service.GetScreeningResponse, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "cinemaRepositoryWrapper.GetScreening")
defer span.Finish()

screening, err := w.repo.GetScreening(ctx, id)
if errors.Is(err, repository.ErrNotFound) {
return nil, w.createErrorResponceWithSpan(span, ErrNotFound, fmt.Sprintf("screening with %d id not found", id))
}
if err != nil {
return nil, w.createErrorResponceWithSpan(span, ErrInternal, err.Error())
}

res := &cinema_service.GetScreeningResponse{
ScreeningType: screening.ScreeningType,
CinemaId: screening.CinemaId,
MovieId: screening.MovieId,
HallID: screening.HallId,
StartTime: &cinema_service.Timestamp{FormattedTimestamp: screening.StartTime.Format(time.RFC3339)},
TicketPrice: priceFromFloat(screening.TicketPrice),
}

res.HallConfiguration, err = w.GetHallConfiguraion(ctx, res.HallID)
if err != nil {
return nil, w.createErrorResponceWithSpan(span, ErrInternal, err.Error())
}
return res, nil
}

func (w *cinemaRepositoryWrapper) GetCinema(ctx context.Context, id int32) (*cinema_service.Cinema, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "cinemaRepositoryWrapper.GetCinema")
defer span.Finish()
Expand Down
Loading

0 comments on commit 8fa44f3

Please sign in to comment.