Skip to content

Commit

Permalink
use di container
Browse files Browse the repository at this point in the history
  • Loading branch information
tructn committed May 29, 2024
1 parent fbcc9e1 commit e51442e
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 80 deletions.
19 changes: 19 additions & 0 deletions di/di.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package di

import (
"github.com/truc9/racket/db"
"github.com/truc9/racket/handler"
"github.com/truc9/racket/logger"
"go.uber.org/dig"
)

func Register() *dig.Container {
c := dig.New()
c.Provide(db.NewDatabase)
c.Provide(logger.NewLogger)
c.Provide(handler.NewMatchHandler)
c.Provide(handler.NewPlayerHandler)
c.Provide(handler.NewRegHandler)
c.Provide(handler.NewSportCenterHandler)
return c
}
14 changes: 14 additions & 0 deletions domain/sport_center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package domain

type SportCenter struct {
BaseModel
Name string `json:"name"`
Location string `json:"location"`
}

func NewSportCenter(name, location string) *SportCenter {
return &SportCenter{
Name: name,
Location: location,
}
}
9 changes: 9 additions & 0 deletions dto/sport_center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dto

type (
SportCenterDto struct {
ID uint `json:"id"`
Name string `json:"name"`
Location string `json:"location"`
}
)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
github.com/tdewolff/parse/v2 v2.7.14 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
go.uber.org/dig v1.17.1 h1:Tga8Lz8PcYNsWsyHMZ1Vm0OQOUaJNDyvPImgbAu9YSc=
go.uber.org/dig v1.17.1/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
32 changes: 16 additions & 16 deletions handler/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,41 @@ import (
"gorm.io/gorm"
)

type matchHandler struct {
db *gorm.DB
sugar *zap.SugaredLogger
type MatchHandler struct {
db *gorm.DB
logger *zap.SugaredLogger
}

func NewMatchHandler(db *gorm.DB, sugar *zap.SugaredLogger) *matchHandler {
return &matchHandler{
db: db,
sugar: sugar,
func NewMatchHandler(db *gorm.DB, logger *zap.SugaredLogger) *MatchHandler {
return &MatchHandler{
db: db,
logger: logger,
}
}

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

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

h.sugar.Debug(dto)
h.logger.Debug(dto)

m := &domain.Match{
Start: dto.Start,
End: dto.End,
Location: dto.Location,
}

h.sugar.Debug(m)
h.logger.Debug(m)

if err = h.db.Create(m).Error; err != nil {
c.AbortWithError(http.StatusBadRequest, err)
Expand All @@ -55,10 +55,10 @@ func (h *matchHandler) Create(c *gin.Context) {
c.JSON(http.StatusCreated, m)
}

func (h *matchHandler) GetRegistrationsByMatch(c *gin.Context) {
func (h *MatchHandler) GetRegistrationsByMatch(c *gin.Context) {
var result []dto.RegistrationOverviewDto
matchId, _ := c.Params.Get("matchId")
h.sugar.Infof("getting match id %s", matchId)
h.logger.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
FROM "players" pl
Expand All @@ -67,12 +67,12 @@ func (h *matchHandler) GetRegistrationsByMatch(c *gin.Context) {
ORDER BY pl.first_name ASC
`, matchId).Scan(&result)

h.sugar.Info(result)
h.logger.Info(result)

c.JSON(http.StatusOK, result)
}

func (h *matchHandler) UpdateCost(c *gin.Context) {
func (h *MatchHandler) UpdateCost(c *gin.Context) {
matchId := params.Get(c, "matchId")
dto := dto.MatchCostDto{}
if err := c.BindJSON(&dto); err != nil {
Expand All @@ -91,7 +91,7 @@ func (h *matchHandler) UpdateCost(c *gin.Context) {
c.JSON(http.StatusOK, match)
}

func (h *matchHandler) CreateAdditionalCost(c *gin.Context) {
func (h *MatchHandler) CreateAdditionalCost(c *gin.Context) {
matchId := params.Get(c, "matchId")
dto := dto.AdditionalCostDto{}
if err := c.BindJSON(&dto); err != nil {
Expand Down
22 changes: 11 additions & 11 deletions handler/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
"gorm.io/gorm"
)

type playerHandler struct {
db *gorm.DB
sugar *zap.SugaredLogger
type PlayerHandler struct {
db *gorm.DB
logger *zap.SugaredLogger
}

func NewPlayerHandler(db *gorm.DB, sugar *zap.SugaredLogger) *playerHandler {
return &playerHandler{
db: db,
sugar: sugar,
func NewPlayerHandler(db *gorm.DB, logger *zap.SugaredLogger) *PlayerHandler {
return &PlayerHandler{
db: db,
logger: logger,
}
}

func (h playerHandler) Create(c *gin.Context) {
func (h PlayerHandler) Create(c *gin.Context) {
dto := dto.PlayerDto{}
var err error
if err = c.BindJSON(&dto); err != nil {
Expand All @@ -39,13 +39,13 @@ func (h playerHandler) Create(c *gin.Context) {
c.JSON(http.StatusCreated, p)
}

func (h playerHandler) GetAll(c *gin.Context) {
func (h PlayerHandler) GetAll(c *gin.Context) {
var result []domain.Player
h.db.Order("first_name ASC").Find(&result)
c.JSON(http.StatusOK, result)
}

func (h playerHandler) Update(c *gin.Context) {
func (h PlayerHandler) Update(c *gin.Context) {
var model dto.PlayerDto
id := params.Get(c, "playerId")
if err := c.BindJSON(&model); err != nil {
Expand All @@ -61,7 +61,7 @@ func (h playerHandler) Update(c *gin.Context) {
c.JSON(http.StatusOK, p)
}

func (h playerHandler) Delete(c *gin.Context) {
func (h PlayerHandler) Delete(c *gin.Context) {
id := params.Get(c, "playerId")
h.db.Delete(&domain.Player{}, id)
c.Status(http.StatusOK)
Expand Down
26 changes: 13 additions & 13 deletions handler/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (
"gorm.io/gorm"
)

type registrationHandler struct {
db *gorm.DB
sugar *zap.SugaredLogger
type RegistrationHandler struct {
db *gorm.DB
logger *zap.SugaredLogger
}

func NewRegHandler(db *gorm.DB, sugar *zap.SugaredLogger) *registrationHandler {
return &registrationHandler{
db: db,
sugar: sugar,
func NewRegHandler(db *gorm.DB, logger *zap.SugaredLogger) *RegistrationHandler {
return &RegistrationHandler{
db: db,
logger: logger,
}
}

func (h registrationHandler) Register(ctx *gin.Context) {
func (h RegistrationHandler) Register(ctx *gin.Context) {
dto := dto.RegistrationDto{}
var err error
if err = ctx.BindJSON(&dto); err != nil {
Expand All @@ -49,13 +49,13 @@ func (h registrationHandler) Register(ctx *gin.Context) {
}
}

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

func (h registrationHandler) MarkPaid(ctx *gin.Context) {
func (h RegistrationHandler) MarkPaid(ctx *gin.Context) {
registrationId, _ := ctx.Params.Get("registrationId")
entity := domain.Registration{}
h.db.Find(&entity, registrationId)
Expand All @@ -65,7 +65,7 @@ func (h registrationHandler) MarkPaid(ctx *gin.Context) {
ctx.JSON(http.StatusOK, entity)
}

func (h registrationHandler) MarkUnPaid(ctx *gin.Context) {
func (h RegistrationHandler) MarkUnPaid(ctx *gin.Context) {
registrationId, _ := ctx.Params.Get("registrationId")
entity := domain.Registration{}
h.db.Find(&entity, registrationId)
Expand All @@ -75,9 +75,9 @@ func (h registrationHandler) MarkUnPaid(ctx *gin.Context) {
ctx.JSON(http.StatusOK, entity)
}

func (h registrationHandler) GetAll(ctx *gin.Context) {
func (h RegistrationHandler) GetAll(ctx *gin.Context) {
var result []dto.RegistrationOverviewDto
h.sugar.Info("querying registration report")
h.logger.Info("querying registration report")
h.db.Raw(`
SELECT
r.id as registration_id,
Expand Down
70 changes: 70 additions & 0 deletions handler/sport_center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package handler

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/truc9/racket/domain"
"github.com/truc9/racket/dto"
"github.com/truc9/racket/params"
"go.uber.org/zap"
"gorm.io/gorm"
)

type SportCenterHandler struct {
db *gorm.DB
logger *zap.SugaredLogger
}

func NewSportCenterHandler(db *gorm.DB, logger *zap.SugaredLogger) *SportCenterHandler {
return &SportCenterHandler{
db: db,
logger: logger,
}
}

func (h *SportCenterHandler) GetAll(c *gin.Context) {
var result []dto.SportCenterDto
if err := h.db.Find(&result); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.JSON(http.StatusOK, result)
}

func (h *SportCenterHandler) Create(c *gin.Context) {
dto := dto.SportCenterDto{}
if err := c.BindJSON(&dto); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

h.db.Create(&domain.SportCenter{
Name: dto.Name,
Location: dto.Location,
})

c.Status(http.StatusOK)
}

func (h *SportCenterHandler) Update(c *gin.Context) {
dto := dto.SportCenterDto{}
id := params.Get(c, "sportCenterId")

if err := c.BindJSON(&dto); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

entity := domain.SportCenter{}
if err := h.db.Find(&entity, id).Error; err != nil {
c.AbortWithStatus(http.StatusNotFound)
return
}

entity.Name = dto.Name
entity.Location = dto.Location

h.db.Save(entity)
c.Status(http.StatusOK)
}
10 changes: 10 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package logger

import "go.uber.org/zap"

func NewLogger() *zap.SugaredLogger {
var zapLogger *zap.Logger
zapLogger, _ = zap.NewDevelopment()
logger := zapLogger.Sugar()
return logger
}
Loading

0 comments on commit e51442e

Please sign in to comment.