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
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/datatypes"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Id uint `json:"id" gorm:"primarykey;autoincrement"`
|
Id uint `json:"id" gorm:"primarykey;autoincrement"`
|
||||||
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
|
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueIndex;not null"`
|
||||||
EventId uuid.UUID `json:"event_id" 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"`
|
Name string `json:"name" gorm:"type:varchar(255);index;not null"`
|
||||||
StartTime time.Time `json:"start_time" gorm:"index"`
|
StartTime time.Time `json:"start_time" gorm:"index"`
|
||||||
EndTime time.Time `json:"end_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 {
|
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 {
|
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 {
|
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
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
62
data/user.go
62
data/user.go
@@ -16,17 +16,16 @@ import (
|
|||||||
// Super User: 30
|
// Super User: 30
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id uint `json:"id" gorm:"primarykey;autoincrement"`
|
Id uint `json:"id" gorm:"primarykey;autoincrement"`
|
||||||
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
|
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
|
||||||
UserId uuid.UUID `json:"user_id" 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"`
|
Email string `json:"email" gorm:"type:varchar(255);uniqueindex;not null"`
|
||||||
Type string `json:"type" gorm:"type:varchar(32);index;not null"`
|
Type string `json:"type" gorm:"type:varchar(32);index;not null"`
|
||||||
Nickname string `json:"nickname"`
|
Nickname string `json:"nickname"`
|
||||||
Subtitle string `json:"subtitle"`
|
Subtitle string `json:"subtitle"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
Checkin datatypes.JSONMap `json:"checkin"`
|
Checkin datatypes.JSONMap `json:"checkin"`
|
||||||
JoinedEvent datatypes.JSONSlice[uuid.UUID] `json:"joined_event"`
|
PermissionLevel uint `json:"permission_level" gorm:"default:10;not null"`
|
||||||
PermissionLevel uint `json:"permission_level" gorm:"default:10;not null"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *User) GetByEmail(email string) error {
|
func (self *User) GetByEmail(email string) error {
|
||||||
@@ -43,7 +42,7 @@ func (self *User) GetByUserId(userId uuid.UUID) error {
|
|||||||
return nil
|
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 {
|
return Database.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := tx.
|
if err := tx.
|
||||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||||
@@ -79,44 +78,11 @@ func (self *User) Create() error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserUpdateInput struct {
|
func (self *User) UpdateByUserID(userId uuid.UUID) error {
|
||||||
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 {
|
|
||||||
return Database.Transaction(func(tx *gorm.DB) error {
|
return Database.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := tx.
|
if err := tx.Model(&User{}).Where("user_id = ?", userId).Updates(&self).Error; err != nil {
|
||||||
Where("user_id = ?", userID).
|
|
||||||
First(self).Error; err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
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
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,18 +82,8 @@ func VerifyMagicLink(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
// Create user
|
// Create user
|
||||||
newUUID, err := uuid.NewUUID()
|
user.UUID = uuid.New()
|
||||||
if err != nil {
|
user.UserId = uuid.New()
|
||||||
c.JSON(500, gin.H{"status": "internal server error"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
newUserId, err := uuid.NewUUID()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(500, gin.H{"status": "internal server error"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
user.UUID = newUUID
|
|
||||||
user.UserId = newUserId
|
|
||||||
user.Email = email
|
user.Email = email
|
||||||
user.Type = "Normal"
|
user.Type = "Normal"
|
||||||
user.PermissionLevel = 10
|
user.PermissionLevel = 10
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ func Info(c *gin.Context) {
|
|||||||
"subtitle": data.Subtitle,
|
"subtitle": data.Subtitle,
|
||||||
"avatar": data.Avatar,
|
"avatar": data.Avatar,
|
||||||
"checkin": data.Checkin,
|
"checkin": data.Checkin,
|
||||||
"joined_event": data.JoinedEvent,
|
|
||||||
"permission_level": data.PermissionLevel,
|
"permission_level": data.PermissionLevel,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Update(c *gin.Context) {
|
func Update(c *gin.Context) {
|
||||||
var ReqInfo data.UserUpdateInput
|
var ReqInfo data.User
|
||||||
c.BindJSON(&ReqInfo)
|
c.BindJSON(&ReqInfo)
|
||||||
|
|
||||||
// New user model
|
// New user model
|
||||||
@@ -32,13 +32,17 @@ func Update(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user.Avatar = ReqInfo.Avatar
|
||||||
|
user.Email = ReqInfo.Email
|
||||||
|
user.Nickname = ReqInfo.Nickname
|
||||||
|
user.Subtitle = ReqInfo.Subtitle
|
||||||
// Cant change user type under permission 2
|
// Cant change user type under permission 2
|
||||||
if user.PermissionLevel < 2 {
|
if user.PermissionLevel >= 2 {
|
||||||
ReqInfo.Type = nil
|
user.Type = ReqInfo.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update user info
|
// Update user info
|
||||||
user.UpdateByUserID(userId.(uuid.UUID), &ReqInfo)
|
user.UpdateByUserID(userId.(uuid.UUID))
|
||||||
|
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
|
|||||||
Reference in New Issue
Block a user