diff --git a/domain/base.go b/domain/base.go new file mode 100644 index 0000000..18b3971 --- /dev/null +++ b/domain/base.go @@ -0,0 +1,17 @@ +package domain + +import ( + "time" + + "gorm.io/gorm" +) + +type ( + BaseModel struct { + *gorm.Model + ID uint `gorm:"primarykey" json:"id"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` + } +) diff --git a/domain/domain.go b/domain/domain.go deleted file mode 100644 index d0dfd4b..0000000 --- a/domain/domain.go +++ /dev/null @@ -1,37 +0,0 @@ -package domain - -import ( - "time" - - "gorm.io/gorm" -) - -type ( - BaseModel struct { - *gorm.Model - ID uint `gorm:"primarykey" json:"id"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` - DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` - } - Player struct { - BaseModel - FirstName string `json:"firstName"` - LastName string `json:"lastName"` - } - - Registration struct { - BaseModel - PlayerId int64 `json:"playerId"` - MatchId int64 `json:"matchId"` - IsPaid bool `json:"isPaid"` - Comment string `json:"comment"` - } - - Match struct { - BaseModel - Start time.Time `json:"start"` - End time.Time `json:"end"` - Location string `json:"location"` - } -) diff --git a/domain/match.go b/domain/match.go new file mode 100644 index 0000000..2a29e04 --- /dev/null +++ b/domain/match.go @@ -0,0 +1,10 @@ +package domain + +import "time" + +type Match struct { + BaseModel + Start time.Time `json:"start"` + End time.Time `json:"end"` + Location string `json:"location"` +} diff --git a/domain/player.go b/domain/player.go new file mode 100644 index 0000000..9402fc3 --- /dev/null +++ b/domain/player.go @@ -0,0 +1,7 @@ +package domain + +type Player struct { + BaseModel + FirstName string `json:"firstName"` + LastName string `json:"lastName"` +} diff --git a/domain/registration.go b/domain/registration.go new file mode 100644 index 0000000..1e1fcd5 --- /dev/null +++ b/domain/registration.go @@ -0,0 +1,17 @@ +package domain + +type Registration struct { + BaseModel + PlayerId int64 `json:"playerId"` + MatchId int64 `json:"matchId"` + IsPaid bool `json:"isPaid"` + Comment string `json:"comment"` +} + +func (reg *Registration) MarkPaid() { + reg.IsPaid = true +} + +func (reg *Registration) MarkUnpaid() { + reg.IsPaid = false +} diff --git a/handler/match.go b/handler/match.go index 48260d9..aeacc4b 100644 --- a/handler/match.go +++ b/handler/match.go @@ -24,7 +24,7 @@ func NewMatchHandler(db *gorm.DB, sugar *zap.SugaredLogger) *matchHandler { func (h matchHandler) GetAll(ctx *gin.Context) { var result []domain.Match - h.db.Find(&result) + h.db.Order("start DESC").Find(&result) ctx.JSON(http.StatusOK, result) } @@ -62,6 +62,7 @@ func (h matchHandler) GetRegistrationsByMatch(ctx *gin.Context) { 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 FROM "players" pl LEFT JOIN "registrations" re ON pl.id = re.player_id AND re.deleted_at IS NULL AND re.match_id = ? + ORDER BY pl.first_name ASC `, matchId).Scan(&result) h.sugar.Info(result) diff --git a/handler/player.go b/handler/player.go index 1427f9c..9c260ff 100644 --- a/handler/player.go +++ b/handler/player.go @@ -40,6 +40,6 @@ func (h playerHandler) Create(ctx *gin.Context) { func (h playerHandler) GetAll(ctx *gin.Context) { var result []domain.Player - h.db.Find(&result) + h.db.Order("first_name ASC").Find(&result) ctx.JSON(http.StatusOK, result) } diff --git a/handler/registration.go b/handler/registration.go index 7915666..e019b45 100644 --- a/handler/registration.go +++ b/handler/registration.go @@ -55,6 +55,26 @@ func (h registrationHandler) Unregister(ctx *gin.Context) { ctx.JSON(http.StatusOK, id) } +func (h registrationHandler) MarkPaid(ctx *gin.Context) { + registrationId, _ := ctx.Params.Get("registrationId") + entity := domain.Registration{} + h.db.Find(&entity, registrationId) + + entity.MarkPaid() + h.db.Save(&entity) + ctx.JSON(http.StatusOK, entity) +} + +func (h registrationHandler) MarkUnPaid(ctx *gin.Context) { + registrationId, _ := ctx.Params.Get("registrationId") + entity := domain.Registration{} + h.db.Find(&entity, registrationId) + + entity.MarkUnpaid() + h.db.Save(&entity) + ctx.JSON(http.StatusOK, entity) +} + func (h registrationHandler) GetAll(ctx *gin.Context) { var result []dto.RegistrationOverviewDto h.sugar.Info("querying registration report") diff --git a/main.go b/main.go index 6b4226f..7828daf 100644 --- a/main.go +++ b/main.go @@ -42,11 +42,15 @@ func main() { { v1.GET("/players", playerHandler.GetAll) v1.POST("/players", playerHandler.Create) + v1.POST("/matches", matchHandler.Create) v1.GET("/matches", matchHandler.GetAll) v1.GET("/matches/:matchId/registrations", matchHandler.GetRegistrationsByMatch) + v1.GET("/registrations", regHandler.GetAll) v1.POST("/registrations", regHandler.Register) + v1.PUT("/registrations/:registrationId/paid", regHandler.MarkPaid) + v1.PUT("/registrations/:registrationId/unpaid", regHandler.MarkUnPaid) v1.DELETE("/registrations/:registrationId", regHandler.Unregister) }