Mod event and user table, add event CURD

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-25 21:55:08 +08:00
parent 8e43d6699c
commit 3dbcc00a2d
5 changed files with 86 additions and 74 deletions

View File

@@ -16,17 +16,16 @@ import (
// Super User: 30
type User struct {
Id uint `json:"id" gorm:"primarykey;autoincrement"`
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
UserId uuid.UUID `json:"user_id" gorm:"type:uuid;uniqueindex;not null"`
Email string `json:"email" gorm:"type:varchar(255);uniqueindex;not null"`
Type string `json:"type" gorm:"type:varchar(32);index;not null"`
Nickname string `json:"nickname"`
Subtitle string `json:"subtitle"`
Avatar string `json:"avatar"`
Checkin datatypes.JSONMap `json:"checkin"`
JoinedEvent datatypes.JSONSlice[uuid.UUID] `json:"joined_event"`
PermissionLevel uint `json:"permission_level" gorm:"default:10;not null"`
Id uint `json:"id" gorm:"primarykey;autoincrement"`
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
UserId uuid.UUID `json:"user_id" gorm:"type:uuid;uniqueindex;not null"`
Email string `json:"email" gorm:"type:varchar(255);uniqueindex;not null"`
Type string `json:"type" gorm:"type:varchar(32);index;not null"`
Nickname string `json:"nickname"`
Subtitle string `json:"subtitle"`
Avatar string `json:"avatar"`
Checkin datatypes.JSONMap `json:"checkin"`
PermissionLevel uint `json:"permission_level" gorm:"default:10;not null"`
}
func (self *User) GetByEmail(email string) error {
@@ -43,7 +42,7 @@ func (self *User) GetByUserId(userId uuid.UUID) error {
return nil
}
func (self *User) UpdateCheckin(userId uuid.UUID, eventId uuid.UUID, time time.Time) error {
func (self *User) UpdateCheckin(userId, eventId uuid.UUID, time time.Time) error {
return Database.Transaction(func(tx *gorm.DB) error {
if err := tx.
Clauses(clause.Locking{Strength: "UPDATE"}).
@@ -79,44 +78,11 @@ func (self *User) Create() error {
})
}
type UserUpdateInput struct {
Email *string `json:"email"`
Nickname *string `json:"nickname"`
Subtitle *string `json:"subtitle"`
Avatar *string `json:"avatar"`
Type *string `json:"type"`
}
func (self *User) UpdateByUserID(userID uuid.UUID, in *UserUpdateInput) error {
func (self *User) UpdateByUserID(userId uuid.UUID) error {
return Database.Transaction(func(tx *gorm.DB) error {
if err := tx.
Where("user_id = ?", userID).
First(self).Error; err != nil {
if err := tx.Model(&User{}).Where("user_id = ?", userId).Updates(&self).Error; err != nil {
return err
}
updates := map[string]any{}
if in.Email != nil {
updates["email"] = *in.Email
}
if in.Nickname != nil {
updates["nickname"] = *in.Nickname
}
if in.Subtitle != nil {
updates["subtitle"] = *in.Subtitle
}
if in.Avatar != nil {
updates["avatar"] = *in.Avatar
}
if in.Type != nil {
updates["type"] = *in.Type
}
if len(updates) == 0 {
return nil
}
return tx.Model(self).Updates(updates).Error
return nil
})
}