Skip to content

Commit

Permalink
namespaces ns ctrl
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Dec 8, 2024
1 parent 5a68bad commit e7a3559
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 45 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.26
github.com/pieceowater-dev/lotof.hub.proto v0.0.32
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +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.26 h1:e1jGTiV5QFkd8bpUjw8vSjeMA7cPbEoISGUtSqzrIro=
github.com/pieceowater-dev/lotof.hub.proto v0.0.26/go.mod h1:9uwfvIUGGzTrTIVzQ4gH8hmrRC8sgtnLUhyPN5swIME=
github.com/pieceowater-dev/lotof.hub.proto v0.0.32 h1:5w/rZ0H6Dee3zdm6WJjIa51A8IkH3ui708xzbvY0mYY=
github.com/pieceowater-dev/lotof.hub.proto v0.0.32/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
17 changes: 9 additions & 8 deletions internal/pkg/namespaces/member/svc/member.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package svc

import (
"app/internal/core/cfg"
//member "app/internal/core/grpc/generated"
member "app/internal/core/grpc/generated"
gossiper "github.com/pieceowater-dev/lotof.lib.gossiper/v2"
"log"
)

type MemberService struct {
transport gossiper.Transport
//client member.MemberServiceClient // gRPC client
client member.MemberServiceClient // gRPC client
}

func NewMemberService() *MemberService {
Expand All @@ -19,14 +20,14 @@ func NewMemberService() *MemberService {
)

// Create the client once and store it as a property
//clientConstructor := member.NewMemberServiceClient
//client, err := grpcTransport.CreateClient(clientConstructor)
//if err != nil {
// log.Fatalf("Error creating client: %v", err)
//}
clientConstructor := member.NewMemberServiceClient
client, err := grpcTransport.CreateClient(clientConstructor)
if err != nil {
log.Fatalf("Error creating client: %v", err)
}

return &MemberService{
transport: grpcTransport,
//client: client.(member.MemberServiceClient), // Cast to the correct type
client: client.(member.MemberServiceClient), // Cast to the correct type
}
}
123 changes: 105 additions & 18 deletions internal/pkg/namespaces/ns/ctrl/ns.ctrl.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ctrl

import (
"app/internal/core/generic"
"app/internal/core/graph/model"
ns "app/internal/core/grpc/generated"
"app/internal/pkg/namespaces/ns/svc"
"context"
"log"
)

type NSController struct {
Expand All @@ -18,14 +21,22 @@ func (c *NSController) CreateNamespace(
_ context.Context,
input model.NamespaceInput,
) (*model.Namespace, error) {
request := &ns.NamespaceInput{
Title: input.Title,
Slug: input.Slug,
Description: *input.Description,
}
res, err := c.nsService.CreateNamespace(request)
if err != nil {
return nil, err
}

return &model.Namespace{
ID: "",
Title: "",
Slug: "",
Description: nil,
Owner: "",
Services: nil,
Members: nil,
ID: res.Namespace.Id,
Title: res.Namespace.Title,
Slug: res.Namespace.Slug,
Description: &res.Namespace.Description,
Owner: res.Namespace.Owner,
}, nil
}

Expand All @@ -34,37 +45,113 @@ func (c *NSController) UpdateNamespace(
id string,
input model.NamespaceInput,
) (*model.Namespace, error) {
request := &ns.NamespaceMutationRequest{
Id: id,
Input: &ns.NamespaceInput{
Title: input.Title,
Slug: input.Slug,
Description: *input.Description,
},
}

res, err := c.nsService.UpdateNamespace(request)
if err != nil {
return nil, err
}

return &model.Namespace{
ID: "",
Title: "",
Slug: "",
Description: nil,
Owner: "",
Services: nil,
Members: nil,
ID: res.Namespace.Id,
Title: res.Namespace.Title,
Slug: res.Namespace.Slug,
Description: &res.Namespace.Description,
Owner: res.Namespace.Owner,
}, nil
}

func (c *NSController) DeleteNamespace(
_ context.Context,
id string,
) (bool, error) {
return false, nil
request := &ns.DeleteNamespaceRequest{
Id: id,
}

res, err := c.nsService.DeleteNamespace(request)
if err != nil {
return false, err
}

return res.Success, nil
}

func (c *NSController) Namespaces(
_ context.Context,
filter *model.DefaultFilterInput,
) (*model.PaginatedNamespaceList, error) {
request := &ns.NamespacesRequest{
Search: "",
Pagination: &ns.Pagination{},
Sort: &ns.Sort{},
}

if filter.Search != nil {
request.Search = *filter.Search
}
if filter.Pagination != nil {
request.Pagination = &ns.Pagination{
Page: int32(*filter.Pagination.Page),
Length: generic.PaginationLengthToInt(*filter.Pagination.Length),
}
}
if filter.Sort != nil {
request.Sort = &ns.Sort{
Field: *filter.Sort.Field,
Direction: generic.SortByEnumToString(filter.Sort.By),
}
}

response, err := c.nsService.GetNamespaces(request)
if err != nil {
log.Printf("Error fetching: %v", err)
return nil, err
}

var result []*model.Namespace
for _, n := range response.Namespaces.Rows {
result = append(result, &model.Namespace{
ID: n.Id,
Title: n.Title,
Slug: n.Slug,
Description: &n.Description,
Owner: n.Owner,
})
}

return &model.PaginatedNamespaceList{
Rows: []*model.Namespace{},
Info: nil,
Rows: result,
Info: &model.PaginationInfo{
Count: int(response.Namespaces.Info.Count),
},
}, nil
}

func (c *NSController) Namespace(
_ context.Context,
id string,
) (*model.Namespace, error) {
return nil, nil
request := &ns.NamespaceRequest{
Id: id,
}
res, err := c.nsService.GetNamespace(request)
if err != nil {
return nil, err
}

return &model.Namespace{
ID: res.Namespace.Id,
Title: res.Namespace.Title,
Slug: res.Namespace.Slug,
Description: &res.Namespace.Description,
Owner: res.Namespace.Owner,
}, nil
}
92 changes: 84 additions & 8 deletions internal/pkg/namespaces/ns/svc/ns.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package svc

import (
"app/internal/core/cfg"
//ns "app/internal/core/grpc/generated"
ns "app/internal/core/grpc/generated"
"context"
"errors"
gossiper "github.com/pieceowater-dev/lotof.lib.gossiper/v2"
"log"
)

type NSService struct {
transport gossiper.Transport
//client ns.NamespaceServiceClient // gRPC client
client ns.NamespaceServiceClient
}

func NewNSService() *NSService {
Expand All @@ -19,14 +22,87 @@ func NewNSService() *NSService {
)

// Create the client once and store it as a property
//clientConstructor := ns.NewNamespaceServiceClient
//client, err := grpcTransport.CreateClient(clientConstructor)
//if err != nil {
// log.Fatalf("Error creating client: %v", err)
//}
clientConstructor := ns.NewNamespaceServiceClient
client, err := grpcTransport.CreateClient(clientConstructor)
if err != nil {
log.Fatalf("Error creating client: %v", err)
}

return &NSService{
transport: grpcTransport,
//client: client.(ns.NamespaceServiceClient), // Cast to the correct type
client: client.(ns.NamespaceServiceClient),
}
}

func (s *NSService) GetNamespaces(input *ns.NamespacesRequest) (*ns.NamespacesResponse, error) {
ctx := context.Background()
response, err := s.transport.Send(ctx, s.client, "GetNamespaces", input)
if err != nil {
log.Printf("Error sending request: %v", err)
return nil, err
}
res, ok := response.(*ns.NamespacesResponse)
if !ok {
return nil, errors.New("invalid response type from gRPC transport")
}
return res, nil
}

func (s *NSService) GetNamespace(input *ns.NamespaceRequest) (*ns.NamespaceResponse, error) {
ctx := context.Background()
response, err := s.transport.Send(ctx, s.client, "GetNamespace", input)
if err != nil {
log.Printf("Error sending request: %v", err)
return nil, err
}
res, ok := response.(*ns.NamespaceResponse)
if !ok {
return nil, errors.New("invalid response type from gRPC transport")
}
return res, nil
}

func (s *NSService) CreateNamespace(input *ns.NamespaceInput) (*ns.NamespaceMutationResponse, error) {
ctx := context.Background()

response, err := s.transport.Send(ctx, s.client, "CreateNamespace", input)
if err != nil {
log.Printf("Error sending request: %v", err)
return nil, err
}

res, ok := response.(*ns.NamespaceMutationResponse)
if !ok {
return nil, errors.New("invalid response type from gRPC transport")
}

return res, nil
}

func (s *NSService) UpdateNamespace(input *ns.NamespaceMutationRequest) (*ns.NamespaceMutationResponse, error) {
ctx := context.Background()
response, err := s.transport.Send(ctx, s.client, "UpdateNamespace", input)
if err != nil {
log.Printf("Error sending request: %v", err)
return nil, err
}
res, ok := response.(*ns.NamespaceMutationResponse)
if !ok {
return nil, errors.New("invalid response type from gRPC transport")
}
return res, nil
}

func (s *NSService) DeleteNamespace(input *ns.DeleteNamespaceRequest) (*ns.DeleteNamespaceResponse, error) {
ctx := context.Background()
response, err := s.transport.Send(ctx, s.client, "DeleteNamespace", input)
if err != nil {
log.Printf("Error sending request: %v", err)
return nil, err
}
res, ok := response.(*ns.DeleteNamespaceResponse)
if !ok {
return nil, errors.New("invalid response type from gRPC transport")
}
return res, nil
}
17 changes: 9 additions & 8 deletions internal/pkg/namespaces/service/svc/service.svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package svc

import (
"app/internal/core/cfg"
//service "app/internal/core/grpc/generated"
service "app/internal/core/grpc/generated"
gossiper "github.com/pieceowater-dev/lotof.lib.gossiper/v2"
"log"
)

type ServiceService struct {
transport gossiper.Transport
//client service.ServiceServiceClient // gRPC client
client service.ServiceServiceClient // gRPC client
}

func NewServiceService() *ServiceService {
Expand All @@ -19,14 +20,14 @@ func NewServiceService() *ServiceService {
)

// Create the client once and store it as a property
//clientConstructor := service.NewServiceServiceClient
//client, err := grpcTransport.CreateClient(clientConstructor)
//if err != nil {
// log.Fatalf("Error creating client: %v", err)
//}
clientConstructor := service.NewServiceServiceClient
client, err := grpcTransport.CreateClient(clientConstructor)
if err != nil {
log.Fatalf("Error creating client: %v", err)
}

return &ServiceService{
transport: grpcTransport,
//client: client.(service.ServiceServiceClient), // Cast to the correct type
client: client.(service.ServiceServiceClient), // Cast to the correct type
}
}

0 comments on commit e7a3559

Please sign in to comment.