forked from nixcn/nixcn-cms
@@ -32,7 +32,7 @@ func Init() {
|
||||
}
|
||||
|
||||
// Auto migrate
|
||||
err = db.DB.AutoMigrate(&User{})
|
||||
err = db.DB.AutoMigrate(&User{}, &Event{})
|
||||
if err != nil {
|
||||
log.Error("[Database] Error migrating database: ", err)
|
||||
}
|
||||
|
||||
@@ -14,3 +14,15 @@ type Event struct {
|
||||
StartTime time.Time `json:"start_time" gorm:"index"`
|
||||
EndTime time.Time `json:"end_time" gorm:"index"`
|
||||
}
|
||||
|
||||
func (self *Event) GetEventById(eventId uuid.UUID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Event) UpdateEventById(eventId uuid.UUID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Event) CreateEvent() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
26
data/user.go
26
data/user.go
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -15,16 +16,17 @@ 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 time.Time `json:"checkin" gorm:"index"`
|
||||
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"`
|
||||
JoinedEvent datatypes.JSONSlice[uuid.UUID] `json:"joined_event"`
|
||||
PermissionLevel uint `json:"permission_level" gorm:"default:10;not null"`
|
||||
}
|
||||
|
||||
func (self *User) GetByEmail(email string) error {
|
||||
@@ -41,7 +43,7 @@ func (self *User) GetByUserId(userId uuid.UUID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *User) UpdateCheckin(userId uuid.UUID, time time.Time) error {
|
||||
func (self *User) UpdateCheckin(userId uuid.UUID, eventId uuid.UUID, time time.Time) error {
|
||||
return Database.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
@@ -50,7 +52,7 @@ func (self *User) UpdateCheckin(userId uuid.UUID, time time.Time) error {
|
||||
return err // if error then rollback
|
||||
}
|
||||
|
||||
self.Checkin = time
|
||||
self.Checkin = datatypes.JSONMap{eventId.String(): time}
|
||||
|
||||
if err := tx.Save(self).Error; err != nil {
|
||||
return err // rollback
|
||||
|
||||
Reference in New Issue
Block a user