Skip to content

Commit

Permalink
add grpc api to update user (techschool#58)
Browse files Browse the repository at this point in the history
Co-authored-by: phamlequang <phamlequang@gmail.com>
  • Loading branch information
techschool and phamlequang authored Sep 4, 2022
1 parent 23e1391 commit c335fd5
Show file tree
Hide file tree
Showing 15 changed files with 621 additions and 59 deletions.
1 change: 1 addition & 0 deletions db/query/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ WHERE username = $1 LIMIT 1;
UPDATE users
SET
hashed_password = COALESCE(sqlc.narg(hashed_password), hashed_password),
password_changed_at = COALESCE(sqlc.narg(password_changed_at), password_changed_at),
full_name = COALESCE(sqlc.narg(full_name), full_name),
email = COALESCE(sqlc.narg(email), email)
WHERE
Expand Down
17 changes: 10 additions & 7 deletions db/sqlc/user.sql.go

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

2 changes: 1 addition & 1 deletion doc/statik/statik.go

Large diffs are not rendered by default.

61 changes: 60 additions & 1 deletion doc/swagger/simple_bank.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"swagger": "2.0",
"info": {
"title": "Simple Bank API",
"version": "1.1",
"version": "1.2",
"contact": {
"name": "Tech School",
"url": "https://github.com/techschool",
Expand Down Expand Up @@ -88,6 +88,40 @@
"SimpleBank"
]
}
},
"/v1/update_user": {
"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"
]
}
}
},
"definitions": {
Expand Down Expand Up @@ -152,6 +186,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
92 changes: 92 additions & 0 deletions gapi/rpc_update_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package gapi

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

db "github.com/techschool/simplebank/db/sqlc"
"github.com/techschool/simplebank/pb"
"github.com/techschool/simplebank/util"
"github.com/techschool/simplebank/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) {
// TODO: add authorization

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 violations
}
2 changes: 1 addition & 1 deletion pb/rpc_create_user.pb.go

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

2 changes: 1 addition & 1 deletion 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 c335fd5

Please sign in to comment.