Skip to content

Commit

Permalink
Merge pull request #1 from isd-sgcu/feat/baan
Browse files Browse the repository at this point in the history
Feat/baan
  • Loading branch information
macgeargear authored Jun 22, 2024
2 parents 46eb96a + 42ef044 commit bad1fbe
Show file tree
Hide file tree
Showing 16 changed files with 849 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ server:

watch:
air

mock-gen:
mockgen -source ./internal/auth/auth.handler.go -destination ./mocks/auth/auth.handler.go
mockgen -source ./internal/auth/auth.service.go -destination ./mocks/auth/auth.service.go
mockgen -source ./internal/baan/baan.handler.go -destination ./mocks/baan/baan.handler.go
mockgen -source ./internal/baan/baan.service.go -destination ./mocks/baan/baan.service.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

test:
go vet ./...
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ require (
google.golang.org/grpc v1.64.0
)

require github.com/stretchr/objx v0.5.2 // indirect

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/bxcodec/faker/v4 v4.0.0-beta.3
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tN
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/bxcodec/faker/v4 v4.0.0-beta.3 h1:gqYNBvN72QtzKkYohNDKQlm+pg+uwBDVMN28nWHS18k=
github.com/bxcodec/faker/v4 v4.0.0-beta.3/go.mod h1:m6+Ch1Lj3fqW/unZmvkXIdxWS5+XQWPWxcbbQW2X+Ho=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
Expand Down Expand Up @@ -95,6 +97,7 @@ github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6po
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
66 changes: 66 additions & 0 deletions internal/baan/baan.handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package baan

import (
"net/http"
"strings"

"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
"github.com/isd-sgcu/rpkm67-gateway/internal/router"
"github.com/isd-sgcu/rpkm67-gateway/internal/validator"
"go.uber.org/zap"
)

type Handler interface {
FindAllBaan(c router.Context)
FindOneBaan(c router.Context)
}

func NewHandler(svc Service, validate validator.DtoValidator, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
validate: validate,
log: log,
}
}

type handlerImpl struct {
svc Service
validate validator.DtoValidator
log *zap.Logger
}

func (h *handlerImpl) FindAllBaan(c router.Context) {
req := &dto.FindAllBaanRequest{}
res, appErr := h.svc.FindAllBaan(req)
if appErr != nil {
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.FindAllBaanResponse{Baans: res.Baans})
}

func (h *handlerImpl) FindOneBaan(c router.Context) {
baanId := c.Param("id")
if baanId == "" {
c.BadRequestError("url parameter 'id' not found")
}

req := &dto.FindOneBaanRequest{
Id: baanId,
}

if errorList := h.validate.Validate(req); errorList != nil {
h.log.Named("baan hdr").Error("validation error", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

res, appErr := h.svc.FindOneBaan(req)
if appErr != nil {
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.FindOneBaanResponse{Baan: res.Baan})
}
82 changes: 82 additions & 0 deletions internal/baan/baan.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package baan

import (
"context"
"time"

"github.com/isd-sgcu/rpkm67-gateway/apperrors"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Service interface {
FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaanResponse, *apperrors.AppError)
FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaanResponse, *apperrors.AppError)
}

type serviceImpl struct {
client baanProto.BaanServiceClient
log *zap.Logger
}

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

func (s *serviceImpl) FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaanResponse, *apperrors.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, err := s.client.FindAllBaan(ctx, &baanProto.FindAllBaanRequest{})
if err != nil {
st, ok := status.FromError(err)
if !ok {
return nil, apperrors.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperrors.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperrors.InternalServerError(err.Error())
default:
return nil, apperrors.ServiceUnavailable
}
}

return &dto.FindAllBaanResponse{
Baans: ProtoToDtoList(res.Baans),
}, nil
}

func (s *serviceImpl) FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaanResponse, *apperrors.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

res, err := s.client.FindOneBaan(ctx, &baanProto.FindOneBaanRequest{
Id: req.Id,
})
if err != nil {
st, ok := status.FromError(err)
if !ok {
return nil, apperrors.InternalServer
}
switch st.Code() {
case codes.NotFound:
return nil, apperrors.NotFoundError("Baan not found")
case codes.Internal:
return nil, apperrors.InternalServerError(err.Error())
default:
return nil, apperrors.ServiceUnavailable
}
}

return &dto.FindOneBaanResponse{
Baan: ProtoToDto(res.Baan),
}, nil
}
32 changes: 32 additions & 0 deletions internal/baan/baan.utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package baan

import (
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
baanProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/baan/v1"
)

func ProtoToDto(in *baanProto.Baan) *dto.Baan {
return &dto.Baan{
Id: in.Id,
NameTH: in.NameTH,
DescriptionTH: in.DescriptionTH,
NameEN: in.NameEN,
DescriptionEN: in.DescriptionEN,
Size: dto.BaanSize(in.Size),
Facebook: in.Facebook,
FacebookUrl: in.FacebookUrl,
Instagram: in.Instagram,
InstagramUrl: in.InstagramUrl,
Line: in.Line,
LineUrl: in.LineUrl,
ImageUrl: in.ImageUrl,
}
}

func ProtoToDtoList(in []*baanProto.Baan) []*dto.Baan {
var out []*dto.Baan
for _, b := range in {
out = append(out, ProtoToDto(b))
}
return out
}
92 changes: 92 additions & 0 deletions internal/baan/test/baan.handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package test

import (
"net/http"
"testing"

"github.com/golang/mock/gomock"
"github.com/isd-sgcu/rpkm67-gateway/apperrors"
"github.com/isd-sgcu/rpkm67-gateway/internal/baan"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
baanMock "github.com/isd-sgcu/rpkm67-gateway/mocks/baan"
routerMock "github.com/isd-sgcu/rpkm67-gateway/mocks/router"
validatorMock "github.com/isd-sgcu/rpkm67-gateway/mocks/validator"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
)

type BaanHandlerTest struct {
suite.Suite
controller *gomock.Controller
logger *zap.Logger
Baans []*dto.Baan
Baan *dto.Baan
FindAllBaanReq *dto.FindAllBaanRequest
FindOneBaanReq *dto.FindOneBaanRequest
Err *apperrors.AppError
ParamMock string
}

func TestBaanHandler(t *testing.T) {
suite.Run(t, new(BaanHandlerTest))
}

func (t *BaanHandlerTest) SetupTest() {
t.controller = gomock.NewController(t.T())
t.logger = zap.NewNop()

baansProto := MockBaansProto()
baanProto := baansProto[0]

t.Baans = baan.ProtoToDtoList(baansProto)
t.Baan = baan.ProtoToDto(baanProto)

t.FindAllBaanReq = &dto.FindAllBaanRequest{}
t.FindOneBaanReq = &dto.FindOneBaanRequest{
Id: t.Baan.Id,
}

t.ParamMock = t.Baan.Id
}

func (t *BaanHandlerTest) TestFindAllBaanSuccess() {
expectedResp := &dto.FindAllBaanResponse{
Baans: t.Baans,
}

controller := gomock.NewController(t.T())

baanSvc := baanMock.NewMockService(controller)
validator := validatorMock.NewMockDtoValidator(controller)
context := routerMock.NewMockContext(controller)

baanSvc.EXPECT().FindAllBaan(t.FindAllBaanReq).Return(expectedResp, t.Err)
context.EXPECT().JSON(http.StatusOK, expectedResp)

handler := baan.NewHandler(baanSvc, validator, t.logger)
handler.FindAllBaan(context)
}

func (t *BaanHandlerTest) TestFindOneBaanSuccess() {
expectedResp := &dto.FindOneBaanResponse{
Baan: t.Baan,
}

controller := gomock.NewController(t.T())

baanSvc := baanMock.NewMockService(controller)
validator := validatorMock.NewMockDtoValidator(controller)
context := routerMock.NewMockContext(controller)

context.EXPECT().Param("id").Return(t.ParamMock)
validator.EXPECT().Validate(t.FindOneBaanReq).Return(nil)
baanSvc.EXPECT().FindOneBaan(t.FindOneBaanReq).Return(expectedResp, t.Err)
context.EXPECT().JSON(http.StatusOK, expectedResp)

handler := baan.NewHandler(baanSvc, validator, t.logger)
handler.FindOneBaan(context)
}

func (t *BaanHandlerTest) TearDownTest() {
t.controller.Finish()
}
Loading

0 comments on commit bad1fbe

Please sign in to comment.