-
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 #5 from isd-sgcu/feat/group
Feat/group
- Loading branch information
Showing
8 changed files
with
633 additions
and
1 deletion.
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
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"` | ||
} |
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,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, | ||
}) | ||
} |
Oops, something went wrong.