Skip to content

Commit

Permalink
feat: query user condition
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina committed Oct 2, 2024
1 parent f7b1825 commit 6f05f30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
4 changes: 4 additions & 0 deletions internal/api/v1beta1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,15 @@ func (h Handler) createUser(ctx context.Context, name, email string) (user.User,
}

func (h Handler) DeleteUser(ctx context.Context, request *shieldv1beta1.DeleteUserRequest) (*shieldv1beta1.DeleteUserResponse, error) {
logger := grpczap.Extract(ctx)

if _, err := h.userService.FetchCurrentUser(ctx); err != nil {
logger.Error(err.Error())
return &shieldv1beta1.DeleteUserResponse{}, grpcUnauthenticated
}

if err := h.userService.DeleteUser(ctx, request.Id); err != nil {
logger.Error(err.Error())
switch {
case errors.Is(err, user.ErrNotExist):
return &shieldv1beta1.DeleteUserResponse{}, grpcBadBodyError
Expand Down
50 changes: 39 additions & 11 deletions internal/store/postgres/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (r UserRepository) GetByID(ctx context.Context, id string) (user.User, erro
var fetchedUser User
userQuery, params, err := dialect.From(TABLE_USERS).
Where(goqu.Ex{
"id": id,
"id": id,
"deleted_at": nil,
}).ToSQL()
if err != nil {
return user.User{}, fmt.Errorf("%w: %s", queryErr, err)
Expand Down Expand Up @@ -196,6 +197,7 @@ func (r UserRepository) List(ctx context.Context, flt user.Filter) ([]user.User,
goqu.C("name").ILike(fmt.Sprintf("%%%s%%", flt.Keyword)),
goqu.C("email").ILike(fmt.Sprintf("%%%s%%", flt.Keyword)),
),
goqu.Ex{"deleted_at": nil},
).Limit(uint(flt.Limit)).Offset(uint(offset)).ToSQL()
if err != nil {
return []user.User{}, fmt.Errorf("%w: %s", queryErr, err)
Expand Down Expand Up @@ -315,7 +317,8 @@ func (r UserRepository) GetByIDs(ctx context.Context, userIDs []string) ([]user.

query, params, err := dialect.From(TABLE_USERS).Select("id", "name", "email").Where(
goqu.Ex{
"id": goqu.Op{"in": userIDs},
"id": goqu.Op{"in": userIDs},
"deleted_at": nil,
}).ToSQL()
if err != nil {
return []user.User{}, fmt.Errorf("%w: %s", queryErr, err)
Expand Down Expand Up @@ -389,7 +392,8 @@ func (r UserRepository) UpdateByEmail(ctx context.Context, usr user.User) (user.
"updated_at": goqu.L("now()"),
}).Where(
goqu.Ex{
"email": usr.Email,
"email": usr.Email,
"deleted_at": nil,
},
).Returning("created_at", "deleted_at", "email", "id", "name", "updated_at").ToSQL()
if err != nil {
Expand Down Expand Up @@ -464,7 +468,8 @@ func (r UserRepository) UpdateByID(ctx context.Context, usr user.User) (user.Use
"updated_at": goqu.L("now()"),
}).Where(
goqu.Ex{
"id": usr.ID,
"id": usr.ID,
"deleted_at": nil,
},
).Returning("created_at", "deleted_at", "email", "id", "name", "updated_at").ToSQL()
if err != nil {
Expand Down Expand Up @@ -525,7 +530,8 @@ func (r UserRepository) GetByEmail(ctx context.Context, email string) (user.User

query, params, err := dialect.From(TABLE_USERS).Where(
goqu.Ex{
"email": email,
"email": email,
"deleted_at": nil,
}).ToSQL()
if err != nil {
return user.User{}, fmt.Errorf("%w: %s", queryErr, err)
Expand Down Expand Up @@ -626,7 +632,8 @@ func (r UserRepository) DeleteByEmail(ctx context.Context, email string, emailTa
"email": fmt.Sprintf("%s+%s@%s", emailSplit[0], emailTag, emailSplit[1]),
}).Where(
goqu.Ex{
"email": email,
"email": email,
"deleted_at": nil,
}).ToSQL()
if err != nil {
return fmt.Errorf("%w: %s", queryErr, err)
Expand All @@ -652,8 +659,18 @@ func (r UserRepository) DeleteByEmail(ctx context.Context, email string, emailTa
defer nr.End()
}

_, err := r.dbc.ExecContext(ctx, query, params...)
return err
res, err := r.dbc.ExecContext(ctx, query, params...)
if err != nil {
return err
}
rowCount, err := res.RowsAffected()
if err != nil {
return err
}
if rowCount == 0 {
return sql.ErrNoRows
}
return nil
}); err != nil {
err = checkPostgresError(err)
switch {
Expand All @@ -677,7 +694,8 @@ func (r UserRepository) DeleteById(ctx context.Context, id string, emailTag stri
"email": goqu.Select().From("new_email"),
}).Where(
goqu.Ex{
"id": id,
"id": id,
"deleted_at": nil,
}).ToSQL()
if err != nil {
return fmt.Errorf("%w: %s", queryErr, err)
Expand All @@ -703,8 +721,18 @@ func (r UserRepository) DeleteById(ctx context.Context, id string, emailTag stri
defer nr.End()
}

_, err := r.dbc.ExecContext(ctx, query, params...)
return err
res, err := r.dbc.ExecContext(ctx, query, params...)
if err != nil {
return err
}
rowCount, err := res.RowsAffected()
if err != nil {
return err
}
if rowCount == 0 {
return sql.ErrNoRows
}
return nil
}); err != nil {
err = checkPostgresError(err)
switch {
Expand Down

0 comments on commit 6f05f30

Please sign in to comment.