Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Firebase에 user 정보 저장 및 가져오기 기능 추가 #16

Merged
merged 21 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.164.0 // indirect
google.golang.org/api v0.165.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/appengine/v2 v2.0.5 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
Expand Down
57 changes: 2 additions & 55 deletions go.sum

Large diffs are not rendered by default.

43 changes: 24 additions & 19 deletions src/handler/user.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type UserHandlerImpl struct {

func (handler *UserHandlerImpl) GetUser(c echo.Context) error {
var request = pb.GetUserRequest{}

if err := c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
Expand All @@ -29,7 +30,10 @@ func (handler *UserHandlerImpl) GetUser(c echo.Context) error {
return c.JSON(http.StatusBadRequest, "Id is required")
}

user := handler.userService.GetUser(request.Id)
user, err := handler.userService.GetUser(request.Id)
if err != nil {
return c.JSON(http.StatusNotFound, "invalid id")
}
userDTO := converter.ConvertUser(user)

return c.JSON(http.StatusOK, pb.GetUserResponse{User: &userDTO})
Expand All @@ -41,27 +45,25 @@ func (handler *UserHandlerImpl) UpdateUser(c echo.Context) error {
return c.JSON(http.StatusBadRequest, err)
}

if request.Id == "" {
return c.JSON(http.StatusBadRequest, "Id is required")
}

userUpdateDTO := make(map[string]interface{})

switch {
case request.GetName() != "":
userUpdateDTO["name"] = request.GetName()
userUpdateDTO := map[string]interface{}{}

case request.GetAge() != 0:
userUpdateDTO["age"] = int(request.GetAge())

case request.GetDisorderType() != 0:
userUpdateDTO["disorderType"] = converter.ConvertDisorderType(request.GetDisorderType())
if name := request.GetName(); name != "" {
userUpdateDTO["name"] = name
}
if age := request.GetAge(); age != 0 {
userUpdateDTO["age"] = int(age)
}
if disorderType := request.GetDisorderType(); disorderType != 0 {
userUpdateDTO["disorderType"] = disorderType.Number()
}

default:
if len(userUpdateDTO) == 0 {
return c.JSON(http.StatusBadRequest, "At least one field is required")
}

if user, err := handler.userService.UpdateUser(request.Id, userUpdateDTO); err != nil {
userUpdateDTO["uid"] = request.GetId()

if user, err := handler.userService.UpdateUser(userUpdateDTO); err != nil {
return c.JSON(http.StatusBadRequest, err)
} else {
userDTO := converter.ConvertUser(user)
Expand All @@ -79,9 +81,12 @@ func (handler *UserHandlerImpl) DeleteUser(c echo.Context) error {
return c.JSON(http.StatusBadRequest, "Id is required")
}

deletedUserId := handler.userService.DeleteUser(request.Id)
err := handler.userService.DeleteUser(request.Id)
if err != nil {
return c.JSON(http.StatusNotFound, "invalid id")
}

return c.JSON(http.StatusOK, pb.DeleteUserResponse{Id: deletedUserId})
return c.JSON(http.StatusOK, pb.DeleteUserResponse{Id: request.Id})
}

func UserHandlerInit(userService service.UserService) *UserHandlerImpl {
Expand Down
79 changes: 54 additions & 25 deletions src/service/user.service.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,88 @@
package service

import (
"context"

"firebase.google.com/go/v4/auth"
"github.com/gdsc-ys/fluentify-server/src/model"
)

type UserService interface {
GetUser(id string) model.User
UpdateUser(id string, updateUserDTO map[string]interface{}) (model.User, error)
DeleteUser(id string) string
GetUser(uid string) (model.User, error)
UpdateUser(updateUserDTO map[string]interface{}) (model.User, error)
DeleteUser(uid string) error
}

type UserServiceImpl struct {
authClient *auth.Client
}

func (service *UserServiceImpl) GetUser(id string) model.User {
dummyUser := model.User{
Id: "fake",
Name: "fake",
Age: 1,
DisorderType: model.DISORDER_TYPE_HEARING,
func (service *UserServiceImpl) GetUser(uid string) (model.User, error) {
userRecord, err := service.authClient.GetUser(context.Background(), uid)
if err != nil {
return model.User{}, err
}

return dummyUser
user := service.convertRecordToUser(userRecord)
return user, nil
}

func (service *UserServiceImpl) UpdateUser(id string, updateUserDTO map[string]interface{}) (model.User, error) {
func (service *UserServiceImpl) UpdateUser(updateUserDTO map[string]interface{}) (model.User, error) {

dummyUser := model.User{
Id: "fake",
Name: "fake",
Age: 1,
DisorderType: model.DISORDER_TYPE_HEARING,
}
ctx := context.Background()
uid := updateUserDTO["uid"].(string)
params := &auth.UserToUpdate{}
customClaims := make(map[string]interface{})

for field, value := range updateUserDTO {
switch field {
case "name":
dummyUser.Name = value.(string)
params = params.DisplayName(value.(string))
case "age":
dummyUser.Age = value.(int)
if dummyUser.Age < 0 {
return dummyUser, &model.UserValidationError{Message: "Age must be greater than 0"}
if value.(int) < 0 {
return model.User{}, &model.UserValidationError{Message: "Age must be greater than 0"}
}
customClaims["age"] = value.(int)
case "disorderType":
dummyUser.DisorderType = value.(model.DisorderType)
customClaims["disorderType"] = value.(int)
}
}
params = params.CustomClaims(customClaims)

userRecord, err := service.authClient.UpdateUser(ctx, uid, params)
if err != nil {
return model.User{}, err
}

user := service.convertRecordToUser(userRecord)
return user, nil
}

return dummyUser, nil
func (service *UserServiceImpl) DeleteUser(uid string) error {
ctx := context.Background()
err := service.authClient.DeleteUser(ctx, uid)
if err != nil {
return err
}
return nil
}

func (service *UserServiceImpl) DeleteUser(id string) string {
return id
func (service *UserServiceImpl) convertRecordToUser(record *auth.UserRecord) model.User {
getAge := func() int {
if age, ok := record.CustomClaims["age"].(float64); ok {
return int(age)
}
return 0
}

user := model.User{
Id: record.UID,
Name: record.DisplayName,
Age: getAge(),
DisorderType: 0,
}

return user
}

func UserServiceInit(authClient *auth.Client) *UserServiceImpl {
Expand Down

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

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

Loading
Loading