Add isjoined to event info and event list
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2026-02-05 17:43:20 +08:00
parent a7a6b7aa4e
commit 4f0b4262ed
9 changed files with 191 additions and 19 deletions

View File

@@ -26,6 +26,35 @@ import (
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /event/info [get] // @Router /event/info [get]
func (self *EventHandler) Info(c *gin.Context) { 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") eventIdOrig := c.Query("event_id")
eventId, err := uuid.Parse(eventIdOrig) eventId, err := uuid.Parse(eventIdOrig)
if err != nil { if err != nil {
@@ -45,6 +74,7 @@ func (self *EventHandler) Info(c *gin.Context) {
result := self.svc.GetEventInfo(&service_event.EventInfoPayload{ result := self.svc.GetEventInfo(&service_event.EventInfoPayload{
Context: c, Context: c,
UserId: userId,
Data: &service_event.EventInfoData{ Data: &service_event.EventInfoData{
EventId: eventId, EventId: eventId,
}, },

View File

@@ -6,6 +6,7 @@ import (
"nixcn-cms/utils" "nixcn-cms/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
// List retrieves a paginated list of events from the database. // List retrieves a paginated list of events from the database.
@@ -25,6 +26,35 @@ import (
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /event/list [get] // @Router /event/list [get]
func (self *EventHandler) List(c *gin.Context) { 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 { type ListQuery struct {
Limit *string `form:"limit"` Limit *string `form:"limit"`
Offset *string `form:"offset"` Offset *string `form:"offset"`
@@ -49,8 +79,11 @@ func (self *EventHandler) List(c *gin.Context) {
// Prepare payload for the service layer // Prepare payload for the service layer
eventListPayload := &service_event.EventListPayload{ eventListPayload := &service_event.EventListPayload{
Context: c, Context: c,
UserId: userId,
Data: &service_event.EventListData{
Limit: query.Limit, Limit: query.Limit,
Offset: query.Offset, Offset: query.Offset,
},
} }
// Call the service implementation // Call the service implementation

View File

@@ -141,6 +141,31 @@ func (self *Attendance) GetAttendanceListByEventId(ctx context.Context, eventId
return &result, err 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) { func (self *Attendance) CountUsersByEventID(ctx context.Context, eventID uuid.UUID) (int64, error) {
var count int64 var count int64

View File

@@ -32,7 +32,8 @@ type EventIndexDoc struct {
StartTime time.Time `json:"start_time"` StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"` EndTime time.Time `json:"end_time"`
Thumbnail string `json:"thumbnail"` Thumbnail string `json:"thumbnail"`
RequireKyc bool `json:"require_kyc"` EnableKYC bool `json:"enable_kyc"`
IsJoined bool `json:"is_joined"`
JoinCount int64 `json:"join_count"` JoinCount int64 `json:"join_count"`
CheckinCount int64 `json:"checkin_count"` CheckinCount int64 `json:"checkin_count"`
} }
@@ -98,7 +99,7 @@ func (self *Event) FastListEvents(ctx context.Context, limit, offset int64) (*[]
err := Database.WithContext(ctx). err := Database.WithContext(ctx).
Model(&Event{}). 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)). Limit(int(limit)).
Offset(int(offset)). Offset(int(offset)).
Scan(&results).Error Scan(&results).Error

View File

@@ -2002,21 +2002,24 @@ const docTemplate = `{
"description": { "description": {
"type": "string" "type": "string"
}, },
"enable_kyc": {
"type": "boolean"
},
"end_time": { "end_time": {
"type": "string" "type": "string"
}, },
"event_id": { "event_id": {
"type": "string" "type": "string"
}, },
"is_joined": {
"type": "boolean"
},
"join_count": { "join_count": {
"type": "integer" "type": "integer"
}, },
"name": { "name": {
"type": "string" "type": "string"
}, },
"require_kyc": {
"type": "boolean"
},
"start_time": { "start_time": {
"type": "string" "type": "string"
}, },

View File

@@ -2000,21 +2000,24 @@
"description": { "description": {
"type": "string" "type": "string"
}, },
"enable_kyc": {
"type": "boolean"
},
"end_time": { "end_time": {
"type": "string" "type": "string"
}, },
"event_id": { "event_id": {
"type": "string" "type": "string"
}, },
"is_joined": {
"type": "boolean"
},
"join_count": { "join_count": {
"type": "integer" "type": "integer"
}, },
"name": { "name": {
"type": "string" "type": "string"
}, },
"require_kyc": {
"type": "boolean"
},
"start_time": { "start_time": {
"type": "string" "type": "string"
}, },

View File

@@ -6,16 +6,18 @@ definitions:
type: integer type: integer
description: description:
type: string type: string
enable_kyc:
type: boolean
end_time: end_time:
type: string type: string
event_id: event_id:
type: string type: string
is_joined:
type: boolean
join_count: join_count:
type: integer type: integer
name: name:
type: string type: string
require_kyc:
type: boolean
start_time: start_time:
type: string type: string
thumbnail: thumbnail:

View File

@@ -7,6 +7,7 @@ import (
"nixcn-cms/service/shared" "nixcn-cms/service/shared"
"github.com/google/uuid" "github.com/google/uuid"
"gorm.io/gorm"
) )
type EventInfoData struct { type EventInfoData struct {
@@ -15,6 +16,7 @@ type EventInfoData struct {
type EventInfoPayload struct { type EventInfoPayload struct {
Context context.Context Context context.Context
UserId uuid.UUID
Data *EventInfoData Data *EventInfoData
} }
@@ -87,6 +89,32 @@ func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *E
return 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{ result = &EventInfoResult{
Common: shared.CommonResult{ Common: shared.CommonResult{
HttpCode: 200, HttpCode: 200,
@@ -106,7 +134,8 @@ func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *E
StartTime: event.StartTime, StartTime: event.StartTime,
EndTime: event.EndTime, EndTime: event.EndTime,
Thumbnail: event.Thumbnail, Thumbnail: event.Thumbnail,
RequireKyc: event.EnableKYC, EnableKYC: event.EnableKYC,
IsJoined: isJoined,
JoinCount: joinCount, JoinCount: joinCount,
CheckinCount: checkinCount, CheckinCount: checkinCount,
}, },

View File

@@ -6,12 +6,19 @@ import (
"nixcn-cms/internal/exception" "nixcn-cms/internal/exception"
"nixcn-cms/service/shared" "nixcn-cms/service/shared"
"strconv" "strconv"
"github.com/google/uuid"
) )
type EventListData struct {
Limit *string `json:"limit"`
Offset *string `json:"offset"`
}
type EventListPayload struct { type EventListPayload struct {
Context context.Context Context context.Context
Limit *string UserId uuid.UUID
Offset *string Data *EventListData
} }
type EventListResult struct { type EventListResult struct {
@@ -21,14 +28,14 @@ type EventListResult struct {
func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *EventListResult) { func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *EventListResult) {
var limit string var limit string
if payload.Limit == nil || *payload.Limit == "" { if payload.Data.Limit == nil || *payload.Data.Limit == "" {
limit = "20" limit = "20"
} else { } else {
limit = *payload.Limit limit = *payload.Data.Limit
} }
var offset string var offset string
if payload.Offset == nil || *payload.Offset == "" { if payload.Data.Offset == nil || *payload.Data.Offset == "" {
exception := new(exception.Builder). exception := new(exception.Builder).
SetStatus(exception.StatusUser). SetStatus(exception.StatusUser).
SetService(exception.ServiceEvent). SetService(exception.ServiceEvent).
@@ -46,9 +53,10 @@ func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *Eve
Data: nil, Data: nil,
} }
} else { } else {
offset = *payload.Offset offset = *payload.Data.Offset
} }
// 3. 转换数字
limitNum, err := strconv.Atoi(limit) limitNum, err := strconv.Atoi(limit)
if err != nil { if err != nil {
exc := new(exception.Builder). 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). successExc := new(exception.Builder).
SetStatus(exception.StatusSuccess). SetStatus(exception.StatusSuccess).
SetService(exception.ServiceEvent). SetService(exception.ServiceEvent).