feat(accounts): improve account creation
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"nos-comptes/internal/storage/dao"
|
||||||
"nos-comptes/internal/storage/dao/postgresql"
|
"nos-comptes/internal/storage/dao/postgresql"
|
||||||
|
"nos-comptes/internal/utils"
|
||||||
|
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
)
|
)
|
||||||
@@ -41,13 +44,22 @@ func (db *Database) GetAccountWithNameForUser(name string, id string) (*Account,
|
|||||||
WHERE a.user_id = $1
|
WHERE a.user_id = $1
|
||||||
AND a.name = $2
|
AND a.name = $2
|
||||||
`
|
`
|
||||||
row := db.Session.QueryRow(q, id, name)
|
row, err := db.Session.Query(q, id, name)
|
||||||
|
if !row.Next() {
|
||||||
|
return nil, dao.NewDAOError(dao.ErrTypeNotFound, fmt.Errorf("No row found"))
|
||||||
|
}
|
||||||
a := Account{}
|
a := Account{}
|
||||||
err := row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
||||||
|
if row.Next() {
|
||||||
|
return nil, fmt.Errorf("Impossibru")
|
||||||
|
}
|
||||||
if errPq, ok := err.(*pq.Error); ok {
|
if errPq, ok := err.(*pq.Error); ok {
|
||||||
return nil, postgresql.HandlePgError(errPq)
|
return nil, postgresql.HandlePgError(errPq)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
utils.GetLogger().Info(err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &a, nil
|
return &a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"nos-comptes/handler"
|
"nos-comptes/handler"
|
||||||
"nos-comptes/internal/storage/dao/postgresql"
|
"nos-comptes/internal/storage/dao/postgresql"
|
||||||
@@ -64,11 +63,13 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) {
|
|||||||
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
utils.GetLogger().Info(userId)
|
||||||
_, err = c.userService.GetUserById(userId)
|
_, err = c.userService.GetUserById(userId)
|
||||||
if e, ok := err.(*model.APIError); ok {
|
if e, ok := err.(*model.APIError); ok {
|
||||||
|
utils.GetLogger().Info(err)
|
||||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
|
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
|
||||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||||
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
|
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
|
||||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||||
@@ -81,19 +82,15 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
account = Account{AccountEditable: accountEditable, UserId: userId}
|
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)
|
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, ok := err.(*model.APIError); ok {
|
||||||
if e.Type != model.ErrNotFound.Type {
|
if e.Type != model.ErrNotFound.Type {
|
||||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
|
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAccount: get account error")
|
||||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
|
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get account")
|
||||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -101,12 +98,13 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) {
|
|||||||
if accountFound != nil {
|
if accountFound != nil {
|
||||||
utils.GetLoggerFromCtx(gc).WithError(&model.ErrAlreadyExists).WithField("type", model.ErrAlreadyExists.Type).Error("error CreateAccount: account already exists")
|
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")
|
utils.JSONErrorWithMessage(gc.Writer, model.ErrAlreadyExists, "account already exists with the same Name")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
account.UserId = userId
|
account.UserId = userId
|
||||||
accountSaved, err := c.service.CreateAccount(account)
|
accountSaved, err := c.service.CreateAccount(account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
utils.GetLogger().Info(err)
|
||||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
utils.JSONErrorWithMessage(gc.Writer, model.ErrInternalServer, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
utils.JSON(gc.Writer, http.StatusCreated, accountSaved)
|
utils.JSON(gc.Writer, http.StatusCreated, accountSaved)
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package account
|
|||||||
import (
|
import (
|
||||||
"nos-comptes/internal/storage/dao"
|
"nos-comptes/internal/storage/dao"
|
||||||
"nos-comptes/internal/storage/model"
|
"nos-comptes/internal/storage/model"
|
||||||
"nos-comptes/internal/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
@@ -31,7 +30,6 @@ func (s *Service) GetAllAccountOfUser(userId string) ([]*Account, error) {
|
|||||||
|
|
||||||
func (s *Service) GetAccountWithNameForUser(name string, id string) (*Account, error) {
|
func (s *Service) GetAccountWithNameForUser(name string, id string) (*Account, error) {
|
||||||
account, err := s.db.GetAccountWithNameForUser(name, id)
|
account, err := s.db.GetAccountWithNameForUser(name, id)
|
||||||
utils.GetLogger().Warn(err)
|
|
||||||
if e, ok := err.(*dao.Error); ok {
|
if e, ok := err.(*dao.Error); ok {
|
||||||
switch {
|
switch {
|
||||||
case e.Type == dao.ErrTypeNotFound:
|
case e.Type == dao.ErrTypeNotFound:
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package user
|
|||||||
import (
|
import (
|
||||||
"nos-comptes/internal/storage/dao"
|
"nos-comptes/internal/storage/dao"
|
||||||
"nos-comptes/internal/storage/model"
|
"nos-comptes/internal/storage/model"
|
||||||
"nos-comptes/internal/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
@@ -17,7 +16,6 @@ func NewService(database *Database) *Service {
|
|||||||
func (s *Service) GetUserById(userId string) (*User, error) {
|
func (s *Service) GetUserById(userId string) (*User, error) {
|
||||||
user, err := s.db.GetUsersByID(userId)
|
user, err := s.db.GetUsersByID(userId)
|
||||||
if e, ok := err.(*dao.Error); ok {
|
if e, ok := err.(*dao.Error); ok {
|
||||||
utils.GetLogger().Warn(err)
|
|
||||||
switch {
|
switch {
|
||||||
case e.Type == dao.ErrTypeNotFound:
|
case e.Type == dao.ErrTypeNotFound:
|
||||||
return nil, &model.ErrNotFound
|
return nil, &model.ErrNotFound
|
||||||
@@ -37,7 +35,6 @@ func (s *Service) GetUserById(userId string) (*User, error) {
|
|||||||
func (us *Service) GetUserFromGoogleID(googleUserID string) (*User, error) {
|
func (us *Service) GetUserFromGoogleID(googleUserID string) (*User, error) {
|
||||||
user, err := us.db.GetUsersByGoogleID(googleUserID)
|
user, err := us.db.GetUsersByGoogleID(googleUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.GetLogger().Warn(err)
|
|
||||||
if castedError, ok := err.(*dao.Error); ok {
|
if castedError, ok := err.(*dao.Error); ok {
|
||||||
switch castedError.Type {
|
switch castedError.Type {
|
||||||
case dao.ErrTypeNotFound:
|
case dao.ErrTypeNotFound:
|
||||||
|
|||||||
Reference in New Issue
Block a user