Skip to content

Commit

Permalink
feat: update match costs
Browse files Browse the repository at this point in the history
  • Loading branch information
tructn committed May 22, 2024
1 parent 7937991 commit d6c834b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewDatabase() *gorm.DB {
&domain.Player{},
&domain.Match{},
&domain.Registration{},
&domain.AdditionalCost{},
)
db = dbCtx.Debug()
})
Expand Down
9 changes: 9 additions & 0 deletions domain/additional_cost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain

type AdditionalCost struct {
BaseModel
MatchId uint `json:"matchId"`
Match Match `gorm:"foreignKey:MatchId" json:"match"`
Description string `json:"description"`
Amount float64 `json:"amount"`
}
23 changes: 19 additions & 4 deletions domain/match.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package domain

import "time"
import (
"errors"
"time"
)

type Match struct {
BaseModel
Start time.Time `json:"start"`
End time.Time `json:"end"`
Location string `json:"location"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Location string `json:"location"`
Cost float64 `json:"cost"`
AdditionalCosts []AdditionalCost `json:"additionalCosts"`
}

func (m *Match) UpdateCost(cost float64) error {
if cost <= 0 {
return errors.New("invalid cost")
}

m.Cost = cost

return nil
}
4 changes: 4 additions & 0 deletions handler/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ type (
PlayerName string `json:"playerName"`
IsPaid bool `json:"isPaid"`
}

MatchCostDto struct {
Cost float64 `json:"cost"`
}
)
40 changes: 30 additions & 10 deletions handler/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/truc9/racket/domain"
"github.com/truc9/racket/handler/dto"
"github.com/truc9/racket/params"
"go.uber.org/zap"
"gorm.io/gorm"
)
Expand All @@ -22,17 +23,17 @@ func NewMatchHandler(db *gorm.DB, sugar *zap.SugaredLogger) *matchHandler {
}
}

func (h matchHandler) GetAll(ctx *gin.Context) {
func (h matchHandler) GetAll(c *gin.Context) {
var result []domain.Match
h.db.Order("start DESC").Find(&result)
ctx.JSON(http.StatusOK, result)
c.JSON(http.StatusOK, result)
}

func (h matchHandler) Create(ctx *gin.Context) {
func (h matchHandler) Create(c *gin.Context) {
dto := dto.MatchDto{}
var err error
if err = ctx.BindJSON(&dto); err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
if err = c.BindJSON(&dto); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}

Expand All @@ -47,16 +48,16 @@ func (h matchHandler) Create(ctx *gin.Context) {
h.sugar.Debug(m)

if err = h.db.Create(m).Error; err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
c.AbortWithError(http.StatusBadRequest, err)
return
}

ctx.JSON(http.StatusCreated, m)
c.JSON(http.StatusCreated, m)
}

func (h matchHandler) GetRegistrationsByMatch(ctx *gin.Context) {
func (h matchHandler) GetRegistrationsByMatch(c *gin.Context) {
var result []dto.RegistrationOverviewDto
matchId, _ := ctx.Params.Get("matchId")
matchId, _ := c.Params.Get("matchId")
h.sugar.Infof("getting match id %s", matchId)
h.db.Raw(`
SELECT pl.id AS player_id, CONCAT(pl.first_name, ' ', pl.last_name) AS player_name, re.id AS registration_id, re.match_id, re.is_paid
Expand All @@ -68,5 +69,24 @@ func (h matchHandler) GetRegistrationsByMatch(ctx *gin.Context) {

h.sugar.Info(result)

ctx.JSON(http.StatusOK, result)
c.JSON(http.StatusOK, result)
}

func (h matchHandler) UpdateCost(c *gin.Context) {
matchId := params.Get(c, "matchId")
dto := dto.MatchCostDto{}
if err := c.BindJSON(&dto); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

match := domain.Match{}
if err := h.db.Find(&match, matchId).Error; err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

match.UpdateCost(dto.Cost)
h.db.Save(&match)
c.JSON(http.StatusOK, match)
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ func main() {

// API v1
v1 := router.Group("/api/v1")

// Players API
v1.GET("/players", playerHandler.GetAll)
v1.POST("/players", playerHandler.Create)
v1.DELETE("/players/:playerId", playerHandler.Delete)
v1.PUT("/players/:playerId", playerHandler.Update)

// Matches API
v1.POST("/matches", matchHandler.Create)
v1.GET("/matches", matchHandler.GetAll)
v1.GET("/matches/:matchId/registrations", matchHandler.GetRegistrationsByMatch)
v1.PUT("/matches/:matchId/costs", matchHandler.UpdateCost)

// Registrations API
v1.GET("/registrations", regHandler.GetAll)
v1.POST("/registrations", regHandler.Register)
v1.PUT("/registrations/:registrationId/paid", regHandler.MarkPaid)
Expand Down

0 comments on commit d6c834b

Please sign in to comment.