112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"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) {
|
|
users, err := hc.db.GetAllHamsters()
|
|
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, users)
|
|
}
|
|
|
|
func (hc *handlersContext) getAHamster(c *gin.Context) {
|
|
userID := c.Param("hamsterId")
|
|
|
|
err := hc.validator.VarCtx(c, userID, "uuid4")
|
|
if err != nil {
|
|
utils.JSONError(c.Writer, validators.NewDataValidationAPIError(err))
|
|
return
|
|
}
|
|
|
|
user, err := hc.db.GetHamsterById(userID)
|
|
if e, ok := err.(*dao.DAOError); 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 user == nil {
|
|
utils.JSONErrorWithMessage(c.Writer, model.ErrNotFound, "Hamster not found")
|
|
return
|
|
}
|
|
|
|
utils.JSON(c.Writer, http.StatusOK, user)
|
|
}
|
|
|
|
func (hc *handlersContext) createAHamster(c *gin.Context) {
|
|
|
|
}
|
|
|
|
func (hc *handlersContext) updateAHamster(c *gin.Context) {
|
|
panic("TO DO")
|
|
}
|
|
|
|
func (hc *handlersContext) deleteAHamster(c *gin.Context) {
|
|
|
|
hamsterID := c.Param("hamsterId")
|
|
|
|
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)
|
|
if e, ok := err.(*dao.DAOError); 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.DAOError); 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)
|
|
|
|
}
|