From 7bf8db8050ea836e6877260f9ee2fd33ce5b09b1 Mon Sep 17 00:00:00 2001 From: Jeffrey Duroyon Date: Fri, 12 Nov 2021 00:38:44 +0100 Subject: [PATCH] feat(accounts): improve account creation --- .DS_Store | Bin 8196 -> 8196 bytes internal/account/database.go | 18 +++++++++++++++--- internal/account/handler.go | 20 +++++++++----------- internal/account/service.go | 2 -- internal/user/service.go | 3 --- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.DS_Store b/.DS_Store index d8854b97a48b0f75fd16777f8fc9848c07026f97..3a5052a97e2bdef2186fdf7a2b808c5e54f9b54f 100644 GIT binary patch delta 117 zcmZp1XmQw(F2u++`GAm!NKU$8aB_Zb0RtGYy8i%U{YeiBfCLyJAcN;UqF gBa&naPRY$I!py7_3ph5jOMGM5%qq&nJh6Zk0I9zmmH+?% delta 102 zcmZp1XmQw(F2u+^d4iCL7(;PRx?yl~er^E+7_hqnDX^3jLve1ti%U{YeiBfKLyJAc mN;UqFBTSl!p?LBIA#sQ?K+fhrLd>k2*(JWQZ2l_3&I|zkof(b* diff --git a/internal/account/database.go b/internal/account/database.go index 70f76fd..c2c5097 100644 --- a/internal/account/database.go +++ b/internal/account/database.go @@ -1,7 +1,10 @@ package account import ( + "fmt" + "nos-comptes/internal/storage/dao" "nos-comptes/internal/storage/dao/postgresql" + "nos-comptes/internal/utils" "github.com/lib/pq" ) @@ -41,13 +44,22 @@ func (db *Database) GetAccountWithNameForUser(name string, id string) (*Account, WHERE a.user_id = $1 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{} - 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 { return nil, postgresql.HandlePgError(errPq) } + if err != nil { + utils.GetLogger().Info(err) + return nil, err + } return &a, nil } diff --git a/internal/account/handler.go b/internal/account/handler.go index da94a6f..c9c8cd0 100644 --- a/internal/account/handler.go +++ b/internal/account/handler.go @@ -1,7 +1,6 @@ package account import ( - "fmt" "net/http" "nos-comptes/handler" "nos-comptes/internal/storage/dao/postgresql" @@ -64,11 +63,13 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) { utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err)) return } - + utils.GetLogger().Info(userId) _, err = c.userService.GetUserById(userId) 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.JSONErrorWithMessage(gc.Writer, *e, e.Description) + return } else if err != nil { utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user") utils.JSONError(gc.Writer, model.ErrInternalServer) @@ -81,19 +82,15 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) { 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.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetAccount: get account error") utils.JSONErrorWithMessage(gc.Writer, *e, e.Description) + return } } 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) return } @@ -101,12 +98,13 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) { 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") + return } account.UserId = userId accountSaved, err := c.service.CreateAccount(account) if err != nil { - fmt.Println(err) - utils.JSONError(gc.Writer, model.ErrInternalServer) + utils.GetLogger().Info(err) + utils.JSONErrorWithMessage(gc.Writer, model.ErrInternalServer, err.Error()) return } utils.JSON(gc.Writer, http.StatusCreated, accountSaved) diff --git a/internal/account/service.go b/internal/account/service.go index 1cd9584..ccf0ee7 100644 --- a/internal/account/service.go +++ b/internal/account/service.go @@ -3,7 +3,6 @@ package account import ( "nos-comptes/internal/storage/dao" "nos-comptes/internal/storage/model" - "nos-comptes/internal/utils" ) 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) { account, err := s.db.GetAccountWithNameForUser(name, id) - utils.GetLogger().Warn(err) if e, ok := err.(*dao.Error); ok { switch { case e.Type == dao.ErrTypeNotFound: diff --git a/internal/user/service.go b/internal/user/service.go index 73d5210..1583cfc 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -3,7 +3,6 @@ package user import ( "nos-comptes/internal/storage/dao" "nos-comptes/internal/storage/model" - "nos-comptes/internal/utils" ) type Service struct { @@ -17,7 +16,6 @@ func NewService(database *Database) *Service { func (s *Service) GetUserById(userId string) (*User, error) { user, err := s.db.GetUsersByID(userId) if e, ok := err.(*dao.Error); ok { - utils.GetLogger().Warn(err) switch { case e.Type == dao.ErrTypeNotFound: return nil, &model.ErrNotFound @@ -37,7 +35,6 @@ func (s *Service) GetUserById(userId string) (*User, error) { func (us *Service) GetUserFromGoogleID(googleUserID string) (*User, error) { user, err := us.db.GetUsersByGoogleID(googleUserID) if err != nil { - utils.GetLogger().Warn(err) if castedError, ok := err.(*dao.Error); ok { switch castedError.Type { case dao.ErrTypeNotFound: