diff --git a/.DS_Store b/.DS_Store index d8854b9..3a5052a 100644 Binary files a/.DS_Store and b/.DS_Store differ 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: