Skip to content

Commit

Permalink
impl update user rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Jan 4, 2024
1 parent 8474093 commit cdc4242
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gapi/rpc_create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func validateCreateUserRequest(req *pb.CreateUserRequest) (violations []*errdeta
if err := val.ValidateFullName(req.GetFullName()); err != nil {
violations = append(violations, fieldViolation("full_name", err))
}
if err := val.ValidateFullName(req.GetEmail()); err != nil {
if err := val.ValidateEmail(req.GetEmail()); err != nil {
violations = append(violations, fieldViolation("email", err))
}
return
Expand Down
75 changes: 75 additions & 0 deletions gapi/rpc_update_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package gapi

import (
"context"
"database/sql"
"time"

db "github.com/aradwann/eenergy/db/store"
"github.com/aradwann/eenergy/pb"
"github.com/aradwann/eenergy/util"
"github.com/aradwann/eenergy/val"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (server *Server) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserResponse, error) {
violations := validateUpdateUserRequest(req)
if violations != nil {
return nil, invalidArgumentError(violations)
}

arg := db.UpdateUserParams{
Username: req.GetUsername(),
FullName: sql.NullString{String: req.GetFullName(), Valid: req.FullName != nil},
Email: sql.NullString{String: req.GetEmail(), Valid: req.Email != nil},
}

if req.Password != nil {
hashedPassword, err := util.HashPassword(req.GetPassword())
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to hash password: %s", err)
}
arg.HashedPassword = sql.NullString{
String: hashedPassword,
Valid: true,
}
arg.PasswordChangedAt = sql.NullTime{Time: time.Now(), Valid: true}
}

user, err := server.store.UpdateUser(ctx, arg)
if err != nil {
if err == sql.ErrNoRows {
return nil, status.Errorf(codes.NotFound, "user not found")
}
return nil, status.Errorf(codes.Internal, "failed to Update 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.Password != nil {
if err := val.ValidatePassword(req.GetPassword()); err != nil {
violations = append(violations, fieldViolation("password", 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))
}
}
return
}
2 changes: 1 addition & 1 deletion val/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
isValidUsername = regexp.MustCompile(`^[a-z0-9_]+$`).MatchString
isValidFullName = regexp.MustCompile(`^[a-zA-Z_\\s]+$`).MatchString
isValidFullName = regexp.MustCompile(`^[a-zA-Z\s]+$`).MatchString
)

func ValidateString(value string, minLength, maxLength int) error {
Expand Down

0 comments on commit cdc4242

Please sign in to comment.