Skip to content

Commit

Permalink
Merge pull request #5 from isd-sgcu/feat/group
Browse files Browse the repository at this point in the history
Feat/group
  • Loading branch information
macgeargear authored Jun 23, 2024
2 parents 6919d5e + 8852049 commit 7932ab3
Show file tree
Hide file tree
Showing 8 changed files with 633 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/baan/baan.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (h *handlerImpl) FindAllBaan(c router.Context) {
req := &dto.FindAllBaanRequest{}
res, appErr := h.svc.FindAllBaan(req)
if appErr != nil {
h.log.Named("FindAllBaan").Error("FindAllBaan: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}
Expand All @@ -51,13 +52,14 @@ func (h *handlerImpl) FindOneBaan(c router.Context) {
}

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

res, appErr := h.svc.FindOneBaan(req)
if appErr != nil {
h.log.Named("FindOneBaan").Error("FindOneBaan: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}
Expand Down
2 changes: 2 additions & 0 deletions internal/baan/baan.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (s *serviceImpl) FindAllBaan(req *dto.FindAllBaanRequest) (*dto.FindAllBaan

res, err := s.client.FindAllBaan(ctx, &baanProto.FindAllBaanRequest{})
if err != nil {
s.log.Named("FindAllBaan").Error("FindAllBaan: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
Expand Down Expand Up @@ -62,6 +63,7 @@ func (s *serviceImpl) FindOneBaan(req *dto.FindOneBaanRequest) (*dto.FindOneBaan
Id: req.Id,
})
if err != nil {
s.log.Named("FindOneBaan").Error("FindOneBaan: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
Expand Down
77 changes: 77 additions & 0 deletions internal/dto/group.dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dto

type Group struct {
Id string `json:"id"`
LeaderID string `json:"leader_id"`
Token string `json:"token"`
Members []*UserInfo `json:"members"`
}

type UserInfo struct {
Id string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
ImageUrl string `json:"image_url"`
}

type FindOneGroupRequest struct {
UserId string `json:"user_id"`
}

type FindOneGroupResponse struct {
Group *Group `json:"group"`
}

type FindByTokenGroupRequest struct {
Token string `json:"token"`
}

type FindByTokenGroupResponse struct {
Id string `json:"id"`
Token string `json:"token"`
Leader *UserInfo `json:"leader"`
}

type UpdateGroupRequest struct {
Group *Group `json:"group"`
LeaderId string `json:"leader_id"`
}

type UpdateGroupResponse struct {
Group *Group `json:"group"`
}

type JoinGroupRequest struct {
Token string `json:"token"`
UserId string `json:"user_id"`
}

type JoinGroupResponse struct {
Group *Group `json:"group"`
}

type DeleteMemberGroupRequest struct {
UserId string `json:"user_id"`
LeaderId string `json:"leader_id"`
}

type DeleteMemberGroupResponse struct {
Group *Group `json:"group"`
}

type LeaveGroupRequest struct {
UserId string `json:"user_id"`
}

type LeaveGroupResponse struct {
Group *Group `json:"group"`
}

type SelectBaanRequest struct {
UserId string `json:"user_id"`
Baans []string `json:"baans"`
}

type SelectBaanResponse struct {
Success bool `json:"success"`
}
248 changes: 248 additions & 0 deletions internal/group/group.handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package group

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 {
FindOne(c router.Context)
FindByToken(c router.Context)
Update(c router.Context)
Join(c router.Context)
DeleteMember(c router.Context)
Leave(c router.Context)
SelectBaan(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) DeleteMember(c router.Context) {
body := &dto.DeleteMemberGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("DeleteMember").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("DeleteMember").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.DeleteMemberGroupRequest{
UserId: body.UserId,
LeaderId: body.LeaderId,
}

res, appErr := h.svc.DeleteMember(req)
if appErr != nil {
h.log.Named("DeleteMember").Error("DeleteMember: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.DeleteMemberGroupResponse{
Group: res.Group,
})
}

func (h *handlerImpl) FindByToken(c router.Context) {
body := &dto.FindByTokenGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("FindByToken").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("FindByToken").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.FindByTokenGroupRequest{
Token: body.Token,
}

res, appErr := h.svc.FindByToken(req)
if appErr != nil {
h.log.Named("FindByToken").Error("FindByToken: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.FindByTokenGroupResponse{
Id: res.Id,
Token: res.Token,
Leader: res.Leader,
})
}

func (h *handlerImpl) FindOne(c router.Context) {
userId := c.Param("id")
if userId == "" {
c.BadRequestError("url parameter 'user_id' not found")
}

req := &dto.FindOneGroupRequest{
UserId: userId,
}

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

res, appErr := h.svc.FindOne(req)
if appErr != nil {
h.log.Named("FindOne").Error("FindOne: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.FindOneGroupResponse{
Group: res.Group,
})
}

func (h *handlerImpl) Join(c router.Context) {
body := &dto.JoinGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("Join").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("Join").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.JoinGroupRequest{
Token: body.Token,
UserId: body.UserId,
}

res, appErr := h.svc.Join(req)
if appErr != nil {
h.log.Named("Join").Error("Join: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.JoinGroupResponse{
Group: res.Group,
})
}

func (h *handlerImpl) Leave(c router.Context) {
body := &dto.LeaveGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("Leave").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("Leave").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.LeaveGroupRequest{
UserId: body.UserId,
}

res, appErr := h.svc.Leave(req)
if appErr != nil {
h.log.Named("Leave").Error("Leave: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.LeaveGroupResponse{
Group: res.Group,
})
}

func (h *handlerImpl) SelectBaan(c router.Context) {
body := &dto.SelectBaanRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("SelectBaan").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("SelectBaan").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.SelectBaanRequest{
UserId: body.UserId,
Baans: body.Baans,
}

res, appErr := h.svc.SelectBaan(req)
if appErr != nil {
h.log.Named("SelectBaan").Error("SelectBaan: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.SelectBaanResponse{
Success: res.Success,
})
}

func (h *handlerImpl) Update(c router.Context) {
body := &dto.UpdateGroupRequest{}
if err := c.Bind(body); err != nil {
h.log.Named("Update").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("Update").Error("Validate: ", zap.Strings("errorList", errorList))
c.BadRequestError(strings.Join(errorList, ", "))
return
}

req := &dto.UpdateGroupRequest{
Group: body.Group,
}

res, appErr := h.svc.Update(req)
if appErr != nil {
h.log.Named("Update").Error("Update: ", zap.Error(appErr))
c.ResponseError(appErr)
return
}

c.JSON(http.StatusOK, &dto.UpdateGroupResponse{
Group: res.Group,
})
}
Loading

0 comments on commit 7932ab3

Please sign in to comment.