diff --git a/api/event/join.go b/api/event/join.go index ab4f2dd..05ae356 100644 --- a/api/event/join.go +++ b/api/event/join.go @@ -20,7 +20,7 @@ import ( // @Success 200 {object} utils.RespStatus{data=nil} "Successfully joined the event" // @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input or UUID Parse Failed" // @Failure 401 {object} utils.RespStatus{data=nil} "Missing User ID / Unauthorized" -// @Failure 403 {object} utils.RespStatus{data=nil} "Unauthorized / Missing User ID" +// @Failure 403 {object} utils.RespStatus{data=nil} "Unauthorized / Missing User ID / Event Limit Exceeded" // @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error / Database Error" // @Security ApiKeyAuth // @Router /event/join [post] diff --git a/cmd/gen_exception/definitions/specific.yaml b/cmd/gen_exception/definitions/specific.yaml index df991b8..c374215 100644 --- a/cmd/gen_exception/definitions/specific.yaml +++ b/cmd/gen_exception/definitions/specific.yaml @@ -36,6 +36,7 @@ event: database_failed: "00001" join: event_invalid: "00001" + limit_exceeded: "00002" attendance: list_error: "00001" kyc_info_decrypt_failed: "00002" diff --git a/data/attendance.go b/data/attendance.go index 8eeb4d0..dd128df 100644 --- a/data/attendance.go +++ b/data/attendance.go @@ -141,6 +141,21 @@ func (self *Attendance) GetAttendanceListByEventId(ctx context.Context, eventId return &result, err } +func (self *Attendance) CountUsersByEventID(ctx context.Context, eventID uuid.UUID) (int64, error) { + var count int64 + + err := Database.WithContext(ctx). + Model(&Attendance{}). + Where("event_id = ?", eventID). + Count(&count).Error + + if err != nil { + return 0, err + } + + return count, nil +} + func (self *Attendance) Update(ctx context.Context, attendanceId uuid.UUID) (*Attendance, error) { var attendance Attendance diff --git a/data/event.go b/data/event.go index fcef9ae..ea326a5 100644 --- a/data/event.go +++ b/data/event.go @@ -20,6 +20,8 @@ type Event struct { Thumbnail string `json:"thumbnail" gorm:"type:varchar(255)"` Owner uuid.UUID `json:"owner" gorm:"type:uuid;index;not null"` EnableKYC bool `json:"enable_kyc" gorm:"not null"` + Quota int64 `json:"quota" gorm:"not null"` + Limit int64 `json:"limit" gorm:"not null"` } type EventIndexDoc struct { diff --git a/service/service_event/join_event.go b/service/service_event/join_event.go index 77d8b72..0718fce 100644 --- a/service/service_event/join_event.go +++ b/service/service_event/join_event.go @@ -153,8 +153,44 @@ func (self *EventServiceImpl) JoinEvent(payload *EventJoinPayload) (result *Even attendenceData.SetUserId(userId) attendenceData.SetEventId(eventId) - attendenceData.SetRole("notmal") - attendenceData.SetState("success") + attendenceCount, err := attendenceData.CountUsersByEventID(payload.Context, eventId) + if err != nil { + return &EventJoinResult{ + Common: shared.CommonResult{ + HttpCode: 500, + Exception: new(exception.Builder). + SetStatus(exception.StatusServer). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorDatabase). + SetError(err). + Throw(payload.Context), + }, + } + } + + if attendenceCount >= eventData.Quota { + if attendenceCount >= eventData.Limit { + return &EventJoinResult{ + Common: shared.CommonResult{ + HttpCode: 403, + Exception: new(exception.Builder). + SetStatus(exception.StatusUser). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceJoin). + SetType(exception.TypeSpecific). + SetOriginal(exception.EventJoinLimitExceeded). + SetError(errors.New("event limit exceeded")). + Throw(payload.Context), + }, + } + } + attendenceData.SetState("out_of_limit") + } else { + attendenceData.SetState("success") + } + attendenceData.SetRole("normal") err = attendenceData.Create(payload.Context) if err != nil {