forked from nixcn/nixcn-cms
Mod event and user table, add event CURD
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -1,28 +1,81 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
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"`
|
||||
StartTime time.Time `json:"start_time" gorm:"index"`
|
||||
EndTime time.Time `json:"end_time" gorm:"index"`
|
||||
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"`
|
||||
StartTime time.Time `json:"start_time" gorm:"index"`
|
||||
EndTime time.Time `json:"end_time" gorm:"index"`
|
||||
JoinedUsers datatypes.JSONSlice[uuid.UUID] `json:"joined_users"`
|
||||
}
|
||||
|
||||
func (self *Event) GetEventById(eventId uuid.UUID) error {
|
||||
return nil
|
||||
return Database.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("event_id = ?", eventId).First(&self).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Event) UpdateEventById(eventId uuid.UUID) error {
|
||||
return nil
|
||||
return Database.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&Event{}).Where("event_id = ?", eventId).Updates(&self).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Event) CreateEvent() error {
|
||||
return nil
|
||||
if self.UUID == uuid.Nil {
|
||||
self.UUID = uuid.New()
|
||||
}
|
||||
if self.EventId == uuid.Nil {
|
||||
self.EventId = uuid.New()
|
||||
}
|
||||
|
||||
return Database.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(&self).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Event) UserJoinEvent(userId, eventId uuid.UUID) error {
|
||||
return Database.Transaction(func(tx *gorm.DB) error {
|
||||
var event Event
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("event_id = ?", eventId).
|
||||
First(&event).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if user already joined
|
||||
if slices.Contains(event.JoinedUsers, userId) {
|
||||
return errors.New("user already joined")
|
||||
}
|
||||
|
||||
// Add user to list
|
||||
event.JoinedUsers = append(event.JoinedUsers, userId)
|
||||
if err := tx.Model(&Event{}).Where("event_id = ?", eventId).Update("joined_users", event.JoinedUsers).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*self = event
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user