-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from cukhoaimon/hot_fix
Hot fix
- Loading branch information
Showing
22 changed files
with
777 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"github.com/cukhoaimon/SimpleBank/internal/delivery/grpc/pb" | ||
db "github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc" | ||
"github.com/cukhoaimon/SimpleBank/internal/usecase/val" | ||
"github.com/cukhoaimon/SimpleBank/utils" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (handler *Handler) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserResponse, error) { | ||
violations := validateUpdateUserRequest(req) | ||
if violations != nil { | ||
return nil, invalidArgumentError(violations) | ||
} | ||
|
||
arg := db.UpdateUserParams{ | ||
FullName: sql.NullString{ | ||
String: req.GetFullName(), | ||
Valid: req.FullName != nil, | ||
}, | ||
Email: sql.NullString{ | ||
String: req.GetEmail(), | ||
Valid: req.Email != nil, | ||
}, | ||
Username: req.GetUsername(), | ||
} | ||
|
||
if req.Password != nil { | ||
hashedPassword, err := utils.HashPassword(req.GetPassword()) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "fail to hash password: %s", err) | ||
} | ||
|
||
arg.HashedPassword = sql.NullString{ | ||
String: hashedPassword, | ||
Valid: true, | ||
} | ||
} | ||
|
||
user, err := handler.Store.UpdateUser(ctx, arg) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return nil, status.Errorf(codes.NotFound, "user not found") | ||
} | ||
|
||
return nil, status.Errorf(codes.Internal, "fail to create user: %s", err) | ||
} | ||
|
||
rsp := &pb.UpdateUserResponse{ | ||
User: convertUser(user), | ||
} | ||
return rsp, nil | ||
} | ||
|
||
func validateUpdateUserRequest(req *pb.UpdateUserRequest) (violations []*errdetails.BadRequest_FieldViolation) { | ||
if err := val.ValidateUsername(req.GetUsername()); err != nil { | ||
violations = append(violations, fieldViolation("username", err)) | ||
} | ||
|
||
if req.FullName != nil { | ||
if err := val.ValidateFullName(req.GetFullName()); err != nil { | ||
violations = append(violations, fieldViolation("full_name", err)) | ||
} | ||
} | ||
|
||
if req.Email != nil { | ||
if err := val.ValidateEmail(req.GetEmail()); err != nil { | ||
violations = append(violations, fieldViolation("email", err)) | ||
} | ||
} | ||
|
||
if req.Password != nil { | ||
if err := val.ValidatePassword(req.GetPassword()); err != nil { | ||
violations = append(violations, fieldViolation("password", err)) | ||
} | ||
} | ||
|
||
return violations | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.