Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/selection #2

Merged
merged 11 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mock-gen:
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/selection/selection.handler.go -destination ./mocks/selection/selection.handler.go
mockgen -source ./internal/selection/selection.service.go -destination ./mocks/selection/selection.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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/go-playground/validator/v10 v10.20.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
github.com/isd-sgcu/rpkm67-go-proto v0.0.4
github.com/isd-sgcu/rpkm67-go-proto v0.0.7
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v1.0.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/isd-sgcu/rpkm67-go-proto v0.0.4 h1:ZO1m0EfTtjQ2IcRy6mUHj+mAb4loM3ckeisKefKqKmY=
github.com/isd-sgcu/rpkm67-go-proto v0.0.4/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.0.5 h1:8zTvNQ0NPokoVRK/NQJYxWjcBFMZNK0avotPs9RSPh0=
github.com/isd-sgcu/rpkm67-go-proto v0.0.5/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.0.6 h1:alECc0pLyJmbJ2cLBukNDplka+ucWutR6yLXqAn0lJM=
github.com/isd-sgcu/rpkm67-go-proto v0.0.6/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.0.7 h1:BrHjp7hDFmXTMuMKuA4mK3bjtHTvzozO0vYC1hV1a+w=
github.com/isd-sgcu/rpkm67-go-proto v0.0.7/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down
14 changes: 7 additions & 7 deletions internal/auth/auth.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Service interface {
Validate()
RefreshToken()
SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror.AppError)
SignUp(req *dto.SignUpRequest) (*dto.SignupResponse, *apperror.AppError)
SignIn(req *dto.SignInRequest) (*dto.Credential, *apperror.AppError)
SignOut(req *dto.TokenPayloadAuth) (*dto.SignOutResponse, *apperror.AppError)
ForgotPassword(req *dto.ForgotPasswordRequest) (*dto.ForgotPasswordResponse, *apperror.AppError)
Expand All @@ -39,7 +39,7 @@ func (s *serviceImpl) Validate() {
func (s *serviceImpl) RefreshToken() {
}

func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror.AppError) {
func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.SignupResponse, *apperror.AppError) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -48,7 +48,6 @@ func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror
Password: req.Password,
Firstname: req.Firstname,
Lastname: req.Lastname,
Role: "student",
})
if err != nil {
st, ok := status.FromError(err)
Expand All @@ -65,10 +64,11 @@ func (s *serviceImpl) SignUp(req *dto.SignUpRequest) (*dto.Credential, *apperror
}
}

return &dto.Credential{
AccessToken: res.Credential.AccessToken,
RefreshToken: res.Credential.RefreshToken,
ExpiresIn: int(res.Credential.ExpiresIn),
return &dto.SignupResponse{
Id: res.Id,
Email: res.Email,
Firstname: res.Firstname,
Lastname: res.Lastname,
}, nil
}

Expand Down
32 changes: 32 additions & 0 deletions internal/dto/selection.dto.go
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"`
}
118 changes: 118 additions & 0 deletions internal/selection/selection.handler.go
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 {
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
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,
})
}
116 changes: 116 additions & 0 deletions internal/selection/selection.service.go
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
macgeargear marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
}
30 changes: 30 additions & 0 deletions internal/selection/selection.utils.go
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
}
Loading
Loading