Skip to content

Commit

Permalink
more works
Browse files Browse the repository at this point in the history
  • Loading branch information
tructn committed May 16, 2024
1 parent 4d72c0b commit 1e65b5b
Show file tree
Hide file tree
Showing 5 changed files with 62 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 @@ -18,6 +18,7 @@ var (
func CreateDB() *gorm.DB {
once.Do(func() {
dbCtx, err := gorm.Open(postgres.Open("postgres://postgres:admin@localhost:5432/racket?sslmode=disable"), &gorm.Config{
PrepareStmt: true,
NowFunc: func() time.Time {
return time.Now().UTC()
},
Expand Down
18 changes: 12 additions & 6 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ import (
)

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 {
gorm.Model
Id int64 `gorm:"primaryKey" json:"id"`
BaseModel
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}

Registration struct {
gorm.Model
Id int64 `gorm:"primaryKey" json:"id"`
BaseModel
PlayerId int64 `json:"playerId"`
MatchId int64 `json:"matchId"`
IsPaid bool `json:"isPaid"`
Comment string `json:"comment"`
}

Match struct {
gorm.Model
Id int64 `gorm:"primaryKey" json:"id"`
BaseModel
Start time.Time `json:"start"`
End time.Time `json:"end"`
Location string `json:"location"`
Expand Down
24 changes: 24 additions & 0 deletions handler/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ func (h matchHandler) Create(ctx *gin.Context) {

ctx.JSON(http.StatusCreated, m)
}

func (h matchHandler) GetRegistrationsByMatch(ctx *gin.Context) {
var result []dto.RegistrationOverviewDto
matchId, _ := ctx.Params.Get("matchId")
h.sugar.Infof("getting match id %s", matchId)
h.db.Raw(`
SELECT
m.id AS match_id,
m.location,
m.start,
m.end,
p.id AS player_id,
CONCAT(p.first_name, ' ', p.last_name) AS player_name,
r.is_paid AS is_paid
FROM "matches" m
JOIN "registrations" r on m.id = r.match_id
JOIN "players" p on p.id = r.player_id
WHERE m.id = ?
`, matchId).Scan(&result)

h.sugar.Info(result)

ctx.JSON(http.StatusOK, result)
}
29 changes: 22 additions & 7 deletions handler/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,36 @@ func NewRegHandler(db *gorm.DB, sugar *zap.SugaredLogger) *registrationHandler {
}
}

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

p := &domain.Registration{
PlayerId: dto.PlayerId,
MatchId: dto.MatchId,
IsPaid: false,
var count int64
h.db.Model(&domain.Registration{}).
Where("player_id = ? AND match_id = ?", dto.PlayerId, dto.MatchId).
Count(&count)

if count > 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"message": "Player already registered this match"})
} else {
p := &domain.Registration{
PlayerId: dto.PlayerId,
MatchId: dto.MatchId,
IsPaid: false,
}
h.db.Create(p)
ctx.JSON(http.StatusCreated, p)
}
h.db.Create(p)
ctx.JSON(http.StatusCreated, p)
}

func (h registrationHandler) Unregister(ctx *gin.Context) {
id, _ := ctx.Params.Get("id")
h.db.Model(&domain.Registration{}).Delete(id)
ctx.JSON(http.StatusOK, id)
}

func (h registrationHandler) GetAll(ctx *gin.Context) {
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func main() {
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.Create)
v1.POST("/registrations", regHandler.Register)
v1.DELETE("/registrations/:id", regHandler.Unregister)
}

router.Run()
Expand Down

0 comments on commit 1e65b5b

Please sign in to comment.