From b62f589dca4e7987948c7792d6e77ce788f9f6fd Mon Sep 17 00:00:00 2001 From: pieceowater Date: Tue, 3 Dec 2024 01:46:17 +0500 Subject: [PATCH] users friendship creating --- go.mod | 2 +- go.sum | 8 +--- internal/core/generic/pagination.go | 13 +++++++ internal/pkg/resolvers/auth.resolvers.go | 6 ++- internal/pkg/resolvers/resolver.go | 5 +-- internal/pkg/router.go | 16 ++++---- .../users/friendship/ctrl/friendship.ctrl.go | 28 ++++++++++--- .../friendship/schema/friendship.graphqls | 12 ++---- .../users/friendship/svc/friendship.svc.go | 39 ++++++++++++------- internal/pkg/users/user/schema/user.graphqls | 2 +- internal/pkg/users/user/svc/user.svc.go | 22 ----------- 11 files changed, 81 insertions(+), 72 deletions(-) diff --git a/go.mod b/go.mod index f13f1ef..1e51402 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.0 require ( github.com/99designs/gqlgen v0.17.56 github.com/joho/godotenv v1.5.1 - github.com/pieceowater-dev/lotof.hub.proto v0.0.22 + github.com/pieceowater-dev/lotof.hub.proto v0.0.24 github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.6 github.com/vektah/gqlparser/v2 v2.5.19 google.golang.org/grpc v1.68.0 diff --git a/go.sum b/go.sum index fc7f3d7..9bdc6e7 100644 --- a/go.sum +++ b/go.sum @@ -89,12 +89,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= -github.com/pieceowater-dev/lotof.hub.proto v0.0.22 h1:jGDLAACixTVetj08UvT6GQszKyw1P8OL9w95qBwmWNQ= -github.com/pieceowater-dev/lotof.hub.proto v0.0.22/go.mod h1:9uwfvIUGGzTrTIVzQ4gH8hmrRC8sgtnLUhyPN5swIME= -github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.3 h1:YqSOy95Tzw80/MFCRmJwhhOWD2JrAC9uzjbL9yNepSQ= -github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.3/go.mod h1:m/C+3z+Y2n9FPnakJl7jOl/4T1KfrE2/OhSslRKAGGc= -github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.5 h1:NbpDkHMW4VFRAxuTKaEvvEgizt4z7Nkjbzg/m3uYKQI= -github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.5/go.mod h1:m/C+3z+Y2n9FPnakJl7jOl/4T1KfrE2/OhSslRKAGGc= +github.com/pieceowater-dev/lotof.hub.proto v0.0.24 h1:ULHeaE5zOaulaSfgbl+U1wYat+PGYiNayJZpYnxuaC8= +github.com/pieceowater-dev/lotof.hub.proto v0.0.24/go.mod h1:9uwfvIUGGzTrTIVzQ4gH8hmrRC8sgtnLUhyPN5swIME= github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.6 h1:5WEnZAd/hwMDAL8sVUoL+zO4wWeQetVO4Zo+NgxzC80= github.com/pieceowater-dev/lotof.lib.gossiper/v2 v2.0.6/go.mod h1:m/C+3z+Y2n9FPnakJl7jOl/4T1KfrE2/OhSslRKAGGc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/internal/core/generic/pagination.go b/internal/core/generic/pagination.go index 5e7ca38..33ed685 100644 --- a/internal/core/generic/pagination.go +++ b/internal/core/generic/pagination.go @@ -46,3 +46,16 @@ func PaginationLengthToInt(length model.FilterPaginationLengthEnum) int32 { return 10 } } + +func IntToFriendshipStatus(stint int) model.FriendshipStatus { + switch stint { + case 100: + return model.FriendshipStatusPending + case 200: + return model.FriendshipStatusAccepted + case 300: + return model.FriendshipStatusRejected + default: + return model.FriendshipStatusPending + } +} diff --git a/internal/pkg/resolvers/auth.resolvers.go b/internal/pkg/resolvers/auth.resolvers.go index 81ebf2c..28c60a9 100644 --- a/internal/pkg/resolvers/auth.resolvers.go +++ b/internal/pkg/resolvers/auth.resolvers.go @@ -11,10 +11,12 @@ import ( // Login is the resolver for the login field. func (r *mutationResolver) Login(ctx context.Context, input model.LoginRequest) (*model.AuthResponse, error) { - return r.AuthProvider.API.Login(input) + //return r.AuthProvider.API.Login(input) + return &model.AuthResponse{}, nil } // Register is the resolver for the register field. func (r *mutationResolver) Register(ctx context.Context, input model.RegisterRequest) (*model.AuthResponse, error) { - return r.AuthProvider.API.Register(input) + //return r.AuthProvider.API.Register(input) + return &model.AuthResponse{}, nil } diff --git a/internal/pkg/resolvers/resolver.go b/internal/pkg/resolvers/resolver.go index 5952d19..03c462a 100644 --- a/internal/pkg/resolvers/resolver.go +++ b/internal/pkg/resolvers/resolver.go @@ -1,7 +1,6 @@ package graph import ( - "app/internal/pkg/users/auth" "app/internal/pkg/users/friendship" "app/internal/pkg/users/user" ) @@ -11,7 +10,7 @@ import ( // It serves as dependency injection for your app, add any dependencies you require here. type Resolver struct { - UserProvider user.Module - AuthProvider auth.Module + UserProvider user.Module + //AuthProvider auth.Module FriendshipProvider friendship.Module } diff --git a/internal/pkg/router.go b/internal/pkg/router.go index de7ea9b..39d88f1 100644 --- a/internal/pkg/router.go +++ b/internal/pkg/router.go @@ -2,10 +2,8 @@ package pkg import ( resolvers "app/internal/pkg/resolvers" - "app/internal/pkg/users/auth" "app/internal/pkg/users/friendship" "app/internal/pkg/users/user" - "google.golang.org/grpc" ) type Router struct{} @@ -16,15 +14,15 @@ func NewRouter() *Router { func (r *Router) Init() *resolvers.Resolver { return &resolvers.Resolver{ - UserProvider: user.NewUserModule(), - AuthProvider: auth.NewAuthModule(), + UserProvider: user.NewUserModule(), + //AuthProvider: auth.NewAuthModule(), FriendshipProvider: friendship.NewFriendshipModule(), } } // if this gateway serves as grpc server somehow uncomment below -// InitGRPC initializes gRPC routes -func (r *Router) InitGRPC(grpcServer *grpc.Server) { - // Register gRPC services - //pb.RegisterUserServiceServer(grpcServer, r.userModule.Controller) -} +//// InitGRPC initializes gRPC routes +//func (r *Router) InitGRPC(grpcServer *grpc.Server) { +// // Register gRPC services +// //pb.RegisterUserServiceServer(grpcServer, r.userModule.Controller) +//} diff --git a/internal/pkg/users/friendship/ctrl/friendship.ctrl.go b/internal/pkg/users/friendship/ctrl/friendship.ctrl.go index 7941f25..0dd2952 100644 --- a/internal/pkg/users/friendship/ctrl/friendship.ctrl.go +++ b/internal/pkg/users/friendship/ctrl/friendship.ctrl.go @@ -1,8 +1,11 @@ package ctrl import ( + "app/internal/core/generic" "app/internal/core/graph/model" + fr "app/internal/core/grpc/generated" "app/internal/pkg/users/friendship/svc" + "log" ) type FriendshipController struct { @@ -14,21 +17,36 @@ func NewFriendshipController(service *svc.FriendshipService) *FriendshipControll } func (c *FriendshipController) CreateFriendship(input model.CreateFriendshipInput) (*model.Friendship, error) { - //return c.friendshipService.CreateFriendship(ctx, input) - return nil, nil + request := &fr.CreateFriendshipInput{ + UserId: input.UserID, + FriendId: input.FriendID, + } + + createFriendship, err := c.friendshipService.CreateFriendship(request) + if err != nil { + log.Printf("Error creating createFriendship: %v", err) + return nil, err + } + + return &model.Friendship{ + ID: createFriendship.Id, + UserID: createFriendship.User.Id, + FriendID: createFriendship.Friend.Id, + Status: generic.IntToFriendshipStatus(int(createFriendship.Status)), + }, nil } func (c *FriendshipController) AcceptFriendshipRequest(input model.AcceptFriendshipInput) (*model.Friendship, error) { - //return c.friendshipService.AcceptFriendshipRequest(ctx, input) + //return c.friendshipService.AcceptFriendshipRequest(input) return nil, nil } func (c *FriendshipController) RemoveFriendship(input model.RemoveFriendshipInput) (bool, error) { - //return c.friendshipService.RemoveFriendship(ctx, input) + //return c.friendshipService.RemoveFriendship(input) return true, nil } func (c *FriendshipController) FriendshipList(filter *model.FriendshipFilter) (*model.PaginatedFriendshipList, error) { - //return c.friendshipService.FriendshipList(ctx, filter) + //return c.friendshipService.FriendshipList(filter) return nil, nil } diff --git a/internal/pkg/users/friendship/schema/friendship.graphqls b/internal/pkg/users/friendship/schema/friendship.graphqls index 308091c..f56b48f 100644 --- a/internal/pkg/users/friendship/schema/friendship.graphqls +++ b/internal/pkg/users/friendship/schema/friendship.graphqls @@ -1,4 +1,3 @@ -# Friendship Type type Friendship { id: ID! userId: ID! @@ -6,28 +5,24 @@ type Friendship { status: FriendshipStatus! } -# Friendship Request Filter input FriendshipFilter { userId: ID! status: FriendshipStatus } -# Friendship Status Enum enum FriendshipStatus { PENDING ACCEPTED REJECTED } -# Friendship Paginated List type PaginatedFriendshipList { rows: [Friendship!]! info: PaginationInfo! } -# Input Types for Friendship Mutations input CreateFriendshipInput { - userId: ID! + userId: ID! #todo: dont forget to verify userId with user auth token friendId: ID! } @@ -39,7 +34,6 @@ input RemoveFriendshipInput { friendshipId: ID! } -# Friendship Queries extend type Query{ friendshipList(filter: FriendshipFilter): PaginatedFriendshipList! } @@ -47,6 +41,6 @@ extend type Query{ # Friendship Mutations extend type Mutation { createFriendship(input: CreateFriendshipInput!): Friendship! - acceptFriendshipRequest(input: AcceptFriendshipInput!): Friendship! - removeFriendship(input: RemoveFriendshipInput!): Boolean! + acceptFriendshipRequest(input: AcceptFriendshipInput!): Friendship! #makes FriendshipStatus eq ACCEPTED, only user that's eq friendId + removeFriendship(input: RemoveFriendshipInput!): Boolean! #soft deletes Friendship } \ No newline at end of file diff --git a/internal/pkg/users/friendship/svc/friendship.svc.go b/internal/pkg/users/friendship/svc/friendship.svc.go index e67f703..9269dcd 100644 --- a/internal/pkg/users/friendship/svc/friendship.svc.go +++ b/internal/pkg/users/friendship/svc/friendship.svc.go @@ -3,14 +3,16 @@ package svc import ( "app/internal/core/cfg" "app/internal/core/graph/model" - "app/internal/core/grpc/generated" + fr "app/internal/core/grpc/generated" "context" + "errors" gossiper "github.com/pieceowater-dev/lotof.lib.gossiper/v2" "log" ) type FriendshipService struct { - client generated.FriendshipServiceClient + transport gossiper.Transport + client fr.FriendshipServiceClient } func NewFriendshipService() *FriendshipService { @@ -20,34 +22,43 @@ func NewFriendshipService() *FriendshipService { cfg.Inst().LotofHubMSvcUsersGrpcAddress, ) - clientConstructor := generated.NewFriendshipServiceClient + clientConstructor := fr.NewFriendshipServiceClient client, err := grpcTransport.CreateClient(clientConstructor) if err != nil { log.Fatalf("Error creating client: %v", err) } return &FriendshipService{ - client: client.(generated.FriendshipServiceClient), + transport: grpcTransport, + client: client.(fr.FriendshipServiceClient), } } -func (s *FriendshipService) CreateFriendship(ctx context.Context, input *model.CreateFriendshipInput) (*model.Friendship, error) { - //return s.client.CreateFriendship(ctx, input) - return nil, nil +func (s *FriendshipService) CreateFriendship(input *fr.CreateFriendshipInput) (*fr.Friendship, error) { + ctx := context.Background() + + response, err := s.transport.Send(ctx, s.client, "CreateFriendship", input) + if err != nil { + log.Printf("Error sending request: %v", err) + return nil, err + } + + res, ok := response.(*fr.Friendship) + if !ok { + return nil, errors.New("invalid response type from gRPC transport") + } + + return res, nil } -func (s *FriendshipService) AcceptFriendshipRequest(ctx context.Context, input *model.AcceptFriendshipInput) (*model.Friendship, error) { - //return s.client.AcceptFriendshipRequest(ctx, input) +func (s *FriendshipService) AcceptFriendshipRequest(input *model.AcceptFriendshipInput) (*model.Friendship, error) { return nil, nil } -func (s *FriendshipService) RemoveFriendship(ctx context.Context, input *model.RemoveFriendshipInput) error { - //_, err := s.client.RemoveFriendshipRequest(ctx, input) - //return err +func (s *FriendshipService) RemoveFriendship(input *model.RemoveFriendshipInput) error { return nil } -func (s *FriendshipService) FriendshipList(ctx context.Context, filter *model.FriendshipFilter) (*model.PaginatedFriendshipList, error) { - //return s.client.FriendshipRequestList(ctx, filter) +func (s *FriendshipService) FriendshipList(filter *model.FriendshipFilter) (*model.PaginatedFriendshipList, error) { return nil, nil } diff --git a/internal/pkg/users/user/schema/user.graphqls b/internal/pkg/users/user/schema/user.graphqls index 6c33931..c86685c 100644 --- a/internal/pkg/users/user/schema/user.graphqls +++ b/internal/pkg/users/user/schema/user.graphqls @@ -21,6 +21,6 @@ extend type Query { } extend type Mutation { - createUser(input: UserInput!): User! + createUser(input: UserInput!): User! #todo: restrict this method for usual users later. they create itself via registration updateUser(id: ID!, input: UserInput!): User! } \ No newline at end of file diff --git a/internal/pkg/users/user/svc/user.svc.go b/internal/pkg/users/user/svc/user.svc.go index c2a2fe6..f47e782 100644 --- a/internal/pkg/users/user/svc/user.svc.go +++ b/internal/pkg/users/user/svc/user.svc.go @@ -109,25 +109,3 @@ func (s *UserService) UpdateUser(input *user.UpdateUserRequest) (*user.User, err return res, nil } - -// DeleteUser deletes a user by ID and returns raw gRPC response. -func (s *UserService) DeleteUser(id string) (*user.DeleteUserResponse, error) { - ctx := context.Background() - - request := &user.DeleteUserRequest{ - Id: id, - } - - response, err := s.transport.Send(ctx, s.client, "DeleteUser", request) - if err != nil { - log.Printf("Error sending request: %v", err) - return nil, err - } - - res, ok := response.(*user.DeleteUserResponse) - if !ok { - return nil, errors.New("invalid response type from gRPC transport") - } - - return res, nil -}