From 15c0d6829a9aaf828a302229123d6adf6f3b63fe Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 27 Mar 2018 21:59:44 +0200 Subject: [PATCH] create/list events --- api/games.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ model/games.go | 35 ++++++++++++++++++---- utils/utils.go | 9 ++++++ 4 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 utils/utils.go diff --git a/api/games.go b/api/games.go index 4a51109..3dac82d 100644 --- a/api/games.go +++ b/api/games.go @@ -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) { @@ -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 + } +} diff --git a/main.go b/main.go index bad78a0..e54a4b6 100644 --- a/main.go +++ b/main.go @@ -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") } diff --git a/model/games.go b/model/games.go index 32e2a09..e7a0e5e 100644 --- a/model/games.go +++ b/model/games.go @@ -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 { @@ -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 +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..f255d93 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,9 @@ +package utils + +import ( + "strconv" +) + +func ConvertStringToInt(s string) (uint64, error) { + return strconv.ParseUint(s, 10, 32) +}