clean up
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
"hamster-tycoon/storage/validators"
|
||||
@@ -69,112 +68,3 @@ func (hc *handlersContext) getAHamster(c *gin.Context) {
|
||||
|
||||
utils.JSON(c.Writer, http.StatusOK, hamster)
|
||||
}
|
||||
|
||||
func (hc *handlersContext) createAHamster(c *gin.Context) {
|
||||
cageID := c.Param("cageID")
|
||||
|
||||
err := hc.validator.VarCtx(c, cageID, "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}
|
||||
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")
|
||||
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, 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, 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)
|
||||
|
||||
}
|
||||
|
||||
@@ -70,8 +70,8 @@ func NewRouter(config *Config) *gin.Engine {
|
||||
//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)
|
||||
//securedUserRoute.Handle(http.MethodPut, "/:userID", hc.updateUser)
|
||||
//securedUserRoute.Handle(http.MethodDelete, "/:userID", hc.deleteUser)
|
||||
// end: user routes
|
||||
|
||||
cageRoute := securedUserRoute.Group("/cages")
|
||||
@@ -84,9 +84,9 @@ func NewRouter(config *Config) *gin.Engine {
|
||||
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)
|
||||
//hamsterRoute.Handle(http.MethodPost, "/:hamsterID", hc.createAHamster)
|
||||
//hamsterRoute.Handle(http.MethodPut, "/:hamsterID", hc.updateAHamster)
|
||||
//hamsterRoute.Handle(http.MethodDelete, "/:hamsterID", hc.deleteAHamster)
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ func (hc *handlersContext) connectUser(c *gin.Context) {
|
||||
fmt.Println("Return 200")
|
||||
utils.JSON(c.Writer, 200, user)
|
||||
}
|
||||
|
||||
func (hc *handlersContext) createUser(c *gin.Context) {
|
||||
b, err := c.GetRawData()
|
||||
if err != nil {
|
||||
@@ -148,121 +149,3 @@ func (hc *handlersContext) getUser(c *gin.Context) {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user