Skip to content

Commit

Permalink
add payment status API
Browse files Browse the repository at this point in the history
  • Loading branch information
truc9 committed May 18, 2024
1 parent efce030 commit f8ac190
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 39 deletions.
17 changes: 17 additions & 0 deletions domain/base.go
Original file line number Diff line number Diff line change
@@ -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"`
}
)
37 changes: 0 additions & 37 deletions domain/domain.go

This file was deleted.

10 changes: 10 additions & 0 deletions domain/match.go
Original file line number Diff line number Diff line change
@@ -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"`
}
7 changes: 7 additions & 0 deletions domain/player.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package domain

type Player struct {
BaseModel
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
17 changes: 17 additions & 0 deletions domain/registration.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion handler/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion handler/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
20 changes: 20 additions & 0 deletions handler/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit f8ac190

Please sign in to comment.