-
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 #2 from isd-sgcu/feat/selection
Feat/selection
- Loading branch information
Showing
14 changed files
with
922 additions
and
8 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
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 dto | ||
|
||
type Selection struct { | ||
Id string `json:"id"` | ||
GroupId string `json:"group_id"` | ||
BaanIds []string `json:"baan_ids"` | ||
} | ||
|
||
type CreateSelectionRequest struct { | ||
GroupId string `json:"group_id" validate:"required"` | ||
BaanIds []string `json:"baan_ids" validate:"required"` | ||
} | ||
|
||
type CreateSelectionResponse struct { | ||
Selection *Selection `json:"selection"` | ||
} | ||
|
||
type FindByGroupIdSelectionRequest struct { | ||
GroupId string `json:"group_id" validate:"required"` | ||
} | ||
|
||
type FindByGroupIdSelectionResponse struct { | ||
Selection *Selection `json:"selection"` | ||
} | ||
|
||
type UpdateSelectionRequest struct { | ||
Selection *Selection `json:"selection" validate:"required"` | ||
} | ||
|
||
type UpdateSelectionResponse struct { | ||
Success bool `json:"success"` | ||
} |
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,118 @@ | ||
package selection | ||
|
||
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 { | ||
CreateSelection(c router.Context) | ||
FindByGroupIdSelection(c router.Context) | ||
UpdateSelection(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) CreateSelection(c router.Context) { | ||
body := &dto.CreateSelectionRequest{} | ||
if err := c.Bind(body); err != nil { | ||
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("CreateSelection").Error("Validate: ", zap.Strings("errorList", errorList)) | ||
c.BadRequestError(strings.Join(errorList, ", ")) | ||
return | ||
} | ||
|
||
req := &dto.CreateSelectionRequest{ | ||
GroupId: body.GroupId, | ||
BaanIds: body.BaanIds, | ||
} | ||
|
||
res, appErr := h.svc.CreateSelection(req) | ||
if appErr != nil { | ||
h.log.Named("CreateSelection").Error("CreateSelection: ", zap.Error(appErr)) | ||
c.ResponseError(appErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, &dto.CreateSelectionResponse{Selection: res.Selection}) | ||
} | ||
|
||
func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { | ||
groupId := c.Param("id") | ||
if groupId == "" { | ||
h.log.Named("FindByGroupIdSelection").Error("Param: id not found") | ||
c.BadRequestError("url parameter 'id' not found") | ||
return | ||
} | ||
|
||
req := &dto.FindByGroupIdSelectionRequest{ | ||
GroupId: groupId, | ||
} | ||
|
||
if errorList := h.validate.Validate(req); errorList != nil { | ||
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 | ||
} | ||
|
||
c.JSON(http.StatusOK, &dto.FindByGroupIdSelectionResponse{Selection: res.Selection}) | ||
} | ||
|
||
func (h *handlerImpl) UpdateSelection(c router.Context) { | ||
body := &dto.UpdateSelectionRequest{} | ||
if err := c.Bind(body); err != nil { | ||
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("UpdateSelection").Error("Validate: ", zap.Strings("errorList", errorList)) | ||
c.BadRequestError(strings.Join(errorList, ", ")) | ||
return | ||
} | ||
|
||
req := &dto.UpdateSelectionRequest{ | ||
Selection: body.Selection, | ||
} | ||
|
||
res, appErr := h.svc.UpdateSelection(req) | ||
if appErr != nil { | ||
h.log.Named("UpdateSelection").Error("UpdateSelection: ", zap.Error(appErr)) | ||
c.ResponseError(appErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, &dto.UpdateSelectionResponse{ | ||
Success: res.Success, | ||
}) | ||
} |
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,116 @@ | ||
package selection | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/isd-sgcu/rpkm67-gateway/apperror" | ||
"github.com/isd-sgcu/rpkm67-gateway/internal/dto" | ||
selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type Service interface { | ||
CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperror.AppError) | ||
FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperror.AppError) | ||
UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperror.AppError) | ||
} | ||
|
||
type serviceImpl struct { | ||
client selectionProto.SelectionServiceClient | ||
log *zap.Logger | ||
} | ||
|
||
func NewService(client selectionProto.SelectionServiceClient, log *zap.Logger) Service { | ||
return &serviceImpl{ | ||
client: client, | ||
log: log, | ||
} | ||
} | ||
|
||
func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.CreateSelectionResponse, *apperror.AppError) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
res, err := s.client.Create(ctx, &selectionProto.CreateSelectionRequest{ | ||
GroupId: req.GroupId, | ||
BaanIds: req.BaanIds, | ||
}) | ||
if err != nil { | ||
s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
return nil, apperror.InternalServer | ||
} | ||
switch st.Code() { | ||
case codes.InvalidArgument: | ||
return nil, apperror.BadRequestError("Invalid argument") | ||
case codes.Internal: | ||
return nil, apperror.InternalServerError(err.Error()) | ||
default: | ||
return nil, apperror.ServiceUnavailable | ||
} | ||
} | ||
|
||
return &dto.CreateSelectionResponse{ | ||
Selection: ProtoToDto(res.Selection), | ||
}, nil | ||
} | ||
|
||
func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequest) (*dto.FindByGroupIdSelectionResponse, *apperror.AppError) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
res, err := s.client.FindByGroupId(ctx, &selectionProto.FindByGroupIdSelectionRequest{ | ||
GroupId: req.GroupId, | ||
}) | ||
if err != nil { | ||
s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
return nil, apperror.InternalServer | ||
} | ||
switch st.Code() { | ||
case codes.InvalidArgument: | ||
return nil, apperror.BadRequestError("Invalid argument") | ||
case codes.Internal: | ||
return nil, apperror.InternalServerError(err.Error()) | ||
default: | ||
return nil, apperror.ServiceUnavailable | ||
} | ||
} | ||
|
||
return &dto.FindByGroupIdSelectionResponse{ | ||
Selection: ProtoToDto(res.Selection), | ||
}, nil | ||
} | ||
|
||
func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.UpdateSelectionResponse, *apperror.AppError) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
res, err := s.client.Update(ctx, &selectionProto.UpdateSelectionRequest{ | ||
Selection: DtoToProto(req.Selection), | ||
}) | ||
if err != nil { | ||
s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
return nil, apperror.InternalServer | ||
} | ||
switch st.Code() { | ||
case codes.InvalidArgument: | ||
return nil, apperror.BadRequestError("Invalid argument") | ||
case codes.Internal: | ||
return nil, apperror.InternalServerError(err.Error()) | ||
default: | ||
return nil, apperror.ServiceUnavailable | ||
} | ||
} | ||
|
||
return &dto.UpdateSelectionResponse{ | ||
Success: res.Success, | ||
}, 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,30 @@ | ||
package selection | ||
|
||
import ( | ||
"github.com/isd-sgcu/rpkm67-gateway/internal/dto" | ||
selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1" | ||
) | ||
|
||
func ProtoToDto(selection *selectionProto.Selection) *dto.Selection { | ||
return &dto.Selection{ | ||
Id: selection.Id, | ||
GroupId: selection.GroupId, | ||
BaanIds: selection.BaanIds, | ||
} | ||
} | ||
|
||
func DtoToProto(selection *dto.Selection) *selectionProto.Selection { | ||
return &selectionProto.Selection{ | ||
Id: selection.Id, | ||
GroupId: selection.GroupId, | ||
BaanIds: selection.BaanIds, | ||
} | ||
} | ||
|
||
func ProtoToDtoList(selections []*selectionProto.Selection) []*dto.Selection { | ||
var out []*dto.Selection | ||
for _, selection := range selections { | ||
out = append(out, ProtoToDto(selection)) | ||
} | ||
return out | ||
} |
Oops, something went wrong.