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

側の実装 #73

Merged
merged 6 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
61 changes: 61 additions & 0 deletions backend/adaptor/service_server/converter/user_converter.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
76 changes: 76 additions & 0 deletions backend/adaptor/service_server/user_handler.go
Original file line number Diff line number Diff line change
@@ -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
}

47 changes: 47 additions & 0 deletions backend/domain/model/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package model

// This event is used to notify the server or client side
// when data changes are made.
const (
UserCreated = iota
UserUpdated
UserDeleted
UserOnline
UserOffline

UserGroupCreated
UserGroupUpdated
UserGroupDeleted

MemberAdded
MemberUpdated
MemberRemoved

ChannelCreated
ChannelUpdated
ChannelDeleted

MessageChannelJoined
MessageChannelLeaved

VoiceChannelJoined
VoiceChannelLeaved

ChannelEventCreated
ChannelEventUpdated
ChannelEventDeleted
ChannelEventStamped
ChannelEventUnstamped

MessageCreated
MessageUpdated
MessageDeleted
MessageStamped
MessageUnstamped
MessagePinned
MessageUnpinned

StampCreated
StampUpdated
StampDeleted
)
31 changes: 31 additions & 0 deletions backend/domain/model/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package model

const (
STATUS_ONLINE Status = 0
STATUS_OFFLINE Status = 1
)

type User struct {
UserID int64
UserName string
Nick string
RealName string
Password [64]byte
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
}

2 changes: 1 addition & 1 deletion backend/driver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *Server) StartServer() error {
user := pbUser.UnimplementedUserServiceServer{}
userGroup := pbUserGroup.UnimplementedUserGroupServiceServer{}
memberGroup := pbUserGroup.UnimplementedMemberServiceServer{}
// stamp := stamppb.
// stamp := stamppb.
message := pbMessage.UnimplementedMessageServiceServer{}
pinMessage := pbMessage.UnimplementedPinMessageServiceServer{}
event := pbEvent.UnimplementedEventServiceServer{}
Expand Down
7 changes: 7 additions & 0 deletions backend/logic/bto/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package bto
1uf3 marked this conversation as resolved.
Show resolved Hide resolved

type CreateUserRequest struct {
Username string
Email string
Password string
}
77 changes: 77 additions & 0 deletions backend/logic/interactor/user_interactor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package interactor

import (
"fmt"
"context"
"crypto/sha512"

"github.com/ritscc/Snack/domain/model"
"github.com/ritscc/Snack/logic/bto"
)

type UserInteractor struct {
Users []*model.User
Counter int64
}

func (interactor *UserInteractor) CreateUser(ctx context.Context, req *bto.CreateUserRequest) error {
if interactor == nil {
return fmt.Errorf("UserInteractor is nil")
}

newUser := &model.User{
UserID: interactor.Counter,
UserName: req.Username,
RitsEmail: req.Email,
Password: sha512.Sum512([]byte(req.Password)),
}

interactor.Users = append(interactor.Users, newUser)

interactor.Counter++

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{}

for _, u := range interactor.Users {
if userID == u.UserID {
user = u
break
}
}

return user, nil
}

func (interactor *UserInteractor) UpdateUser(ctx context.Context,user *model.User) error {
if interactor == nil {
return fmt.Errorf("UserInteractor is nil")
}

for _, u := range interactor.Users {
if user.UserID == u.UserID {
u = user
return nil
}
}

return fmt.Errorf("Not such a user found")
}

func (interactor *UserInteractor) DeleteUser(context.Context) error {
if interactor == nil {
return fmt.Errorf("UserInteractor is nil")
}

// Get from jwt user token

return nil
}

2 changes: 1 addition & 1 deletion backend/pb/authentication/v1/authentication.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 backend/pb/channel/v1/channel.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 backend/pb/event/v1/event.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 backend/pb/message/v1/message.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 backend/pb/stamp/v1/stamp.pb.go

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

Loading