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"
|
// @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 400 {object} utils.RespStatus{data=nil} "Invalid Input or UUID Parse Failed"
|
||||||
// @Failure 401 {object} utils.RespStatus{data=nil} "Missing User ID / Unauthorized"
|
// @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"
|
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error / Database Error"
|
||||||
// @Security ApiKeyAuth
|
// @Security ApiKeyAuth
|
||||||
// @Router /event/join [post]
|
// @Router /event/join [post]
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ event:
|
|||||||
database_failed: "00001"
|
database_failed: "00001"
|
||||||
join:
|
join:
|
||||||
event_invalid: "00001"
|
event_invalid: "00001"
|
||||||
|
limit_exceeded: "00002"
|
||||||
attendance:
|
attendance:
|
||||||
list_error: "00001"
|
list_error: "00001"
|
||||||
kyc_info_decrypt_failed: "00002"
|
kyc_info_decrypt_failed: "00002"
|
||||||
|
|||||||
@@ -141,6 +141,21 @@ func (self *Attendance) GetAttendanceListByEventId(ctx context.Context, eventId
|
|||||||
return &result, err
|
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) {
|
func (self *Attendance) Update(ctx context.Context, attendanceId uuid.UUID) (*Attendance, error) {
|
||||||
var attendance Attendance
|
var attendance Attendance
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ type Event struct {
|
|||||||
Thumbnail string `json:"thumbnail" gorm:"type:varchar(255)"`
|
Thumbnail string `json:"thumbnail" gorm:"type:varchar(255)"`
|
||||||
Owner uuid.UUID `json:"owner" gorm:"type:uuid;index;not null"`
|
Owner uuid.UUID `json:"owner" gorm:"type:uuid;index;not null"`
|
||||||
EnableKYC bool `json:"enable_kyc" gorm:"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 {
|
type EventIndexDoc struct {
|
||||||
|
|||||||
@@ -153,8 +153,44 @@ func (self *EventServiceImpl) JoinEvent(payload *EventJoinPayload) (result *Even
|
|||||||
|
|
||||||
attendenceData.SetUserId(userId)
|
attendenceData.SetUserId(userId)
|
||||||
attendenceData.SetEventId(eventId)
|
attendenceData.SetEventId(eventId)
|
||||||
attendenceData.SetRole("notmal")
|
attendenceCount, err := attendenceData.CountUsersByEventID(payload.Context, eventId)
|
||||||
attendenceData.SetState("success")
|
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)
|
err = attendenceData.Create(payload.Context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user