model

package
v0.0.0-...-4aeb0dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

View Source
var ErrLeafCategory = errors.New("no subcategories available; this is a leaf category")

查询接口3:根据类别 ID 获取其下的子类别,若无子类别则返回叶子类别错误

Functions

func DB

func DB() *gorm.DB

DB returns the database instance

func GetLastFileID

func GetLastFileID() (uint, error)

GetLastFileID 获取最新的 fileID,返回最后插入的文件的 fileID

func GetUserIDByEmail

func GetUserIDByEmail(email string) (uint, error)

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) Create

func (d *BasicData) Create() error

Create 新增数据

func (BasicData) Delete

func (BasicData) Delete(regionID, categoryID, year string) error

Delete 删除数据

func (BasicData) GetAvailableYearsByLeafCategory

func (BasicData) GetAvailableYearsByLeafCategory(categoryID string) ([]string, error)

func (BasicData) GetBasicDataByCategoryAndYears

func (BasicData) GetBasicDataByCategoryAndYears(categoryID string, years []string) ([]BasicData, error)

查询接口4:根据类别 ID 和年份范围查询 BasicData 中的相关数据

func (*BasicData) Update

func (d *BasicData) Update(regionID, categoryID, year string) error

Update 修改数据

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) Create

func (c *Category) Create() error

Create 新增分类

func (Category) Delete

func (Category) Delete(categoryID string) error

Delete 删除分类

func (Category) GetSubCategories

func (Category) GetSubCategories(categoryID string) ([]Category, error)

func (Category) GetTopLevelCategoriesByRegion

func (Category) GetTopLevelCategoriesByRegion(regionID string) ([]Category, error)

查询接口2:根据地区 ID 获取顶级类别(parent_id 为空的类别)

func (*Category) Update

func (c *Category) Update(categoryID string) error

Update 修改分类

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

func (f *File) DeleteFileByIDAndUserID(fileID, userID uint) error

DeleteFileByIDAndUserID 根据文件 ID 和用户 ID 删除文件

func (*File) GetFileByIDAndUserID

func (f *File) GetFileByIDAndUserID(fileID, userID uint) (*File, error)

GetFileByIDAndUserID 根据文件 ID 和用户 ID 获取文件

func (*File) GetFileListByUserId

func (f *File) GetFileListByUserId(userID uint) ([]File, error)

GetFileListByUserId 获取指定用户 ID 的所有文件

func (*File) PostFileInfo

func (f *File) PostFileInfo(userID uint, filename string) error

PostFileInfo 创建新的文件记录

func (*File) PutFileInfo

func (f *File) PutFileInfo(fileID, userID uint, filename string) error

PutFileInfo 根据文件 ID 和用户 ID 更新文件信息

func (File) TableName

func (File) TableName() string

虽然gorm显式映射到小写复数,但是这里显式声明一下

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) Create

func (p *Province) Create() error

Create 新增省份

func (Province) Delete

func (Province) Delete(provinceID string) error

Delete 删除省份

func (Province) GetAllProvinces

func (Province) GetAllProvinces() ([]Province, error)

GetAllProvinces 获取所有省份信息

func (*Province) Update

func (p *Province) Update(provinceID string) error

Update 修改省份

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) Create

func (r *Region) Create() error

Create 新增区域

func (Region) Delete

func (Region) Delete(regionID string) error

Delete 删除区域

func (Region) GetRegionsByProvince

func (Region) GetRegionsByProvince(provinceID string) ([]Region, error)

查询接口1:根据省份 ID 获取其下的地区

func (*Region) Update

func (r *Region) Update(regionID string) error

Update 修改区域

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

func LoginByEmailAndPassword(email, password string) (*User, error)

LoginByEmailAndPassword login a user by his email and password

func (*User) Create

func (u *User) Create() error

Create a new user

func (*User) DeleteUserByID

func (u *User) DeleteUserByID(id string) error

DeleteUserByID deletes a user by their ID

func (*User) GetAllUsers

func (u *User) GetAllUsers() ([]User, error)

GetAllUsers gets all users

func (*User) GetFirstByEmail

func (u *User) GetFirstByEmail(email string) error

GetFirstByEmail gets the user by his email

func (*User) GetFirstByID

func (u *User) GetFirstByID(id string) error

GetFirstByID gets the user by his ID

func (*User) Login

func (u *User) Login(password string) error

Login a user

func (*User) Signup

func (u *User) Signup() error

Signup a new user

func (User) TableName

func (User) TableName() string

TableName for gorm

func (*User) UpdateUser

func (u *User) UpdateUser() error

UpdateUser updates the user information

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳