Skip to content

Commit

Permalink
st, ed
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jul 22, 2024
1 parent 7630af8 commit e573abf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ func LoadConfig() (*Config, error) {
if err != nil {
return nil, err
}
fmt.Printf("Parsed Checkin start time: %v", parsedCheckinTime)
fmt.Printf("Parsed Checkin start time: %v\n", parsedCheckinTime)

parsedRpkmStartTime, err := parseLocalTime("REG_RPKM_START")
if err != nil {
return nil, err
}
fmt.Printf("Parsed RPKM start time: %v", parsedRpkmStartTime)
fmt.Printf("Parsed RPKM start time: %v\n", parsedRpkmStartTime)

parsedRpkmEndTime, err := parseLocalTime("REG_RPKM_END")
if err != nil {
return nil, err
}
fmt.Printf("Parsed RPKM end time: %v", parsedRpkmEndTime)
fmt.Printf("Parsed RPKM end time: %v\n", parsedRpkmEndTime)

regConfig := RegConfig{
CheckinStart: parsedCheckinTime,
Expand Down
38 changes: 16 additions & 22 deletions internal/group/group.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ type handlerImpl struct {
// @Failure 500 {object} apperror.AppError
// @Router /group/{userId} [get]
func (h *handlerImpl) FindByUserId(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

userId := c.Param("userId")
if userId == "" {
c.BadRequestError("url parameter 'user_id' not found")
Expand Down Expand Up @@ -99,11 +94,6 @@ func (h *handlerImpl) FindByUserId(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /group/token [get]
func (h *handlerImpl) FindByToken(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

token := c.Query("token")
if token == "" {
c.BadRequestError("url parameter 'token' not found")
Expand Down Expand Up @@ -143,8 +133,9 @@ func (h *handlerImpl) FindByToken(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /group/{userId} [put]
func (h *handlerImpl) UpdateConfirm(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}

Expand Down Expand Up @@ -198,8 +189,9 @@ func (h *handlerImpl) UpdateConfirm(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /group/join [post]
func (h *handlerImpl) Join(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}

Expand Down Expand Up @@ -248,8 +240,9 @@ func (h *handlerImpl) Join(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /group/leave [post]
func (h *handlerImpl) Leave(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}

Expand Down Expand Up @@ -297,8 +290,9 @@ func (h *handlerImpl) Leave(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /group/delete-member [delete]
func (h *handlerImpl) DeleteMember(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}

Expand Down Expand Up @@ -332,17 +326,17 @@ func (h *handlerImpl) DeleteMember(c context.Ctx) {
})
}

func (h *handlerImpl) checkRegTime() bool {
func (h *handlerImpl) checkRegTime() (bool, string) {
nowUTC := time.Now().UTC()
gmtPlus7Location := time.FixedZone("GMT+7", 7*60*60)
nowGMTPlus7 := nowUTC.In(gmtPlus7Location)
if nowGMTPlus7.Before(h.regConf.RpkmStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
return false, "Registration hasn't started"
} else if nowGMTPlus7.After(h.regConf.RpkmEnd) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration has ended")
return false
return false, "Registration has ended"
}

return true
return true, ""
}
33 changes: 13 additions & 20 deletions internal/selection/selection.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func NewHandler(svc Service, groupSvc group.Service, regConf *config.RegConfig,
// @Failure 500 {object} apperror.AppError
// @Router /selection [post]
func (h *handlerImpl) Create(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}
h.checkGroupLeader(c)
Expand Down Expand Up @@ -99,11 +100,6 @@ func (h *handlerImpl) Create(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /selection/{groupId} [get]
func (h *handlerImpl) FindByGroupId(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

groupId := c.Param("groupId")
if groupId == "" {
h.log.Named("FindByGroupIdSelection").Error("Param: groupId not found")
Expand Down Expand Up @@ -147,8 +143,9 @@ func (h *handlerImpl) FindByGroupId(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /selection [patch]
func (h *handlerImpl) Update(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}
h.checkGroupLeader(c)
Expand Down Expand Up @@ -194,8 +191,9 @@ func (h *handlerImpl) Update(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /selection [delete]
func (h *handlerImpl) Delete(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
ok, msg := h.checkRegTime()
if !ok {
c.ForbiddenError(msg)
return
}
h.checkGroupLeader(c)
Expand Down Expand Up @@ -238,11 +236,6 @@ func (h *handlerImpl) Delete(c context.Ctx) {
// @Failure 500 {object} apperror.AppError
// @Router /selection/count-by-baan [get]
func (h *handlerImpl) CountByBaanId(c context.Ctx) {
if !h.checkRegTime() {
c.ForbiddenError("Registration hasn't started")
return
}

res, appErr := h.svc.CountByBaanId()
if appErr != nil {
h.log.Named("CountByBaanId").Error("CountByBaanId: ", zap.Error(appErr))
Expand Down Expand Up @@ -276,17 +269,17 @@ func (h *handlerImpl) checkGroupLeader(c context.Ctx) {
c.Next()
}

func (h *handlerImpl) checkRegTime() bool {
func (h *handlerImpl) checkRegTime() (bool, string) {
nowUTC := time.Now().UTC()
gmtPlus7Location := time.FixedZone("GMT+7", 7*60*60)
nowGMTPlus7 := nowUTC.In(gmtPlus7Location)
if nowGMTPlus7.Before(h.regConf.RpkmStart) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration hasn't started")
return false
return false, "Registration hasn't started"
} else if nowGMTPlus7.After(h.regConf.RpkmEnd) {
h.log.Named("checkRegTime").Warn("Forbidden: Registration has ended")
return false
return false, "Registration has ended"
}

return true
return true, ""
}

0 comments on commit e573abf

Please sign in to comment.