Skip to content

Commit

Permalink
create/list events
Browse files Browse the repository at this point in the history
  • Loading branch information
colin014 committed Mar 27, 2018
1 parent 9400e2a commit 15c0d68
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
81 changes: 81 additions & 0 deletions api/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net/http"
"github.com/banzaicloud/banzai-types/components"
"github.com/colin014/football-mentor/utils"
)

func CreateGame(c *gin.Context) {
Expand Down Expand Up @@ -62,3 +64,82 @@ func GetGames(c *gin.Context) {
}

}

func CreateEvents(c *gin.Context) {

log := logger.WithFields(logrus.Fields{"tag": "Add events"})
log.Info("Start add events")

log.Info("Binding request")

gameId, isOk := getGameId(c)
if !isOk {
return
}

var createEventRequest model.CreateEventRequest
if err := c.BindJSON(&createEventRequest); err != nil {
log.Errorf("Error during bind json: %s", err.Error())
c.JSON(http.StatusBadRequest, model.ErrorResponse{
Code: http.StatusBadRequest,
Message: "Error during binding",
Error: err.Error(),
})
return
}

log.Debugf("Binding succeeded: %#v", createEventRequest)
log.Info("Save events into database")

if err := createEventRequest.SaveEvents(uint(gameId)); err != nil {
log.Errorf("Error during save events: %s", err.Error())
c.JSON(http.StatusBadRequest, components.ErrorResponse{
Code: http.StatusBadRequest,
Message: "Error during save",
Error: err.Error(),
})
return
}

c.Status(http.StatusCreated)

}

func ListEvents(c *gin.Context) {

log := logger.WithFields(logrus.Fields{"tag": "List events"})

gameId, isOk := getGameId(c)
if !isOk {
return
}

log.Info("Start listing events by gameId: %s", gameId)

if events, err := model.GetAllEvents(uint(gameId)); err != nil {
log.Errorf("Error during listing events: %s", err.Error())
c.JSON(http.StatusBadRequest, components.ErrorResponse{
Code: http.StatusBadRequest,
Message: "Error during listing events",
Error: err.Error(),
})
} else {
log.Info("Load events succeeded")
c.JSON(http.StatusOK, events)
}

}

func getGameId(c *gin.Context) (int, bool) {
gameId, err := utils.ConvertStringToInt(c.Param("gameid"))
if err != nil {
c.JSON(http.StatusBadRequest, components.ErrorResponse{
Code: http.StatusBadRequest,
Message: "GameId is not a number",
Error: "Wrong game id",
})
return 0, false
} else {
return int(gameId), true
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ func main() {
v1.PUT("/club", api.UpdateClubInfo)
v1.POST("/games", api.CreateGame)
v1.GET("/games", api.GetGames)
v1.POST("/games/:gameid/events", api.CreateEvents)
v1.GET("/games/:gameid/events", api.ListEvents)
router.Run(":6060")
}
35 changes: 30 additions & 5 deletions model/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ type GameListResponse struct {
}

type Event struct {
ResultId uint `gorm:"foreign_key" json:"-"`
IsHome bool `json:"is_home"`
Type int `json:"type"`
Minute int `json:"minute"`
PlayerName string `json:"player_name"`
BaseModel
GameId uint `gorm:"foreign_key" json:"-"`
IsHome bool `json:"is_home" binding:"required"`
Type int `json:"type" binding:"required"`
Minute int `json:"minute" binding:"required"`
PlayerName string `json:"player_name" binding:"required"`
}

type CreateEventRequest struct {
Events []Event `json:"events" binding:"required"`
}

func (GameModel) TableName() string {
Expand Down Expand Up @@ -65,3 +70,23 @@ func GetAllGames() ([]GameModel, error) {
func ConvertGameModelToResponse(games []GameModel) *GameListResponse {
return &GameListResponse{Games: games}
}

func (request *CreateEventRequest) SaveEvents(gameId uint) error {

for _, e := range request.Events {
e.GameId = gameId
err := db.Save(&e).Error
if err != nil {
return err
}
}

return nil

}

func GetAllEvents(gameId uint) ([]Event, error) {
var events []Event
err := db.Where(Event{GameId: gameId}).Find(&events).Error
return events, err
}
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

import (
"strconv"
)

func ConvertStringToInt(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 32)
}

0 comments on commit 15c0d68

Please sign in to comment.