-
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 #7 from isd-sgcu/gear-be/fdr-50-be-gateway-checkin
Gear be/fdr 50 be gateway checkin
- Loading branch information
Showing
15 changed files
with
952 additions
and
19 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
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,36 @@ | ||
package checkin | ||
|
||
import ( | ||
"context" | ||
|
||
checkinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type clientImpl struct { | ||
client checkinProto.CheckInServiceClient | ||
} | ||
|
||
type Client interface { | ||
Create(ctx context.Context, in *checkinProto.CreateCheckInRequest, opts ...grpc.CallOption) (*checkinProto.CreateCheckInResponse, error) | ||
FindByUserId(ctx context.Context, in *checkinProto.FindByUserIdCheckInRequest, opts ...grpc.CallOption) (*checkinProto.FindByUserIdCheckInResponse, error) | ||
FindByEmail(ctx context.Context, in *checkinProto.FindByEmailCheckInRequest, opts ...grpc.CallOption) (*checkinProto.FindByEmailCheckInResponse, error) | ||
} | ||
|
||
func NewClient(client checkinProto.CheckInServiceClient) Client { | ||
return &clientImpl{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (c *clientImpl) Create(ctx context.Context, in *checkinProto.CreateCheckInRequest, opts ...grpc.CallOption) (*checkinProto.CreateCheckInResponse, error) { | ||
return c.client.Create(ctx, in, opts...) | ||
} | ||
|
||
func (c *clientImpl) FindByUserId(ctx context.Context, in *checkinProto.FindByUserIdCheckInRequest, opts ...grpc.CallOption) (*checkinProto.FindByUserIdCheckInResponse, error) { | ||
return c.client.FindByUserId(ctx, in, opts...) | ||
} | ||
|
||
func (c *clientImpl) FindByEmail(ctx context.Context, in *checkinProto.FindByEmailCheckInRequest, opts ...grpc.CallOption) (*checkinProto.FindByEmailCheckInResponse, error) { | ||
return c.client.FindByEmail(ctx, in, opts...) | ||
} |
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 checkin | ||
|
||
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 { | ||
Create(c router.Context) | ||
FindByEmail(c router.Context) | ||
FindByUserID(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) Create(c router.Context) { | ||
body := &dto.CreateCheckInRequest{} | ||
if err := c.Bind(body); err != nil { | ||
h.log.Named("Create").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("Create").Error("Validate: ", zap.Strings("errorList", errorList)) | ||
c.BadRequestError(strings.Join(errorList, ", ")) | ||
return | ||
} | ||
|
||
req := &dto.CreateCheckInRequest{ | ||
Email: body.Email, | ||
UserID: body.UserID, | ||
Event: body.Event, | ||
} | ||
|
||
res, appErr := h.svc.Create(req) | ||
if appErr != nil { | ||
h.log.Named("Create").Error("Create: ", zap.Error(appErr)) | ||
c.ResponseError(appErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, &dto.CreateCheckInResponse{ | ||
CheckIn: &dto.CheckIn{ | ||
ID: res.CheckIn.ID, | ||
UserID: res.CheckIn.UserID, | ||
Email: res.CheckIn.Email, | ||
Event: res.CheckIn.Event, | ||
}, | ||
}) | ||
} | ||
|
||
func (h *handlerImpl) FindByEmail(c router.Context) { | ||
email := c.Param("email") | ||
if email == "" { | ||
h.log.Named("FindByEmail").Error("FindByEmail: email should not be empty") | ||
c.BadRequestError("email should not be empty") | ||
return | ||
} | ||
|
||
req := &dto.FindByEmailCheckInRequest{ | ||
Email: email, | ||
} | ||
|
||
res, appErr := h.svc.FindByEmail(req) | ||
if appErr != nil { | ||
h.log.Named("FindByEmail").Error("FindByEmail: ", zap.Error(appErr)) | ||
c.ResponseError(appErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, &dto.FindByEmailCheckInResponse{ | ||
CheckIns: res.CheckIns, | ||
}) | ||
} | ||
|
||
func (h *handlerImpl) FindByUserID(c router.Context) { | ||
userId := c.Param("userId") | ||
if userId == "" { | ||
h.log.Named("FindByUserID").Error("FindByUserID: user_id should not be empty") | ||
c.BadRequestError("user_id should not be empty") | ||
return | ||
} | ||
|
||
req := &dto.FindByUserIdCheckInRequest{ | ||
UserID: userId, | ||
} | ||
|
||
res, appErr := h.svc.FindByUserID(req) | ||
if appErr != nil { | ||
h.log.Named("FindByUserID").Error("FindByUserID: ", zap.Error(appErr)) | ||
c.ResponseError(appErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, &dto.FindByUserIdCheckInResponse{ | ||
CheckIns: res.CheckIns, | ||
}) | ||
} |
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,122 @@ | ||
package checkin | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/isd-sgcu/rpkm67-gateway/apperror" | ||
"github.com/isd-sgcu/rpkm67-gateway/internal/dto" | ||
checkinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type Service interface { | ||
Create(req *dto.CreateCheckInRequest) (*dto.CreateCheckInResponse, *apperror.AppError) | ||
FindByUserID(req *dto.FindByUserIdCheckInRequest) (*dto.FindByUserIdCheckInResponse, *apperror.AppError) | ||
FindByEmail(req *dto.FindByEmailCheckInRequest) (*dto.FindByEmailCheckInResponse, *apperror.AppError) | ||
} | ||
|
||
type serviceImpl struct { | ||
client checkinProto.CheckInServiceClient | ||
log *zap.Logger | ||
} | ||
|
||
func NewService(client checkinProto.CheckInServiceClient, log *zap.Logger) Service { | ||
return &serviceImpl{ | ||
client: client, | ||
log: log, | ||
} | ||
} | ||
|
||
func (s *serviceImpl) Create(req *dto.CreateCheckInRequest) (*dto.CreateCheckInResponse, *apperror.AppError) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
res, err := s.client.Create(ctx, &checkinProto.CreateCheckInRequest{ | ||
Email: req.Email, | ||
UserId: req.UserID, | ||
Event: req.Event, | ||
}) | ||
if err != nil { | ||
s.log.Named("Create").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.BadRequest | ||
case codes.Internal: | ||
return nil, apperror.InternalServer | ||
default: | ||
return nil, apperror.ServiceUnavailable | ||
} | ||
} | ||
|
||
return &dto.CreateCheckInResponse{ | ||
CheckIn: &dto.CheckIn{ | ||
ID: res.CheckIn.Id, | ||
UserID: res.CheckIn.UserId, | ||
Email: res.CheckIn.Email, | ||
Event: res.CheckIn.Event, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (s *serviceImpl) FindByEmail(req *dto.FindByEmailCheckInRequest) (*dto.FindByEmailCheckInResponse, *apperror.AppError) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
res, err := s.client.FindByEmail(ctx, &checkinProto.FindByEmailCheckInRequest{ | ||
Email: req.Email, | ||
}) | ||
if err != nil { | ||
s.log.Named("FindByEmail").Error("FindByEmail: ", zap.Error(err)) | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
return nil, apperror.InternalServer | ||
} | ||
switch st.Code() { | ||
case codes.InvalidArgument: | ||
return nil, apperror.BadRequest | ||
case codes.Internal: | ||
return nil, apperror.InternalServer | ||
default: | ||
return nil, apperror.ServiceUnavailable | ||
} | ||
} | ||
|
||
return &dto.FindByEmailCheckInResponse{ | ||
CheckIns: ProtoToDtos(res.CheckIns), | ||
}, nil | ||
} | ||
|
||
func (s *serviceImpl) FindByUserID(req *dto.FindByUserIdCheckInRequest) (*dto.FindByUserIdCheckInResponse, *apperror.AppError) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
res, err := s.client.FindByUserId(ctx, &checkinProto.FindByUserIdCheckInRequest{ | ||
UserId: req.UserID, | ||
}) | ||
if err != nil { | ||
s.log.Named("FindByUserID").Error("FindByUserID: ", zap.Error(err)) | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
return nil, apperror.InternalServer | ||
} | ||
switch st.Code() { | ||
case codes.InvalidArgument: | ||
return nil, apperror.BadRequest | ||
case codes.Internal: | ||
return nil, apperror.InternalServer | ||
default: | ||
return nil, apperror.ServiceUnavailable | ||
} | ||
} | ||
|
||
return &dto.FindByUserIdCheckInResponse{ | ||
CheckIns: ProtoToDtos(res.CheckIns), | ||
}, nil | ||
} |
Oops, something went wrong.