Skip to content

Commit

Permalink
Draw method filters fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gapidobri committed Aug 14, 2024
1 parent 5f30b84 commit 6eeaf75
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 24 deletions.
53 changes: 53 additions & 0 deletions api/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ basePath: /
consumes:
- application/json
definitions:
DrawMethod:
properties:
data:
type: string
draw_method_id:
type: string
method:
enum:
- first_n
- chance
type: string
name:
type: string
required:
- draw_method_id
- name
- method
- data
type: object
ErrorResponse:
properties:
code:
Expand Down Expand Up @@ -56,6 +75,10 @@ definitions:
- id
- name
type: object
GetDrawMethodsResponse:
items:
$ref: '#/definitions/DrawMethod'
type: array
GetGameResponse:
$ref: '#/definitions/Game'
GetGamesResponse:
Expand Down Expand Up @@ -214,6 +237,36 @@ info:
title: Prizer Admin API
version: "1.0"
paths:
/draw-methods:
get:
operationId: getDrawMethods
parameters:
- in: query
name: gameId
type: string
- in: query
name: participationMethodId
type: string
responses:
"200":
description: GetDrawMethodsResponse
schema:
$ref: '#/definitions/GetDrawMethodsResponse'
"400":
description: ErrorResponse
schema:
$ref: '#/definitions/ErrorResponse'
"403":
description: ErrorResponse
schema:
$ref: '#/definitions/ErrorResponse'
"500":
description: ErrorResponse
schema:
$ref: '#/definitions/ErrorResponse'
tags:
- admin
- drawMethods
/games:
get:
operationId: getGames
Expand Down
23 changes: 23 additions & 0 deletions api/public.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ basePath: /
consumes:
- application/json
definitions:
DrawMethod:
properties:
data:
type: string
draw_method_id:
type: string
method:
enum:
- first_n
- chance
type: string
name:
type: string
required:
- draw_method_id
- name
- method
- data
type: object
ErrorResponse:
properties:
code:
Expand Down Expand Up @@ -56,6 +75,10 @@ definitions:
- id
- name
type: object
GetDrawMethodsResponse:
items:
$ref: '#/definitions/DrawMethod'
type: array
GetGameResponse:
$ref: '#/definitions/Game'
GetGamesResponse:
Expand Down
44 changes: 44 additions & 0 deletions internal/api/admin/draw_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package admin

import (
"github.com/gapidobri/prizer/internal/pkg/models/api"
"github.com/gin-gonic/gin"
"net/http"
)

func (s *Server) drawMethodRoutes() {
group := s.engine.Group("/draw-methods")

// swagger:route GET /draw-methods admin drawMethods getDrawMethods
//
// parameters:
// + name: gameId
// in: query
// type: string
// + name: participationMethodId
// in: query
// type: string
//
// responses:
// 200: GetDrawMethodsResponse
// 400: ErrorResponse
// 403: ErrorResponse
// 500: ErrorResponse
//
group.GET("", func(c *gin.Context) {
var filter api.GetDrawMethodsFilter
err := c.ShouldBindQuery(&filter)
if err != nil {
_ = c.Error(err)
return
}

drawMethods, err := s.drawMethodService.GetDrawMethods(c.Request.Context(), filter)
if err != nil {
_ = c.Error(err)
return
}

c.JSON(http.StatusOK, drawMethods)
})
}
4 changes: 4 additions & 0 deletions internal/api/admin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Server struct {
prizeService *service.PrizeService
wonPrizeService *service.WonPrizeService
participationMethodService *service.ParticipationMethodService
drawMethodService *service.DrawMethodService
}

func NewServer(
Expand All @@ -24,6 +25,7 @@ func NewServer(
prizeService *service.PrizeService,
wonPrizeService *service.WonPrizeService,
participationMethodService *service.ParticipationMethodService,
drawMethodService *service.DrawMethodService,
) *Server {
return &Server{
engine: api.NewServer(db),
Expand All @@ -32,6 +34,7 @@ func NewServer(
prizeService: prizeService,
wonPrizeService: wonPrizeService,
participationMethodService: participationMethodService,
drawMethodService: drawMethodService,
}
}

Expand All @@ -41,6 +44,7 @@ func (s *Server) Run(address string) {
s.prizeRoutes()
s.wonPrizeRoutes()
s.participationMethodRoutes()
s.drawMethodRoutes()

log.Infof("Admin API listening on %s", address)

Expand Down
2 changes: 2 additions & 0 deletions internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func ErrorHandler(c *gin.Context) {
errString += "is missing"
case "email":
errString += "is not a valid email address"
case "uuid":
errString += "is not a valid UUID"
default:
errString += fmt.Sprintf("failed on '%s'", err.Tag())
}
Expand Down
2 changes: 2 additions & 0 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func Run() {
prizeService := service.NewPrizeService(prizeRepository)
wonPrizeService := service.NewWonPrizeService(wonPrizeRepository)
participationMethodService := service.NewParticipationMethodService(participationMethodRepository)
drawMethodService := service.NewDrawMethodService(drawMethodRepository)

// APIs
publicApi := public.NewServer(db, gameService)
Expand All @@ -86,6 +87,7 @@ func Run() {
prizeService,
wonPrizeService,
participationMethodService,
drawMethodService,
)

go publicApi.Run(cfg.Http.Public.Address)
Expand Down
18 changes: 10 additions & 8 deletions internal/database/draw_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type DrawMethodRepository interface {
GetDrawMethods(ctx context.Context, gameId string, filter database.GetDrawMethodsFilter) ([]database.DrawMethod, error)
GetDrawMethods(ctx context.Context, filter database.GetDrawMethodsFilter) ([]database.DrawMethod, error)
}

type drawMethodRepository struct {
Expand All @@ -21,16 +21,18 @@ func NewDrawMethodRepository(db *sqlx.DB) DrawMethodRepository {
}
}

func (d *drawMethodRepository) GetDrawMethods(ctx context.Context, gameId string, filter database.GetDrawMethodsFilter) ([]database.DrawMethod, error) {
func (d *drawMethodRepository) GetDrawMethods(ctx context.Context, filter database.GetDrawMethodsFilter) ([]database.DrawMethod, error) {
query := sq.
Select("DISTINCT ON (draw_method_id) dm.*").
From("participation_methods").
InnerJoin("participation_methods_draw_methods USING (participation_method_id)").
InnerJoin("draw_methods dm USING (draw_method_id)").
Where("game_id = ?", gameId)
Select("dm.*").
From("draw_methods dm")

if filter.GameId != nil {
query = query.Where(sq.Eq{"dm.game_id": filter.GameId})
}
if filter.ParticipationMethodId != nil {
query = query.Where("participation_method_id = ?", filter.ParticipationMethodId)
query = query.
InnerJoin("participation_methods_draw_methods USING (draw_method_id)").
Where(sq.Eq{"participation_method_id": filter.ParticipationMethodId})
}

sql, args := query.
Expand Down
49 changes: 49 additions & 0 deletions internal/pkg/models/api/draw_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package api

import (
dbModels "github.com/gapidobri/prizer/internal/pkg/models/database"
"github.com/gapidobri/prizer/internal/pkg/models/enums"
)

// swagger:model DrawMethod
type DrawMethod struct {
// required: true
Id string `json:"draw_method_id"`

// required: true
GameId string `json:"game_id"`

// required: true
Name string `json:"name"`

// required: true
Method enums.DrawMethod `json:"method"`

// required: true
Data string `json:"data"`
}

func DrawMethodFromDB(drawMethod dbModels.DrawMethod) DrawMethod {
return DrawMethod{
Id: drawMethod.Id,
GameId: drawMethod.GameId,
Name: drawMethod.Name,
Method: drawMethod.Method,
Data: drawMethod.Data,
}
}

type GetDrawMethodsFilter struct {
GameId *string `form:"gameId" binding:"omitnil,uuid"`
ParticipationId *string `form:"participationId" binding:"omitnil,uuid"`
}

func (f GetDrawMethodsFilter) ToDB() dbModels.GetDrawMethodsFilter {
return dbModels.GetDrawMethodsFilter{
GameId: f.GameId,
ParticipationMethodId: f.ParticipationId,
}
}

// swagger:model GetDrawMethodsResponse
type GetDrawMethodsResponse []DrawMethod
6 changes: 3 additions & 3 deletions internal/pkg/models/api/won_prize.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func WonPrizeFromDB(wonPrize dbModels.WonPrize) WonPrize {
}

type GetWonPrizesFilter struct {
GameId *string `form:"gameId"`
UserId *string `form:"userId"`
PrizeId *string `form:"prizeId"`
GameId *string `form:"gameId" binding:"omitnil,uuid"`
UserId *string `form:"userId" binding:"omitnil,uuid"`
PrizeId *string `form:"prizeId" binding:"omitnil,uuid"`
}

func (f GetWonPrizesFilter) ToDB() dbModels.GetWonPrizesFilter {
Expand Down
17 changes: 7 additions & 10 deletions internal/pkg/models/database/draw_method.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package database

type DrawMethodEnum string

const (
DrawMethodFirstN DrawMethodEnum = "first_n"
DrawMethodChance DrawMethodEnum = "chance"
)
import "github.com/gapidobri/prizer/internal/pkg/models/enums"

type DrawMethod struct {
Id string `db:"draw_method_id"`
Name string `db:"name"`
Method DrawMethodEnum `db:"method"`
Data string `db:"data"`
Id string `db:"draw_method_id"`
GameId string `db:"game_id"`
Name string `db:"name"`
Method enums.DrawMethod `db:"method"`
Data string `db:"data"`
}

type DrawMethodChanceData struct {
Expand All @@ -23,5 +19,6 @@ type DrawMethodFirstNData struct {
}

type GetDrawMethodsFilter struct {
GameId *string
ParticipationMethodId *string
}
9 changes: 9 additions & 0 deletions internal/pkg/models/enums/draw_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package enums

// swagger:enum DrawMethod
type DrawMethod string

const (
DrawMethodFirstN DrawMethod = "first_n"
DrawMethodChance DrawMethod = "chance"
)
32 changes: 32 additions & 0 deletions internal/service/draw_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package service

import (
"context"
"github.com/gapidobri/prizer/internal/database"
"github.com/gapidobri/prizer/internal/pkg/models/api"
dbModels "github.com/gapidobri/prizer/internal/pkg/models/database"
"github.com/samber/lo"
)

type DrawMethodService struct {
drawMethodRepository database.DrawMethodRepository
}

func NewDrawMethodService(drawMethodRepository database.DrawMethodRepository) *DrawMethodService {
return &DrawMethodService{
drawMethodRepository: drawMethodRepository,
}
}

func (s *DrawMethodService) GetDrawMethods(ctx context.Context, filter api.GetDrawMethodsFilter) (api.GetDrawMethodsResponse, error) {
drawMethods, err := s.drawMethodRepository.GetDrawMethods(ctx, filter.ToDB())
if err != nil {
return nil, err
}

apiDrawMethods := lo.Map(drawMethods, func(drawMethod dbModels.DrawMethod, _ int) api.DrawMethod {
return api.DrawMethodFromDB(drawMethod)
})

return apiDrawMethods, nil
}
Loading

0 comments on commit 6eeaf75

Please sign in to comment.