Add quota and limit for events
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -36,6 +36,7 @@ event:
|
||||
database_failed: "00001"
|
||||
join:
|
||||
event_invalid: "00001"
|
||||
limit_exceeded: "00002"
|
||||
attendance:
|
||||
list_error: "00001"
|
||||
kyc_info_decrypt_failed: "00002"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user