wood chen aeb05f790a 重构数据库层并迁移到 GORM
- 将数据库操作从原生 SQL 迁移到 GORM ORM
- 更新模型定义,添加 GORM 标签和关系
- 移除手动创建表的 SQL 方法,改用 AutoMigrate
- 更新所有数据库相关处理逻辑以适配 GORM
- 升级 Go 版本和依赖库
- 移除数据库和路由中间件,简化项目结构
2025-03-06 23:32:18 +08:00

38 lines
1.1 KiB
Go

package models
import (
"time"
"gorm.io/gorm"
)
type User struct {
ID uint `json:"id" gorm:"primaryKey"`
Username string `json:"username" gorm:"not null;unique"`
Email string `json:"email" gorm:"not null"`
Role string `json:"role" gorm:"not null;default:user"` // admin or user
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
type Session struct {
ID string `json:"id" gorm:"primaryKey"`
UserID uint `json:"user_id" gorm:"not null"`
User User `json:"user" gorm:"foreignKey:UserID"`
ExpiresAt time.Time `json:"expires_at" gorm:"not null"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
// TableName 指定User表名
func (User) TableName() string {
return "user"
}
// TableName 指定Session表名
func (Session) TableName() string {
return "session"
}