From 4f0b4262ede98917ed7e8f5e317600a1b2d74036 Mon Sep 17 00:00:00 2001 From: Asai Neko Date: Thu, 5 Feb 2026 17:43:20 +0800 Subject: [PATCH] Add isjoined to event info and event list Signed-off-by: Asai Neko --- api/event/info.go | 30 +++++++++++++ api/event/list.go | 37 +++++++++++++++- data/attendance.go | 25 +++++++++++ data/event.go | 5 ++- docs/docs.go | 9 ++-- docs/swagger.json | 9 ++-- docs/swagger.yaml | 6 ++- service/service_event/get_event_info.go | 31 ++++++++++++- service/service_event/list_events.go | 58 ++++++++++++++++++++++--- 9 files changed, 191 insertions(+), 19 deletions(-) diff --git a/api/event/info.go b/api/event/info.go index 5117a3b..ae53ae9 100644 --- a/api/event/info.go +++ b/api/event/info.go @@ -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, }, diff --git a/api/event/list.go b/api/event/list.go index 238df2f..1c7ee71 100644 --- a/api/event/list.go +++ b/api/event/list.go @@ -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, - Limit: query.Limit, - Offset: query.Offset, + UserId: userId, + Data: &service_event.EventListData{ + Limit: query.Limit, + Offset: query.Offset, + }, } // Call the service implementation diff --git a/data/attendance.go b/data/attendance.go index b4207d4..343b23d 100644 --- a/data/attendance.go +++ b/data/attendance.go @@ -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 diff --git a/data/event.go b/data/event.go index 4264def..968cecc 100644 --- a/data/event.go +++ b/data/event.go @@ -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 diff --git a/docs/docs.go b/docs/docs.go index 732587f..66eb775 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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" }, diff --git a/docs/swagger.json b/docs/swagger.json index b0382a0..87a28f5 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8e0b182..07a7e98 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: diff --git a/service/service_event/get_event_info.go b/service/service_event/get_event_info.go index fa24d69..f87ec2d 100644 --- a/service/service_event/get_event_info.go +++ b/service/service_event/get_event_info.go @@ -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, }, diff --git a/service/service_event/list_events.go b/service/service_event/list_events.go index c8221c2..781f035 100644 --- a/service/service_event/list_events.go +++ b/service/service_event/list_events.go @@ -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).