Files
cms-server/service/service_event/list_events.go
Asai Neko 4f0b4262ed
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished
Add isjoined to event info and event list
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-02-05 17:43:20 +08:00

178 lines
4.1 KiB
Go

package service_event
import (
"context"
"nixcn-cms/data"
"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
UserId uuid.UUID
Data *EventListData
}
type EventListResult struct {
Common shared.CommonResult
Data *[]data.EventIndexDoc `json:"event_list"`
}
func (self *EventServiceImpl) ListEvents(payload *EventListPayload) (result *EventListResult) {
var limit string
if payload.Data.Limit == nil || *payload.Data.Limit == "" {
limit = "20"
} else {
limit = *payload.Data.Limit
}
var offset string
if payload.Data.Offset == nil || *payload.Data.Offset == "" {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceList).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
SetError(nil).
Throw(payload.Context)
return &EventListResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exception,
},
Data: nil,
}
} else {
offset = *payload.Data.Offset
}
// 3. 转换数字
limitNum, err := strconv.Atoi(limit)
if err != nil {
exc := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceList).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
SetError(err).
Throw(payload.Context)
return &EventListResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exc,
},
Data: nil,
}
}
offsetNum, err := strconv.Atoi(offset)
if err != nil {
exc := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceList).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorInvalidInput).
SetError(err).
Throw(payload.Context)
return &EventListResult{
Common: shared.CommonResult{
HttpCode: 400,
Exception: exc,
},
Data: nil,
}
}
eventList, err := new(data.Event).
FastListEvents(payload.Context, int64(limitNum), int64(offsetNum))
if err != nil {
exc := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceEvent).
SetEndpoint(exception.EndpointEventServiceList).
SetType(exception.TypeSpecific).
SetOriginal(exception.EventListDatabaseFailed).
SetError(err).
Throw(payload.Context)
return &EventListResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exc,
},
Data: nil,
}
}
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).
SetEndpoint(exception.EndpointEventServiceList).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
SetError(nil).
Throw(payload.Context)
result = &EventListResult{
Common: shared.CommonResult{
HttpCode: 200,
Exception: successExc,
},
Data: eventList,
}
return
}