From 56134fc9755da9bb20a5ac9f417dbbdbb25433cd Mon Sep 17 00:00:00 2001 From: Jeffrey Duroyon Date: Tue, 24 Aug 2021 00:26:05 +0200 Subject: [PATCH] clean up --- handlers_v2/cage_handler.go | 69 --------- handlers_v2/hamster_handler.go | 207 ------------------------- handlers_v2/handler.go | 126 ---------------- handlers_v2/health_handler.go | 12 -- handlers_v2/user_handler.go | 268 --------------------------------- 5 files changed, 682 deletions(-) delete mode 100644 handlers_v2/cage_handler.go delete mode 100644 handlers_v2/hamster_handler.go delete mode 100755 handlers_v2/handler.go delete mode 100755 handlers_v2/health_handler.go delete mode 100755 handlers_v2/user_handler.go diff --git a/handlers_v2/cage_handler.go b/handlers_v2/cage_handler.go deleted file mode 100644 index 2d00f9a..0000000 --- a/handlers_v2/cage_handler.go +++ /dev/null @@ -1,69 +0,0 @@ -package handlers - -import ( - "hamster-tycoon/storage/dao" - "hamster-tycoon/storage/model" - "hamster-tycoon/storage/validators" - "hamster-tycoon/utils" - "net/http" - - "github.com/gin-gonic/gin" -) - -func (hc *handlersContext) getAllCages(c *gin.Context) { - gameID := c.Param("gameID") - err := hc.validator.VarCtx(c, gameID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - cages, err := hc.db.GetAllCages(gameID) - if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting cages") - utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting cages") - return - } - utils.JSON(c.Writer, http.StatusOK, cages) -} - -func (hc *handlersContext) getACage(c *gin.Context) { - cageID := c.Param("cageID") - gameID := c.Param("gameID") - - err := hc.validator.VarCtx(c, cageID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, gameID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - cage, err := hc.db.GetCageByID(gameID, cageID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error GetCage: get cage error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while get cage") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - if cage == nil { - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Cage not found") - return - } - - utils.JSON(c.Writer, http.StatusOK, cage) -} diff --git a/handlers_v2/hamster_handler.go b/handlers_v2/hamster_handler.go deleted file mode 100644 index 89ba0db..0000000 --- a/handlers_v2/hamster_handler.go +++ /dev/null @@ -1,207 +0,0 @@ -package handlers - -import ( - "encoding/json" - "github.com/gin-gonic/gin" - "hamster-tycoon/storage/dao" - "hamster-tycoon/storage/model" - "hamster-tycoon/storage/validators" - "hamster-tycoon/utils" - "net/http" -) - -func (hc *handlersContext) getAllHamsters(c *gin.Context) { - gameID := c.Param("gameID") - cageID := c.Param("cageID") - - err := hc.validator.VarCtx(c, cageID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, gameID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - hamsters, err := hc.db.GetAllHamsters(gameID, cageID) - if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting hamsters") - utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting hamsters") - return - } - utils.JSON(c.Writer, http.StatusOK, hamsters) -} - -func (hc *handlersContext) getAHamster(c *gin.Context) { - gameID := c.Param("gameID") - cageID := c.Param("cageID") - hamsterID := c.Param("hamsterID") - - err := hc.validator.VarCtx(c, cageID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, gameID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, hamsterID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - hamster, err := hc.db.GetHamsterByID(hamsterID, gameID, cageID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error GetHamster: get hamster error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while get hamster") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - if hamster == nil { - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found") - return - } - - utils.JSON(c.Writer, http.StatusOK, hamster) -} - -func (hc *handlersContext) createAHamster(c *gin.Context) { - gameID := c.Param("gameID") - cageID := c.Param("cageID") - - err := hc.validator.VarCtx(c, cageID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, gameID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - b, err := c.GetRawData() - if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating hamster, read data fail") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - hamsterToCreate := model.Hamster{} - err = json.Unmarshal(b, &hamsterToCreate) - if err != nil { - utils.JSONError(c.Writer, model.ErrBadRequestFormat) - return - } - - err = hc.validator.StructCtx(c, hamsterToCreate) - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - hamsterToCreate.Cage = &model.Cage{ID: cageID, Game: model.Game{ID: gameID}} - err = hc.db.CreateHamster(&hamsterToCreate) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeDuplicate: - utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "Hamster already exists") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error CreateHamster: Error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating hamster") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - utils.JSON(c.Writer, http.StatusCreated, hamsterToCreate) -} - -func (hc *handlersContext) updateAHamster(c *gin.Context) { - panic("TO DO") -} - -func (hc *handlersContext) deleteAHamster(c *gin.Context) { - hamsterID := c.Param("hamsterID") - gameID := c.Param("gameID") - cageID := c.Param("cageID") - - err := hc.validator.VarCtx(c, cageID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, gameID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - err = hc.validator.VarCtx(c, hamsterID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - // check hamster id given in URL exists - _, err = hc.db.GetHamsterByID(hamsterID, gameID, cageID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster to delete not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteHamster: get hamster error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while get hamster to delete") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - err = hc.db.DeleteHamster(hamsterID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster to delete not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteHamster: Error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while deleting hamster") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - utils.JSON(c.Writer, http.StatusNoContent, nil) - -} diff --git a/handlers_v2/handler.go b/handlers_v2/handler.go deleted file mode 100755 index 6d9749e..0000000 --- a/handlers_v2/handler.go +++ /dev/null @@ -1,126 +0,0 @@ -package handlers - -import ( - "hamster-tycoon/middlewares" - "hamster-tycoon/service" - "hamster-tycoon/storage/dao" - "hamster-tycoon/storage/dao/fake" - "hamster-tycoon/storage/dao/postgresql" - "hamster-tycoon/storage/validators" - "net/http" - "time" - - "reflect" - "strings" - - "github.com/gin-contrib/cors" - "github.com/gin-gonic/gin" - "gopkg.in/go-playground/validator.v9" -) - -type Config struct { - Mock bool - DBConnectionURI string - Port int - LogLevel string - LogFormat string -} - -type handlersContext struct { - db dao.Database - validator *validator.Validate - userService *service.UserService - gameService *service.GameService - cageService *service.CageService - hamsterService *service.HamsterService -} - -func NewRouter(config *Config) *gin.Engine { - gin.SetMode(gin.ReleaseMode) - - router := gin.New() - router.HandleMethodNotAllowed = true - - router.Use(cors.New(cors.Config{ - AllowOrigins: []string{"*"}, - AllowMethods: []string{"*"}, - AllowHeaders: []string{"*"}, - ExposeHeaders: []string{"*"}, - AllowCredentials: true, - MaxAge: 12 * time.Hour, - })) - router.Use(gin.Recovery()) - router.Use(middlewares.GetLoggerMiddleware()) - router.Use(middlewares.GetHTTPLoggerMiddleware()) - - hc := &handlersContext{} - if config.Mock { - hc.db = fake.NewDatabaseFake() - } else { - hc.db = postgresql.NewDatabasePostgreSQL(config.DBConnectionURI) - } - hc.validator = newValidator() - hc.userService = service.NewUserService(hc.db) - hc.gameService = service.NewGameService(hc.db) - hc.cageService = service.NewCageService(hc.db) - hc.hamsterService = service.NewHamsterService(hc.db) - - public := router.Group("/") - public.Handle(http.MethodGet, "/_health", hc.GetHealth) - - userRoute := public.Group("/users") - userRoute.Handle("LOGIN", "", hc.connectUser) - userRoute.Handle(http.MethodPost, "", hc.createUser) - - securedUserRoute := userRoute.Group("") - //TODO add secure auth - securedUserRoute.Handle(http.MethodGet, "", hc.getAllUsers) - securedUserRoute.Handle(http.MethodGet, "/:userID", hc.getUser) - securedUserRoute.Handle(http.MethodPut, "/:userID", hc.updateUser) - securedUserRoute.Handle(http.MethodDelete, "/:userID", hc.deleteUser) - // end: user routes - - gameRoute := securedUserRoute.Group("/:userID/games") - gameRoute.Handle(http.MethodGet, "", hc.getAllGames) - gameRoute.Handle(http.MethodGet, "/:gameID", hc.getAGame) - gameRoute.Handle(http.MethodPost, "/:gameID", hc.createAGame) - gameRoute.Handle(http.MethodPut, "/:gameID", hc.updateAGame) - gameRoute.Handle(http.MethodGet, "/:gameID/hamsters", hc.getHamstersOfGame) - gameRoute.Handle(http.MethodDelete, "/:gameID", hc.deleteAGame) - - cageRoute := gameRoute.Group("/:gameID/cages") - cageRoute.Handle(http.MethodGet, "", hc.getAllCages) - cageRoute.Handle(http.MethodGet, "/:cageID", hc.getACage) - cageRoute.Handle(http.MethodPost, "/:cageID", hc.createACage) - cageRoute.Handle(http.MethodPut, "/:cageID", hc.updateACage) - cageRoute.Handle(http.MethodDelete, "/:cageID", hc.deleteACage) - - hamsterRoute := cageRoute.Group("/:cageID/hamsters") - hamsterRoute.Handle(http.MethodGet, "", hc.getAllHamsters) - hamsterRoute.Handle(http.MethodGet, "/:hamsterID", hc.getAHamster) - hamsterRoute.Handle(http.MethodPost, "/:hamsterID", hc.createAHamster) - hamsterRoute.Handle(http.MethodPut, "/:hamsterID", hc.updateAHamster) - hamsterRoute.Handle(http.MethodDelete, "/:hamsterID", hc.deleteAHamster) - - return router -} - -func newValidator() *validator.Validate { - va := validator.New() - - va.RegisterTagNameFunc(func(fld reflect.StructField) string { - name := strings.SplitN(fld.Tag.Get("json"), ",", 2) - if len(name) < 1 { - return "" - } - return name[0] - }) - - for k, v := range validators.CustomValidators { - if v.Validator != nil { - va.RegisterValidationCtx(k, v.Validator) - } - } - - return va -} diff --git a/handlers_v2/health_handler.go b/handlers_v2/health_handler.go deleted file mode 100755 index 1a2cb76..0000000 --- a/handlers_v2/health_handler.go +++ /dev/null @@ -1,12 +0,0 @@ -package handlers - -import ( - "hamster-tycoon/utils" - "net/http" - - "github.com/gin-gonic/gin" -) - -func (hc *handlersContext) GetHealth(c *gin.Context) { - utils.JSON(c.Writer, http.StatusNoContent, nil) -} diff --git a/handlers_v2/user_handler.go b/handlers_v2/user_handler.go deleted file mode 100755 index 317deea..0000000 --- a/handlers_v2/user_handler.go +++ /dev/null @@ -1,268 +0,0 @@ -package handlers - -import ( - "encoding/json" - "fmt" - "hamster-tycoon/storage/dao" - "hamster-tycoon/storage/model" - "hamster-tycoon/storage/validators" - "hamster-tycoon/utils" - "net/http" - "strings" - - "github.com/gin-gonic/gin" - "google.golang.org/api/oauth2/v2" -) - -func (hc *handlersContext) getAllUsers(c *gin.Context) { - users, err := hc.db.GetAllUsers() - if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while getting users") - utils.JSONErrorWithMessage(c.Writer, model.ErrInternalServer, "Error while getting users") - return - } - utils.JSON(c.Writer, http.StatusOK, users) -} - -func (hc *handlersContext) connectUser(c *gin.Context) { - authorizationHeader := c.GetHeader("Authorization") - authorizationHeaderSplitted := strings.Split(authorizationHeader, " ") - if len(authorizationHeaderSplitted) != 2 { - utils.JSONError(c.Writer, model.ErrBadRequestFormat) - return - } - - oauth2Service, err := oauth2.New(&http.Client{}) - if oauth2Service == nil { - fmt.Println(err) - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - tokenInfoCall := oauth2Service.Tokeninfo() - tokenInfoCall.IdToken(authorizationHeaderSplitted[1]) - tokenInfo, err := tokenInfoCall.Do() - if err != nil { - utils.GetLogger().WithError(err).Error(err) - utils.JSONError(c.Writer, model.ErrBadRequestFormat) - return - } - user, err := hc.userService.GetUserFromGoogleID(tokenInfo.UserId) - if err != nil { - utils.GetLogger().WithError(err).Error(err) - if castedError, ok := err.(*model.APIError); ok { - if castedError.Type == model.ErrNotFound.Type { - user, err := hc.userService.CreateUserFromGoogleToken(tokenInfo.UserId, tokenInfo.Email) - if err != nil { - fmt.Println(err) - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - utils.JSON(c.Writer, 200, user) - return - } - utils.JSONError(c.Writer, *castedError) - return - } - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - fmt.Println("Found the user " + user.Nickname) - fmt.Println("Return 200") - utils.JSON(c.Writer, 200, user) -} -func (hc *handlersContext) createUser(c *gin.Context) { - b, err := c.GetRawData() - if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating user, read data fail") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - userToCreate := model.UserEditable{} - err = json.Unmarshal(b, &userToCreate) - if err != nil { - utils.JSONError(c.Writer, model.ErrBadRequestFormat) - return - } - - err = hc.validator.StructCtx(c, userToCreate) - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - user := model.User{ - UserEditable: userToCreate, - } - - err = hc.db.CreateUser(&user) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeDuplicate: - utils.JSONErrorWithMessage(c.Writer, model.ErrAlreadyExists, "User already exists") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error CreateUser: Error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while creating user") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - utils.JSON(c.Writer, http.StatusCreated, user) -} - -func (hc *handlersContext) getUser(c *gin.Context) { - userID := c.Param("id") - - err := hc.validator.VarCtx(c, userID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - user, err := hc.db.GetUsersByID(userID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while get user") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - if user == nil { - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User not found") - return - } - - utils.JSON(c.Writer, http.StatusOK, user) -} - -func (hc *handlersContext) deleteUser(c *gin.Context) { - userID := c.Param("id") - - err := hc.validator.VarCtx(c, userID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - // check user id given in URL exists - _, err = hc.db.GetUsersByID(userID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to delete not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteUser: get user error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while get user to delete") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - err = hc.db.DeleteUser(userID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to delete not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error DeleteUser: Error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while deleting user") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - utils.JSON(c.Writer, http.StatusNoContent, nil) -} - -func (hc *handlersContext) updateUser(c *gin.Context) { - userID := c.Param("id") - - err := hc.validator.VarCtx(c, userID, "uuid4") - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - // check user id given in URL exists - user, err := hc.db.GetUsersByID(userID) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to update not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("deleteUser: get user error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while get user to update") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - // get body and verify data - b, err := c.GetRawData() - if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while updating user, read data fail") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - userToUpdate := model.UserEditable{} - err = json.Unmarshal(b, &userToUpdate) - if err != nil { - utils.JSONError(c.Writer, model.ErrBadRequestFormat) - return - } - - err = hc.validator.StructCtx(c, userToUpdate) - if err != nil { - utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err)) - return - } - - user.UserEditable = userToUpdate - - // make the update - err = hc.db.UpdateUser(user) - if e, ok := err.(*dao.Error); ok { - switch { - case e.Type == dao.ErrTypeNotFound: - utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "User to update not found") - return - default: - utils.GetLoggerFromCtx(c).WithError(err).WithField("type", e.Type).Error("error UpdateUser: Error type not handled") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - } else if err != nil { - utils.GetLoggerFromCtx(c).WithError(err).Error("error while deleting user") - utils.JSONError(c.Writer, model.ErrInternalServer) - return - } - - utils.JSON(c.Writer, http.StatusOK, user) -}