Files
noscomptes-back/internal/jointaccount/handler.go

101 lines
3.4 KiB
Go

package jointaccount
import (
"net/http"
"nos-comptes/handler"
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/storage/model"
"nos-comptes/internal/storage/validators"
"nos-comptes/internal/user"
"nos-comptes/internal/utils"
"github.com/gin-gonic/gin"
)
type Context struct {
service *Service
db *Database
userService *user.Service
*handler.Context
}
func (c *Context) GetAllJointaccountOfUser(gc *gin.Context) {
userId := gc.Param("userId")
jointaccounts, err := c.service.GetAllJointaccountOfUser(userId)
if e, ok := err.(*model.APIError); ok {
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAllJointaccounts: get jointaccounts")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get jointaccounts")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
if len(jointaccounts) == 0 {
utils.JSON(gc.Writer, http.StatusNoContent, nil)
} else {
utils.JSON(gc.Writer, http.StatusOK, jointaccounts)
}
}
func (c *Context) CreateJointaccountOfUser(gc *gin.Context) {
userId := gc.Param("userId")
var jointaccount Jointaccount
var jointaccountEditable JointaccountEditable
if err := gc.BindJSON(&jointaccountEditable); err != nil {
utils.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
return
}
jointaccount = Jointaccount{JointaccountEditable: jointaccountEditable, UserId: userId}
jointaccountFound, err := c.service.GetJointaccountWithNameForUser(jointaccount.Name, userId)
if e, ok := err.(*model.APIError); ok {
if e.Type != model.ErrNotFound.Type {
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetJointaccount: get jointaccount error")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
return
}
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get jointaccount")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
if jointaccountFound != nil {
utils.GetLoggerFromCtx(gc).WithError(&model.ErrAlreadyExists).WithField("type", model.ErrAlreadyExists.Type).Error("error CreateJointaccount: jointaccount already exists")
utils.JSONErrorWithMessage(gc.Writer, model.ErrAlreadyExists, "jointaccount already exists with the same Name")
return
}
jointaccount.UserId = userId
jointaccountSaved, err := c.service.CreateJointaccount(jointaccount)
if err != nil {
utils.GetLogger().Info(err)
utils.JSONErrorWithMessage(gc.Writer, model.ErrInternalServer, err.Error())
return
}
utils.JSON(gc.Writer, http.StatusCreated, jointaccountSaved)
return
}
func (c *Context) DeleteJointaccountOfUser(gc *gin.Context) {
userId := gc.Param("userId")
jointaccountId := gc.Param("jointaccountId")
c.service.DeleteJointaccountOfUser(userId, jointaccountId)
}
func (c *Context) GetSpecificJointaccountOfUser(gc *gin.Context) {
userId := gc.Param("userId")
jointaccountId := gc.Param("jointaccountId")
jointaccount, _ := c.service.GetASpecificJointaccountForUser(userId, jointaccountId)
utils.JSON(gc.Writer, http.StatusOK, jointaccount)
}
func NewHandler(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Context {
database := NewDatabase(db)
service := NewService(database)
userService := user.NewService(user.NewDatabase(db))
return &Context{service: service, db: database, userService: userService, Context: ctx}
}