Skip to content

Commit

Permalink
Merge pull request #5 from cukhoaimon/hot_fix
Browse files Browse the repository at this point in the history
Hot fix
  • Loading branch information
cukhoaimon authored Feb 15, 2024
2 parents 8736077 + 1d01ff3 commit 126e134
Show file tree
Hide file tree
Showing 22 changed files with 777 additions and 78 deletions.
63 changes: 62 additions & 1 deletion docs/swagger/simplebank.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"swagger": "2.0",
"info": {
"title": "SimpleBank",
"version": "1.0",
"version": "1.1",
"contact": {
"name": "Nguyen Hoang Phuc",
"url": "https://github.com/cukhoaimon",
Expand All @@ -23,6 +23,8 @@
"paths": {
"/api/v1/user": {
"post": {
"summary": "Create new user",
"description": "Use this API to create user",
"operationId": "SimpleBank_CreateUser",
"responses": {
"200": {
Expand Down Expand Up @@ -51,10 +53,44 @@
"tags": [
"SimpleBank"
]
},
"patch": {
"summary": "Update user",
"description": "Use this API to update user",
"operationId": "SimpleBank_UpdateUser",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/pbUpdateUserResponse"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/pbUpdateUserRequest"
}
}
],
"tags": [
"SimpleBank"
]
}
},
"/api/v1/user/login": {
"post": {
"summary": "login user",
"description": "Use this API to login user",
"operationId": "SimpleBank_LoginUser",
"responses": {
"200": {
Expand Down Expand Up @@ -148,6 +184,31 @@
}
}
},
"pbUpdateUserRequest": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"fullName": {
"type": "string"
},
"email": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"pbUpdateUserResponse": {
"type": "object",
"properties": {
"user": {
"$ref": "#/definitions/pbUser"
}
}
},
"pbUser": {
"type": "object",
"properties": {
Expand Down
2 changes: 2 additions & 0 deletions internal/delivery/grpc/gapi/handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package gapi

import (
"github.com/cukhoaimon/SimpleBank/internal/delivery/grpc/pb"
db "github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc"
"github.com/cukhoaimon/SimpleBank/pkg/token"
"github.com/cukhoaimon/SimpleBank/utils"
)

type Handler struct {
pb.UnimplementedSimpleBankServer
Config utils.Config
TokenMaker token.Maker
Store db.Store
Expand Down
85 changes: 85 additions & 0 deletions internal/delivery/grpc/gapi/rpc_update_user.go
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
}
16 changes: 9 additions & 7 deletions internal/delivery/grpc/pb/rpc_create_user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions internal/delivery/grpc/pb/rpc_login_user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 126e134

Please sign in to comment.