Add isjoined to event info and event list
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -26,6 +26,35 @@ import (
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /event/info [get]
|
||||
func (self *EventHandler) Info(c *gin.Context) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 403, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
SetError(err).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
eventIdOrig := c.Query("event_id")
|
||||
eventId, err := uuid.Parse(eventIdOrig)
|
||||
if err != nil {
|
||||
@@ -45,6 +74,7 @@ func (self *EventHandler) Info(c *gin.Context) {
|
||||
|
||||
result := self.svc.GetEventInfo(&service_event.EventInfoPayload{
|
||||
Context: c,
|
||||
UserId: userId,
|
||||
Data: &service_event.EventInfoData{
|
||||
EventId: eventId,
|
||||
},
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// List retrieves a paginated list of events from the database.
|
||||
@@ -25,6 +26,35 @@ import (
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /event/list [get]
|
||||
func (self *EventHandler) List(c *gin.Context) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 403, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
SetError(err).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
type ListQuery struct {
|
||||
Limit *string `form:"limit"`
|
||||
Offset *string `form:"offset"`
|
||||
@@ -49,8 +79,11 @@ func (self *EventHandler) List(c *gin.Context) {
|
||||
// Prepare payload for the service layer
|
||||
eventListPayload := &service_event.EventListPayload{
|
||||
Context: c,
|
||||
UserId: userId,
|
||||
Data: &service_event.EventListData{
|
||||
Limit: query.Limit,
|
||||
Offset: query.Offset,
|
||||
},
|
||||
}
|
||||
|
||||
// Call the service implementation
|
||||
|
||||
@@ -141,6 +141,31 @@ func (self *Attendance) GetAttendanceListByEventId(ctx context.Context, eventId
|
||||
return &result, err
|
||||
}
|
||||
|
||||
func (self *Attendance) GetJoinedEventIDs(ctx context.Context, userId uuid.UUID, eventIds []uuid.UUID) (map[uuid.UUID]bool, error) {
|
||||
joinedMap := make(map[uuid.UUID]bool)
|
||||
|
||||
if len(eventIds) == 0 {
|
||||
return joinedMap, nil
|
||||
}
|
||||
|
||||
var foundEventIds []uuid.UUID
|
||||
|
||||
err := Database.WithContext(ctx).
|
||||
Model(&Attendance{}).
|
||||
Where("user_id = ? AND event_id IN ?", userId, eventIds).
|
||||
Pluck("event_id", &foundEventIds).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, id := range foundEventIds {
|
||||
joinedMap[id] = true
|
||||
}
|
||||
|
||||
return joinedMap, nil
|
||||
}
|
||||
|
||||
func (self *Attendance) CountUsersByEventID(ctx context.Context, eventID uuid.UUID) (int64, error) {
|
||||
var count int64
|
||||
|
||||
|
||||
@@ -32,7 +32,8 @@ type EventIndexDoc struct {
|
||||
StartTime time.Time `json:"start_time"`
|
||||
EndTime time.Time `json:"end_time"`
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
RequireKyc bool `json:"require_kyc"`
|
||||
EnableKYC bool `json:"enable_kyc"`
|
||||
IsJoined bool `json:"is_joined"`
|
||||
JoinCount int64 `json:"join_count"`
|
||||
CheckinCount int64 `json:"checkin_count"`
|
||||
}
|
||||
@@ -98,7 +99,7 @@ func (self *Event) FastListEvents(ctx context.Context, limit, offset int64) (*[]
|
||||
|
||||
err := Database.WithContext(ctx).
|
||||
Model(&Event{}).
|
||||
Select("event_id", "name", "type", "description", "start_time", "end_time").
|
||||
Select("event_id", "name", "type", "description", "start_time", "end_time", "enable_kyc").
|
||||
Limit(int(limit)).
|
||||
Offset(int(offset)).
|
||||
Scan(&results).Error
|
||||
|
||||
@@ -2002,21 +2002,24 @@ const docTemplate = `{
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
"enable_kyc": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"end_time": {
|
||||
"type": "string"
|
||||
},
|
||||
"event_id": {
|
||||
"type": "string"
|
||||
},
|
||||
"is_joined": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"join_count": {
|
||||
"type": "integer"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"require_kyc": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"start_time": {
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
@@ -2000,21 +2000,24 @@
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
"enable_kyc": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"end_time": {
|
||||
"type": "string"
|
||||
},
|
||||
"event_id": {
|
||||
"type": "string"
|
||||
},
|
||||
"is_joined": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"join_count": {
|
||||
"type": "integer"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"require_kyc": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"start_time": {
|
||||
"type": "string"
|
||||
},
|
||||
|
||||
@@ -6,16 +6,18 @@ definitions:
|
||||
type: integer
|
||||
description:
|
||||
type: string
|
||||
enable_kyc:
|
||||
type: boolean
|
||||
end_time:
|
||||
type: string
|
||||
event_id:
|
||||
type: string
|
||||
is_joined:
|
||||
type: boolean
|
||||
join_count:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
require_kyc:
|
||||
type: boolean
|
||||
start_time:
|
||||
type: string
|
||||
thumbnail:
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"nixcn-cms/service/shared"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type EventInfoData struct {
|
||||
@@ -15,6 +16,7 @@ type EventInfoData struct {
|
||||
|
||||
type EventInfoPayload struct {
|
||||
Context context.Context
|
||||
UserId uuid.UUID
|
||||
Data *EventInfoData
|
||||
}
|
||||
|
||||
@@ -87,6 +89,32 @@ func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *E
|
||||
return
|
||||
}
|
||||
|
||||
var isJoined bool
|
||||
joinedInfo, err := new(data.Attendance).GetAttendance(payload.Context, payload.UserId, payload.Data.EventId)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorDatabase).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &EventInfoResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
} else if err == gorm.ErrRecordNotFound {
|
||||
isJoined = false
|
||||
} else if joinedInfo.AttendanceId != uuid.Nil {
|
||||
isJoined = true
|
||||
}
|
||||
|
||||
result = &EventInfoResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
@@ -106,7 +134,8 @@ func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *E
|
||||
StartTime: event.StartTime,
|
||||
EndTime: event.EndTime,
|
||||
Thumbnail: event.Thumbnail,
|
||||
RequireKyc: event.EnableKYC,
|
||||
EnableKYC: event.EnableKYC,
|
||||
IsJoined: isJoined,
|
||||
JoinCount: joinCount,
|
||||
CheckinCount: checkinCount,
|
||||
},
|
||||
|
||||
@@ -6,12 +6,19 @@ import (
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service/shared"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type EventListData struct {
|
||||
Limit *string `json:"limit"`
|
||||
Offset *string `json:"offset"`
|
||||
}
|
||||
|
||||
type EventListPayload struct {
|
||||
Context context.Context
|
||||
Limit *string
|
||||
Offset *string
|
||||
UserId uuid.UUID
|
||||
Data *EventListData
|
||||
}
|
||||
|
||||
type EventListResult struct {
|
||||
@@ -21,14 +28,14 @@ type EventListResult struct {
|
||||
|
||||
func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *EventListResult) {
|
||||
var limit string
|
||||
if payload.Limit == nil || *payload.Limit == "" {
|
||||
if payload.Data.Limit == nil || *payload.Data.Limit == "" {
|
||||
limit = "20"
|
||||
} else {
|
||||
limit = *payload.Limit
|
||||
limit = *payload.Data.Limit
|
||||
}
|
||||
|
||||
var offset string
|
||||
if payload.Offset == nil || *payload.Offset == "" {
|
||||
if payload.Data.Offset == nil || *payload.Data.Offset == "" {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceEvent).
|
||||
@@ -46,9 +53,10 @@ func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *Eve
|
||||
Data: nil,
|
||||
}
|
||||
} else {
|
||||
offset = *payload.Offset
|
||||
offset = *payload.Data.Offset
|
||||
}
|
||||
|
||||
// 3. 转换数字
|
||||
limitNum, err := strconv.Atoi(limit)
|
||||
if err != nil {
|
||||
exc := new(exception.Builder).
|
||||
@@ -110,6 +118,44 @@ func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *Eve
|
||||
}
|
||||
}
|
||||
|
||||
if eventList != nil && len(*eventList) > 0 {
|
||||
var eventIds []uuid.UUID
|
||||
for _, e := range *eventList {
|
||||
parsedId, parseErr := uuid.Parse(e.EventId)
|
||||
if parseErr == nil {
|
||||
eventIds = append(eventIds, parsedId)
|
||||
}
|
||||
}
|
||||
|
||||
joinedMap, err := new(data.Attendance).GetJoinedEventIDs(payload.Context, payload.UserId, eventIds)
|
||||
|
||||
if err != nil {
|
||||
exc := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceEvent).
|
||||
SetEndpoint(exception.EndpointEventServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorDatabase).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
return &EventListResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exc,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
}
|
||||
|
||||
for i := range *eventList {
|
||||
currentIdStr := (*eventList)[i].EventId
|
||||
currentIdUuid, _ := uuid.Parse(currentIdStr)
|
||||
|
||||
(*eventList)[i].IsJoined = joinedMap[currentIdUuid]
|
||||
}
|
||||
}
|
||||
|
||||
successExc := new(exception.Builder).
|
||||
SetStatus(exception.StatusSuccess).
|
||||
SetService(exception.ServiceEvent).
|
||||
|
||||
Reference in New Issue
Block a user