Skip to content

Commit

Permalink
Merge pull request #11 from isd-sgcu/gear-be/fdr-53-be-gateway-store-…
Browse files Browse the repository at this point in the history
…service

Gear be/fdr 53 be gateway store service
  • Loading branch information
macgeargear authored Jun 29, 2024
2 parents 65ef391 + 6cbf5a6 commit 55e29e6
Show file tree
Hide file tree
Showing 9 changed files with 549 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mock-gen:
mockgen -source ./internal/selection/selection.client.go -destination ./mocks/selection/selection.client.go
mockgen -source ./internal/checkin/checkin.service.go -destination ./mocks/checkin/checkin.service.go
mockgen -source ./internal/checkin/checkin.client.go -destination ./mocks/checkin/checkin.client.go
mockgen -source ./internal/store/store.service.go -destination ./mocks/store/store.service.go
mockgen -source ./internal/store/store.client.go -destination ./mocks/store/store.client.go
mockgen -source ./internal/router/context.go -destination ./mocks/router/context.mock.go
mockgen -source ./internal/validator/validator.go -destination ./mocks/validator/validator.mock.go

Expand Down
30 changes: 30 additions & 0 deletions internal/dto/object.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,33 @@ type DecomposedFile struct {
Filename string
Data []byte
}

type Object struct {
Url string `json:"url"`
Key string `json:"key"`
}

type UploadObjectRequest struct {
Filename string `json:"filename"`
Data []byte `json:"data"`
}

type UploadObjectResponse struct {
Object *Object `json:"object"`
}

type FindByKeyObjectRequest struct {
Key string `json:"key"`
}

type FindByKeyObjectResponse struct {
Object *Object `json:"object"`
}

type DeleteObjectRequest struct {
Key string `json:"key"`
}

type DeleteObjectResponse struct {
Success bool `json:"success"`
}
11 changes: 5 additions & 6 deletions internal/selection/test/selection.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (t *SelectionServiceTest) SetupTest() {
t.SelectionsProto = MockSelectionsProto()
t.SelectionProto = (t.SelectionsProto)[0]
t.SelectionsDto = selection.ProtoToDtoList(t.SelectionsProto)
// t.SelectionDto = selection.ProtoToDto(t.SelectionProto)
t.SelectionDto = (t.SelectionsDto)[0]

t.CreateSelectionProtoRequest = &selectionProto.CreateSelectionRequest{
Expand Down Expand Up @@ -93,7 +92,7 @@ func (t *SelectionServiceTest) TestCreateSelectionInvalidArgument() {
svc := selection.NewService(client, t.logger)

protoReq := t.CreateSelectionProtoRequest
expected := apperror.BadRequestError("Invalid argument")
expected := apperror.BadRequest
clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error())

client.EXPECT().Create(gomock.Any(), protoReq).Return(nil, clientErr)
Expand All @@ -108,8 +107,8 @@ func (t *SelectionServiceTest) TestCreateSelectionInternalError() {
svc := selection.NewService(client, t.logger)

protoReq := t.CreateSelectionProtoRequest
expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error")
clientErr := status.Error(codes.Internal, apperror.InternalServer.Error())
expected := apperror.InternalServer
clientErr := apperror.InternalServer

client.EXPECT().Create(gomock.Any(), protoReq).Return(nil, clientErr)
actual, err := svc.CreateSelection(t.CreateSelectionDtoRequest)
Expand Down Expand Up @@ -141,7 +140,7 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionInvalidArgument() {
svc := selection.NewService(client, t.logger)

protoReq := t.FindByGroupIdSelectionProtoRequest
expected := apperror.BadRequestError("Invalid argument")
expected := apperror.BadRequest
clientErr := status.Error(codes.InvalidArgument, apperror.BadRequest.Error())

client.EXPECT().FindByGroupId(gomock.Any(), protoReq).Return(nil, clientErr)
Expand All @@ -156,7 +155,7 @@ func (t *SelectionServiceTest) TestFindByGroupIdSelectionInternalError() {
svc := selection.NewService(client, t.logger)

protoReq := t.FindByGroupIdSelectionProtoRequest
expected := apperror.InternalServerError("rpc error: code = Internal desc = Internal error")
expected := apperror.InternalServer
clientErr := status.Error(codes.Internal, apperror.InternalServer.Error())

client.EXPECT().FindByGroupId(gomock.Any(), protoReq).Return(nil, clientErr)
Expand Down
36 changes: 36 additions & 0 deletions internal/store/store.client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package store

import (
"context"

storeProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/store/object/v1"
"google.golang.org/grpc"
)

type clientImpl struct {
client storeProto.ObjectServiceClient
}

type Client interface {
Upload(ctx context.Context, in *storeProto.UploadObjectRequest, opts ...grpc.CallOption) (*storeProto.UploadObjectResponse, error)
FindByKey(ctx context.Context, in *storeProto.FindByKeyObjectRequest, opts ...grpc.CallOption) (*storeProto.FindByKeyObjectResponse, error)
DeleteByKey(ctx context.Context, in *storeProto.DeleteByKeyObjectRequest, opts ...grpc.CallOption) (*storeProto.DeleteByKeyObjectResponse, error)
}

func NewClient(client storeProto.ObjectServiceClient) Client {
return &clientImpl{
client: client,
}
}

func (c *clientImpl) Upload(ctx context.Context, in *storeProto.UploadObjectRequest, opts ...grpc.CallOption) (*storeProto.UploadObjectResponse, error) {
return c.client.Upload(ctx, in, opts...)
}

func (c *clientImpl) FindByKey(ctx context.Context, in *storeProto.FindByKeyObjectRequest, opts ...grpc.CallOption) (*storeProto.FindByKeyObjectResponse, error) {
return c.client.FindByKey(ctx, in, opts...)
}

func (c *clientImpl) DeleteByKey(ctx context.Context, in *storeProto.DeleteByKeyObjectRequest, opts ...grpc.CallOption) (*storeProto.DeleteByKeyObjectResponse, error) {
return c.client.DeleteByKey(ctx, in, opts...)
}
86 changes: 86 additions & 0 deletions internal/store/store.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package store

import (
"context"
"time"

"github.com/isd-sgcu/rpkm67-gateway/apperror"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
storeProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/store/object/v1"
"go.uber.org/zap"
)

type Service interface {
Upload(req *dto.UploadObjectRequest) (*dto.UploadObjectResponse, *apperror.AppError)
FindByKey(req *dto.FindByKeyObjectRequest) (*dto.FindByKeyObjectResponse, *apperror.AppError)
DeleteByKey(req *dto.DeleteObjectRequest) (*dto.DeleteObjectResponse, *apperror.AppError)
}

type serviceImpl struct {
client storeProto.ObjectServiceClient
log *zap.Logger
}

func NewService(client storeProto.ObjectServiceClient, log *zap.Logger) Service {
return &serviceImpl{
client: client,
log: log,
}
}

func (s *serviceImpl) Upload(req *dto.UploadObjectRequest) (*dto.UploadObjectResponse, *apperror.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, err := s.client.Upload(ctx, &storeProto.UploadObjectRequest{
Filename: req.Filename,
})
if err != nil {
s.log.Named("Upload").Error("Upload: ", zap.Error(err))
return nil, apperror.HandleServiceError(err)
}

return &dto.UploadObjectResponse{
Object: &dto.Object{
Url: res.Object.Url,
Key: res.Object.Key,
},
}, nil
}

func (s *serviceImpl) FindByKey(req *dto.FindByKeyObjectRequest) (*dto.FindByKeyObjectResponse, *apperror.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, err := s.client.FindByKey(ctx, &storeProto.FindByKeyObjectRequest{
Key: req.Key,
})
if err != nil {
s.log.Named("FindByKey").Error("FindByKey: ", zap.Error(err))
return nil, apperror.HandleServiceError(err)
}

return &dto.FindByKeyObjectResponse{
Object: &dto.Object{
Url: res.Object.Url,
Key: res.Object.Key,
},
}, nil
}

func (s *serviceImpl) DeleteByKey(req *dto.DeleteObjectRequest) (*dto.DeleteObjectResponse, *apperror.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, err := s.client.DeleteByKey(ctx, &storeProto.DeleteByKeyObjectRequest{
Key: req.Key,
})
if err != nil {
s.log.Named("DeleteByKey").Error("DeleteByKey: ", zap.Error(err))
return nil, apperror.HandleServiceError(err)
}

return &dto.DeleteObjectResponse{
Success: res.Success,
}, nil
}
Loading

0 comments on commit 55e29e6

Please sign in to comment.