-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from isd-sgcu/feat/baan
Feat/baan
- Loading branch information
Showing
16 changed files
with
849 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.