Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
macgeargear committed Jun 23, 2024
1 parent 44dc5fd commit 61686be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
15 changes: 9 additions & 6 deletions internal/selection/selection.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ type handlerImpl struct {
func (h *handlerImpl) CreateSelection(c router.Context) {
body := &dto.CreateSelectionRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err))
h.log.Named("CreateSelection").Error("Bind: failed to bind request body", zap.Error(err))
c.BadRequestError(err.Error())
return
}

if errorList := h.validate.Validate(body); errorList != nil {
h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList))
h.log.Named("CreateSelection").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}
Expand All @@ -51,6 +51,7 @@ func (h *handlerImpl) CreateSelection(c router.Context) {

res, appErr := h.svc.CreateSelection(req)
if appErr != nil {
h.log.Named("CreateSelection").Error("CreateSelection: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}
Expand All @@ -61,7 +62,7 @@ func (h *handlerImpl) CreateSelection(c router.Context) {
func (h *handlerImpl) FindByGroupIdSelection(c router.Context) {
groupdId := c.Param("id")
if groupdId == "" {
h.log.Named("selection hdr").Error("url parameter 'id' not found", zap.String("id", groupdId))
h.log.Named("FindByGroupIdSelection").Error("Param: id not found")
c.BadRequestError("url parameter 'id' not found")
return
}
Expand All @@ -71,13 +72,14 @@ func (h *handlerImpl) FindByGroupIdSelection(c router.Context) {
}

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

res, appErr := h.svc.FindByGroupIdSelection(req)
if appErr != nil {
h.log.Named("FindByGroupIdSelection").Error("FindByGroupIdSelection: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}
Expand All @@ -88,13 +90,13 @@ func (h *handlerImpl) FindByGroupIdSelection(c router.Context) {
func (h *handlerImpl) UpdateSelection(c router.Context) {
body := &dto.UpdateSelectionRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err))
h.log.Named("UpdateSelection").Error("Bind: ", zap.Error(err))
c.BadRequestError(err.Error())
return
}

if errorList := h.validate.Validate(body); errorList != nil {
h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList))
h.log.Named("UpdateSelection").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}
Expand All @@ -105,6 +107,7 @@ func (h *handlerImpl) UpdateSelection(c router.Context) {

res, appErr := h.svc.UpdateSelection(req)
if appErr != nil {
h.log.Named("UpdateSelection").Error("UpdateSelection: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}
Expand Down
12 changes: 12 additions & 0 deletions internal/selection/selection.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre
if err != nil {
st, ok := status.FromError(err)
if !ok {
s.log.Named("CreateSelection").Error("FromeError: ", zap.Error(err))
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
s.log.Named("CreateSelection").Error("Create: ", zap.Error(err))
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
s.log.Named("CreateSelection").Error("Create: ", zap.Error(err))
return nil, apperror.InternalServerError(err.Error())
default:
s.log.Named("CreateSelection").Error("Create: ", zap.Error(err))
return nil, apperror.ServiceUnavailable
}
}
Expand All @@ -68,14 +72,18 @@ func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequ
if err != nil {
st, ok := status.FromError(err)
if !ok {
s.log.Named("FindByGroupIdSelection").Error("FromeError: ", zap.Error(err))
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err))
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err))
return nil, apperror.InternalServerError(err.Error())
default:
s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err))
return nil, apperror.ServiceUnavailable
}
}
Expand All @@ -95,14 +103,18 @@ func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.Upd
if err != nil {
st, ok := status.FromError(err)
if !ok {
s.log.Named("UpdateSelection").Error("FromeError: ", zap.Error(err))
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err))
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err))
return nil, apperror.InternalServerError(err.Error())
default:
s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err))
return nil, apperror.ServiceUnavailable
}
}
Expand Down
28 changes: 19 additions & 9 deletions internal/selection/test/selection.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() {

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

selectionSvc := selectionMock.NewMockService(controller)
validator := validatorMock.NewMockDtoValidator(controller)
context := routerMock.NewMockContext(controller)

context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr)
context.EXPECT().BadRequestError(expectedErr.Error())

handler := selection.NewHandler(selectionSvc, validator, t.logger)
handler := selection.NewHandler(nil, nil, t.logger)
handler.CreateSelection(context)
}

Expand Down Expand Up @@ -130,14 +128,12 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() {

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

selectionSvc := selectionMock.NewMockService(controller)
validator := validatorMock.NewMockDtoValidator(controller)
context := routerMock.NewMockContext(controller)

context.EXPECT().Param("id").Return("")
context.EXPECT().BadRequestError(expectedErr.Error())

handler := selection.NewHandler(selectionSvc, validator, t.logger)
handler := selection.NewHandler(nil, nil, t.logger)
handler.FindByGroupIdSelection(context)
}

Expand Down Expand Up @@ -185,16 +181,30 @@ func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() {

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

selectionSvc := selectionMock.NewMockService(controller)
validator := validatorMock.NewMockDtoValidator(controller)
context := routerMock.NewMockContext(controller)

context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr)
context.EXPECT().BadRequestError(expectedErr.Error())

handler := selection.NewHandler(selectionSvc, validator, t.logger)
handler := selection.NewHandler(nil, nil, t.logger)
handler.UpdateSelection(context)
}

func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() {
expectedErr := apperror.InternalServerError("internal error")

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

selectionSvc := selectionMock.NewMockService(controller)
validator := validatorMock.NewMockDtoValidator(controller)
context := routerMock.NewMockContext(controller)

context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil)
validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil)

selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(nil, expectedErr)
context.EXPECT().ResponseError(expectedErr)

handler := selection.NewHandler(selectionSvc, validator, t.logger)
handler.UpdateSelection(context)
}

0 comments on commit 61686be

Please sign in to comment.