From 7158ac1c9d87207121b874d342741205ef26243b Mon Sep 17 00:00:00 2001 From: 1uf3 Date: Fri, 9 Jun 2023 18:47:00 +0900 Subject: [PATCH] =?UTF-8?q?UserService=E3=81=AEinteractor=E3=81=A8handler?= =?UTF-8?q?=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/user_converter.go | 61 +++++++++++++++ .../adaptor/service_server/user_handler.go | 76 +++++++++++++++++++ backend/domain/model/user.go | 37 +++++++++ backend/logic/bto/request.go | 7 ++ backend/logic/interactor/user_interactor.go | 44 +++++++++++ 5 files changed, 225 insertions(+) create mode 100644 backend/adaptor/service_server/converter/user_converter.go create mode 100644 backend/adaptor/service_server/user_handler.go create mode 100644 backend/domain/model/user.go create mode 100644 backend/logic/bto/request.go create mode 100644 backend/logic/interactor/user_interactor.go diff --git a/backend/adaptor/service_server/converter/user_converter.go b/backend/adaptor/service_server/converter/user_converter.go new file mode 100644 index 0000000..97e979e --- /dev/null +++ b/backend/adaptor/service_server/converter/user_converter.go @@ -0,0 +1,61 @@ +package converter + +import ( + "github.com/ritscc/Snack/domain/model" + pbUser "github.com/ritscc/Snack/pb/user/v1" + + "github.com/ritscc/Snack/logic/bto" +) + +type UserConverter struct { +} + +func(converter UserConverter) UserTopbUser(user *model.User) *pbUser.User { + return &pbUser.User{ + UserId: user.UserID, + Username: user.UserName, + Nick: user.Nick, + RealName: user.RealName, + Avatar: user.Avatar, + Role: user.Role, + Locale: user.Locale, + RitsEmail: user.RitsEmail, + OwnEmail: &user.OwnEmail, + Comment: user.Comment, + Status: pbUser.Status(user.Status), + Services: &pbUser.Service{ + Twitter: &user.Services.Twitter, + Github: &user.Services.Github, + Discord: &user.Services.Discord, + }, + } +} + +func(converter UserConverter) PbUserToUser(user *pbUser.User) *model.User { + return &model.User{ + UserID: user.UserId, + UserName: user.Username, + Nick: user.Nick, + RealName: user.RealName, + Avatar: user.Avatar, + Role: user.Role, + Locale: user.Locale, + RitsEmail: user.RitsEmail, + OwnEmail: *user.OwnEmail, + Comment: user.Comment, + Status: model.Status(user.Status), + Services: &model.Service{ + Twitter: *user.Services.Twitter, + Github: *user.Services.Github, + Discord: *user.Services.Discord, + }, + } +} + +func(converter UserConverter) PbCreateUserRequestToCreateUserRequest(req *pbUser.CreateUserRequest) *bto.CreateUserRequest { + return &bto.CreateUserRequest{ + Username: req.Username, + Email: req.Email, + Password: req.Password, + } +} diff --git a/backend/adaptor/service_server/user_handler.go b/backend/adaptor/service_server/user_handler.go new file mode 100644 index 0000000..1be8ee4 --- /dev/null +++ b/backend/adaptor/service_server/user_handler.go @@ -0,0 +1,76 @@ +package service_server + +import ( + "context" + + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" + + "github.com/ritscc/Snack/adaptor/service_server/converter" + "github.com/ritscc/Snack/logic/interactor" + pbUser "github.com/ritscc/Snack/pb/user/v1" +) + +type UserService struct { + converter converter.UserConverter + interactor interactor.UserInteractor +} + +func InitUserService() *UserService { + return &UserService{} +} + +func (service *UserService) CreateUser(ctx context.Context, pbReq *pbUser.CreateUserRequest) (*emptypb.Empty, error) { + if service == nil { + return nil, status.Errorf(codes.Internal, "UserService is nil") + } + + req := service.converter.PbCreateUserRequestToCreateUserRequest(pbReq) + service.interactor.CreateUser(ctx, req) + res := new(emptypb.Empty) + + return res, nil +} + +func (service *UserService) GetUser(ctx context.Context, req *pbUser.GetUserRequest) (*pbUser.User, error) { + if service == nil { + return nil, status.Errorf(codes.Internal, "UserService is nil") + } + + user, err := service.interactor.GetUser(ctx, req.UserId) + if err != nil { + return nil, status.Errorf(codes.Internal, "err") + } + + pbUser := service.converter.UserTopbUser(user) + + return pbUser, nil +} + +func (service *UserService) UpdateUser(ctx context.Context, pbUser *pbUser.User) (*emptypb.Empty, error) { + if service == nil { + return nil, status.Errorf(codes.Internal, "UserService is nil") + } + + user := service.converter.PbUserToUser(pbUser) + if err := service.interactor.UpdateUser(ctx, user); err != nil { + return nil, status.Errorf(codes.Internal, "err") + } + res := new(emptypb.Empty) + + return res, nil +} + +func (service *UserService) DeleteUser(ctx context.Context, e *emptypb.Empty) (*emptypb.Empty, error) { + if service == nil { + return nil, status.Errorf(codes.Internal, "UserService is nil") + } + + service.interactor.DeleteUser(ctx) + + res := new(emptypb.Empty) + + return res, nil +} + diff --git a/backend/domain/model/user.go b/backend/domain/model/user.go new file mode 100644 index 0000000..69adedd --- /dev/null +++ b/backend/domain/model/user.go @@ -0,0 +1,37 @@ +package model + +const ( + STATUS_ONLINE Status = 0 + STATUS_OFFLINE Status = 1 +) + +type User struct { + UserID int64 + UserName string + Nick string + RealName string + Avatar string + Role string + Locale string + RitsEmail string + OwnEmail string + Comment string + Status Status + Services *Service +} + +type Status int32 + +type Service struct { + Twitter string + Github string + Discord string +} + +type UserService interface { + GetUser() + GetUsers() + CreateUser() + UpdateUser() + DeleteUser() +} diff --git a/backend/logic/bto/request.go b/backend/logic/bto/request.go new file mode 100644 index 0000000..b06fd23 --- /dev/null +++ b/backend/logic/bto/request.go @@ -0,0 +1,7 @@ +package bto + +type CreateUserRequest struct { + Username string + Email string + Password string +} diff --git a/backend/logic/interactor/user_interactor.go b/backend/logic/interactor/user_interactor.go new file mode 100644 index 0000000..41faecc --- /dev/null +++ b/backend/logic/interactor/user_interactor.go @@ -0,0 +1,44 @@ +package interactor + +import ( + "fmt" + "context" + + "github.com/ritscc/Snack/domain/model" + "github.com/ritscc/Snack/logic/bto" +) + +type UserInteractor struct { +} + +func (interactor *UserInteractor) CreateUser(context.Context, *bto.CreateUserRequest) error { + if interactor == nil { + return fmt.Errorf("UserInteractor is nil") + } + return nil +} + +func (interactor *UserInteractor) GetUser(ctx context.Context, userID int64) (*model.User, error) { + if interactor == nil { + return nil, fmt.Errorf("UserInteractor is nil") + } + + user := &model.User{} + + return user, nil +} + +func (interactor *UserInteractor) UpdateUser(context.Context, *model.User) error { + if interactor == nil { + return fmt.Errorf("UserInteractor is nil") + } + return nil +} + +func (interactor *UserInteractor) DeleteUser(context.Context) error { + if interactor == nil { + return fmt.Errorf("UserInteractor is nil") + } + return nil +} +