Documentation
¶
Index ¶
- Variables
- func DB() *gorm.DB
- func GetLastFileID() (uint, error)
- func GetUserIDByEmail(email string) (uint, error)
- type BasicData
- func (d *BasicData) Create() error
- func (BasicData) Delete(regionID, categoryID, year string) error
- func (BasicData) GetAvailableYearsByLeafCategory(categoryID string) ([]string, error)
- func (BasicData) GetBasicDataByCategoryAndYears(categoryID string, years []string) ([]BasicData, error)
- func (d *BasicData) Update(regionID, categoryID, year string) error
- type Category
- type DBInstance
- type File
- func (f *File) DeleteFileByIDAndUserID(fileID, userID uint) error
- func (f *File) GetFileByIDAndUserID(fileID, userID uint) (*File, error)
- func (f *File) GetFileListByUserId(userID uint) ([]File, error)
- func (f *File) PostFileInfo(userID uint, filename string) error
- func (f *File) PutFileInfo(fileID, userID uint, filename string) error
- func (File) TableName() string
- type Province
- type Region
- type User
- func (u *User) Create() error
- func (u *User) DeleteUserByID(id string) error
- func (u *User) GetAllUsers() ([]User, error)
- func (u *User) GetFirstByEmail(email string) error
- func (u *User) GetFirstByID(id string) error
- func (u *User) Login(password string) error
- func (u *User) Signup() error
- func (User) TableName() string
- func (u *User) UpdateUser() error
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidArgs = errors.New("Invalid Args") ErrKeyConflict = errors.New("Key Conflict") ErrDataNotFound = errors.New("Record Not Found") ErrUserExists = errors.New("User already exists") ErrUnknown = errors.New("Unknown Error") ErrFailed = errors.New("Failed") )
Model errors
var ErrLeafCategory = errors.New("no subcategories available; this is a leaf category")
查询接口3:根据类别 ID 获取其下的子类别,若无子类别则返回叶子类别错误
Functions ¶
func GetLastFileID ¶
GetLastFileID 获取最新的 fileID,返回最后插入的文件的 fileID
func GetUserIDByEmail ¶
Types ¶
type BasicData ¶
type BasicData struct { CategoryID string `gorm:"type:char(10);not null" json:"category_id"` // 分类ID,外键关联到类别表 DataName string `gorm:"type:char(20);not null" json:"data_name"` // 数据名称,非空 Data int `gorm:"type:int;default:0;check:data >= 0" json:"data"` // 数据,默认为0且不允许负数 Year string `gorm:"type:char(4);not null" json:"year"` // 年份,非空,且应是4位字符 Category Category `gorm:"foreignKey:CategoryID;constraint:OnDelete:CASCADE" json:"-"` // 外键设置级联删除 }
BasicData 表结构
func (BasicData) GetAvailableYearsByLeafCategory ¶
type Category ¶
type Category struct { CategoryID string `gorm:"type:char(10);primaryKey" json:"category_id"` // 类别ID ParentID *string `gorm:"type:char(10)" json:"parent_id"` // 父类别ID,允许为空 CategoryName string `gorm:"type:char(20);not null" json:"category_name"` // 类别名称,非空 Level int `gorm:"type:int" json:"level"` // 类别层级 RegionID string `gorm:"type:char(10);not null" json:"region_id"` // 地区ID,外键关联到地区表 Region Region `gorm:"foreignKey:RegionID;constraint:OnDelete:CASCADE" json:"-"` // 外键设置级联删除 Parent *Category `gorm:"foreignKey:ParentID;constraint:OnDelete:SET NULL" json:"-"` // 自关联,设置父类,删除置空 }
Category 表结构
func (Category) GetSubCategories ¶
func (Category) GetTopLevelCategoriesByRegion ¶
查询接口2:根据地区 ID 获取顶级类别(parent_id 为空的类别)
type DBInstance ¶
type DBInstance struct {
// contains filtered or unexported fields
}
定义了数据库实例的结构,而且被指定了单例,只会生成一次
func (*DBInstance) Instance ¶
func (i *DBInstance) Instance() any
Instance gets the singleton instance
type File ¶
type File struct { FileID uint `gorm:"primaryKey;autoIncrement;column:file_id" json:"fileId"` // 文件 ID,主键且自动递增 UserID uint `gorm:"not null;column:user_id" json:"userId"` // 用户 ID,外键,关联 users 表 Filename string `gorm:"not null;column:filename" json:"filename"` // 文件名,最大长度 255 UploadTime time.Time `gorm:"default:current_timestamp;column:upload_time" json:"uploadTime"` // 上传时间,默认为当前时间戳 // Relationships User User `gorm:"foreignKey:UserID;references:ID" json:"user"` // 定义外键关联,指向 User 表的 ID 字段 }
File represents the file model
func (*File) DeleteFileByIDAndUserID ¶
DeleteFileByIDAndUserID 根据文件 ID 和用户 ID 删除文件
func (*File) GetFileByIDAndUserID ¶
GetFileByIDAndUserID 根据文件 ID 和用户 ID 获取文件
func (*File) GetFileListByUserId ¶
GetFileListByUserId 获取指定用户 ID 的所有文件
func (*File) PostFileInfo ¶
PostFileInfo 创建新的文件记录
func (*File) PutFileInfo ¶
PutFileInfo 根据文件 ID 和用户 ID 更新文件信息
type Province ¶
type Province struct { ProvinceID string `gorm:"type:char(10);primaryKey" json:"province_id"` // 省份ID ProvinceName string `gorm:"type:char(20);not null;unique" json:"province_name"` // 省份名称,非空且唯一 Regions []Region `gorm:"foreignKey:ProvinceID" json:"-"` // 与地区的关系,一对多 }
Province 表结构
func (Province) GetAllProvinces ¶
GetAllProvinces 获取所有省份信息
type Region ¶
type Region struct { RegionID string `gorm:"type:char(10);primaryKey" json:"region_id"` // 地区ID RegionName string `gorm:"type:char(20);not null" json:"region_name"` // 地区名称,非空 ProvinceID string `gorm:"type:char(10);not null" json:"province_id"` // 省份ID,外键关联到省份表 Province Province `gorm:"foreignKey:ProvinceID;constraint:OnDelete:CASCADE" json:"-"` // 外键设置级联删除 Categories []Category `gorm:"foreignKey:RegionID" json:"-"` // 与类别的关系,一对多 }
Region 表结构
func (Region) GetRegionsByProvince ¶
查询接口1:根据省份 ID 获取其下的地区
type User ¶
type User struct { ID uint `gorm:"primary_key" json:"id"` Name string `json:"name"` Email string `json:"email"` Password string `json:"-"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` }
User the user model
func LoginByEmailAndPassword ¶
LoginByEmailAndPassword login a user by his email and password
func (*User) DeleteUserByID ¶
DeleteUserByID deletes a user by their ID
func (*User) GetFirstByEmail ¶
GetFirstByEmail gets the user by his email
func (*User) GetFirstByID ¶
GetFirstByID gets the user by his ID