Skip to content

Commit

Permalink
users friendship creating
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Dec 2, 2024
1 parent fafa37c commit b62f589
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 72 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 13 additions & 0 deletions internal/core/generic/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 4 additions & 2 deletions internal/pkg/resolvers/auth.resolvers.go

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

5 changes: 2 additions & 3 deletions internal/pkg/resolvers/resolver.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package graph

import (
"app/internal/pkg/users/auth"
"app/internal/pkg/users/friendship"
"app/internal/pkg/users/user"
)
Expand All @@ -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
}
16 changes: 7 additions & 9 deletions internal/pkg/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
//}
28 changes: 23 additions & 5 deletions internal/pkg/users/friendship/ctrl/friendship.ctrl.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
12 changes: 3 additions & 9 deletions internal/pkg/users/friendship/schema/friendship.graphqls
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
# Friendship Type
type Friendship {
id: ID!
userId: ID!
friendId: ID!
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!
}

Expand All @@ -39,14 +34,13 @@ input RemoveFriendshipInput {
friendshipId: ID!
}

# Friendship Queries
extend type Query{
friendshipList(filter: FriendshipFilter): PaginatedFriendshipList!
}

# 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
}
39 changes: 25 additions & 14 deletions internal/pkg/users/friendship/svc/friendship.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion internal/pkg/users/user/schema/user.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
22 changes: 0 additions & 22 deletions internal/pkg/users/user/svc/user.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b62f589

Please sign in to comment.