Files
cms-server/data/event.go
Asai Neko 2af9d23aba
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished
Add join count to event api
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-02-05 14:35:32 +08:00

111 lines
2.9 KiB
Go

package data
import (
"context"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type Event struct {
Id uint `json:"id" gorm:"primarykey;autoincrement"`
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueIndex;not null"`
EventId uuid.UUID `json:"event_id" gorm:"type:uuid;uniqueIndex;not null"`
Name string `json:"name" gorm:"type:varchar(255);index;not null"`
Type string `json:"type" gotm:"type:varchar(255);index;not null"` // official | party
Description string `json:"description" gorm:"type:text;not null"`
StartTime time.Time `json:"start_time" gorm:"index"`
EndTime time.Time `json:"end_time" gorm:"index"`
Thumbnail string `json:"thumbnail" gorm:"type:varchar(255)"`
Owner uuid.UUID `json:"owner" gorm:"type:uuid;index;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 {
EventId string `json:"event_id"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Thumbnail string `json:"thumbnail"`
JoinCount int64 `json:"join_count"`
CheckinCount int64 `json:"checkin_count"`
}
func (self *Event) GetEventById(ctx context.Context, eventId uuid.UUID) (*Event, error) {
var event Event
err := Database.WithContext(ctx).
Where("event_id = ?", eventId).
First(&event).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
}
return nil, err
}
return &event, nil
}
func (self *Event) UpdateEventById(ctx context.Context, eventId uuid.UUID) error {
// DB transaction
if err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// Update by business key
if err := tx.
Model(&Event{}).
Where("event_id = ?", eventId).
Updates(self).Error; err != nil {
return err
}
// Reload to ensure struct is fresh
return tx.
Where("event_id = ?", eventId).
First(self).Error
}); err != nil {
return err
}
return nil
}
func (self *Event) Create(ctx context.Context) error {
self.UUID = uuid.New()
self.EventId = uuid.New()
// DB transaction only
if err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Create(self).Error; err != nil {
return err
}
return nil
}); err != nil {
return err
}
return nil
}
func (self *Event) FastListEvents(ctx context.Context, limit, offset int64) (*[]EventIndexDoc, error) {
var results []EventIndexDoc
err := Database.WithContext(ctx).
Model(&Event{}).
Select("event_id", "name", "type", "description", "start_time", "end_time").
Limit(int(limit)).
Offset(int(offset)).
Scan(&results).Error
if err != nil {
return nil, err
}
return &results, nil
}