feat(account): get a specific account
This commit is contained in:
@@ -91,6 +91,26 @@ func (db *Database) DeleteAccountOfAnUser(userId, accountId string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) GetASpecificAccountForUser(userId, accountId string) (*Account, error) {
|
||||
q := `
|
||||
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
|
||||
FROM public.account a
|
||||
WHERE a.user_id = $1
|
||||
AND a.id = $2
|
||||
`
|
||||
row := db.Session.QueryRow(q, userId, accountId)
|
||||
a := Account{}
|
||||
err := row.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
|
||||
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
|
||||
}
|
||||
|
||||
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {
|
||||
return &Database{db}
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ func (c *Context) DeleteAccountOfUser(gc *gin.Context) {
|
||||
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
accountParam, err := c.userService.GetUserById(userId)
|
||||
usrParam, 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")
|
||||
@@ -143,7 +143,7 @@ func (c *Context) DeleteAccountOfUser(gc *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
acc, err := c.userService.GetUserFromGoogleID(googleUserId.(string))
|
||||
usr, err := c.userService.GetUserFromGoogleID(googleUserId.(string))
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLogger().Info(err)
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUserFromGoogleID: get user from google user id")
|
||||
@@ -154,7 +154,7 @@ func (c *Context) DeleteAccountOfUser(gc *gin.Context) {
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if acc == nil || acc.ID != accountParam.ID {
|
||||
if usr == nil || usr.ID != usrParam.ID {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("User in path doesn't match authenticated user")
|
||||
utils.JSONError(gc.Writer, model.ErrBadRequestFormat)
|
||||
return
|
||||
@@ -164,8 +164,70 @@ func (c *Context) DeleteAccountOfUser(gc *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func (c *Context) GetSpecificAccountOfUser(context *gin.Context) {
|
||||
func (c *Context) GetSpecificAccountOfUser(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
|
||||
}
|
||||
|
||||
accountId := gc.Param("accountId")
|
||||
err = c.Validator.VarCtx(gc, userId, "uuid4")
|
||||
if err != nil {
|
||||
utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
|
||||
return
|
||||
}
|
||||
|
||||
usrParam, 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)
|
||||
return
|
||||
}
|
||||
|
||||
googleUserId, exists := gc.Get("googleUserId")
|
||||
if exists == false {
|
||||
utils.GetLoggerFromCtx(gc).Error("error while getting google user id")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
usr, err := c.userService.GetUserFromGoogleID(googleUserId.(string))
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLogger().Info(err)
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUserFromGoogleID: get user from google user id")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
return
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user from google user id")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if usr == nil || usr.ID != usrParam.ID {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("User in path doesn't match authenticated user")
|
||||
utils.JSONError(gc.Writer, model.ErrBadRequestFormat)
|
||||
return
|
||||
}
|
||||
|
||||
account, err := c.service.GetASpecificAccountForUser(usr.ID, accountId)
|
||||
if e, ok := err.(*model.APIError); ok {
|
||||
utils.GetLogger().Info(err)
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUserFromGoogleID: get user from google user id")
|
||||
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
|
||||
return
|
||||
} else if err != nil {
|
||||
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user from google user id")
|
||||
utils.JSONError(gc.Writer, model.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
utils.JSON(gc.Writer, http.StatusCreated, account)
|
||||
}
|
||||
|
||||
func NewHandler(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Context {
|
||||
|
||||
@@ -52,6 +52,11 @@ func (s *Service) DeleteAccountOfUser(userId, accountId string) error {
|
||||
return s.db.DeleteAccountOfAnUser(userId, accountId)
|
||||
}
|
||||
|
||||
func (s *Service) GetASpecificAccountForUser(userId, accountId string) (*Account, error) {
|
||||
return s.db.GetASpecificAccountForUser(userId, accountId)
|
||||
|
||||
}
|
||||
|
||||
func NewService(database *Database) *Service {
|
||||
return &Service{db: database}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user