fix(account): add account creante and fix incorrect field for getting all
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"nos-comptes/handler"
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
@@ -56,8 +57,60 @@ func (c *Context) GetAllAccountOfUser(gc *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) CreateAccountOfUser(context *gin.Context) {
|
||||
func (c *Context) CreateAccountOfUser(gc *gin.Context) {
|
||||
userId := gc.Param("userId")
|
||||
err := c.Validator.VarCtx(gc, userId, "uuid4")
|
||||
if err != nil {
|
||||
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
_, err = c.userService.GetUserById(userId)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
var account Account
|
||||
var accountEditable AccountEditable
|
||||
if err := gc.BindJSON(&accountEditable); err != nil {
|
||||
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
account = Account{AccountEditable: accountEditable, UserId: userId}
|
||||
utils.GetLogger().Warn(account)
|
||||
utils.GetLogger().Warn(accountEditable.Name)
|
||||
utils.GetLogger().Warn(accountEditable.Provider)
|
||||
accountFound, err := c.service.GetAccountWithNameForUser(account.Name, userId)
|
||||
utils.GetLogger().Warn(err)
|
||||
utils.GetLogger().Warn(accountFound)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
if e.Type != model.ErrNotFound.Type {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
}
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
if accountFound != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(&model.ErrAlreadyExists).WithField("type", model.ErrAlreadyExists.Type).Error("error CreateAccount: account already exists")
|
||||
utils.JSONErrorWithMessage(gc.Writer, model.ErrAlreadyExists, "account already exists with the same Name")
|
||||
}
|
||||
account.UserId = userId
|
||||
accountSaved, err := c.service.CreateAccount(account)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
utils.JSON(gc.Writer, http.StatusCreated, accountSaved)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Context) DeleteAccountOfUser(context *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user