models

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

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddArticle

func AddArticle(data map[string]interface{}) error

添加文章

func AddTag

func AddTag(name, createdBy string, state int) error

添加标签

func CheckAuth

func CheckAuth(username, password string) bool

func CleanAllArticles

func CleanAllArticles() error

硬删除文章,将所有已经软删除的文章硬删除,GORM约定硬删除用Unscoped

func CleanAllTags

func CleanAllTags() error

硬删除tag,GORM约定硬删除用Unscoped

func CloseDB

func CloseDB()

func DeleteArticle

func DeleteArticle(id int) error

删除文章

func DeleteTag

func DeleteTag(id int) error

删除标签

func EditArticle

func EditArticle(id int, data interface{}) error

编辑文章

func EditTag

func EditTag(id int, data map[string]interface{}) error

编辑标签

func ExistTagByID

func ExistTagByID(id int) (bool, error)

根据id判断标签是否存在

func ExistTagByName

func ExistTagByName(name string) (bool, error)

根据名字判断标签是否存在

func ExistedArticleByID

func ExistedArticleByID(id int) (bool, error)

根据文章id判断是否存在

func GetArticleTotal

func GetArticleTotal(maps interface{}) (int, error)

获取文章数量

func GetTagTotal

func GetTagTotal(maps interface{}) (int, error)

获取标签数量

func Setup

func Setup()

初始化数据库连接,以及设置gorm回调函数

Types

type Article

type Article struct {
	Model

	TagID int `json:"tag_id" gorm:"index"` // gorm:"index" 用于声明这个字段为索引,如果使用了自动迁移功能则会有所影响
	Tag   Tag `json:"tag"`

	Title      string `json:"title"`
	Desc       string `json:"desc"`
	Content    string `json:"content"`
	CreatedBy  string `json:"created_by"`
	ModifiedBy string `json:"modified_by"`
	State      int    `json:"state"`

	CoverImageURL string `json:"cover_image_url"`
}

func GetArticle

func GetArticle(id int) (*Article, error)

获取文章

func GetArticles

func GetArticles(pageNum, pageSize int, maps interface{}) ([]Article, error)

获取文章列表

func (*Article) AfterCreate

func (article *Article) AfterCreate(scope *gorm.Scope) error

func (*Article) BeforeCreate

func (article *Article) BeforeCreate(scope *gorm.Scope) error

属于gorm的钩子机制 可以将回调方法定义为模型结构的指针,在创建、更新、查询、删除时将被调用 如果任何回调返回错误,gorm将停止未来操作并回滚所有更改

type Auth

type Auth struct {
	ID       int    `gorm:"primary_key" json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
}

type Model

type Model struct {
	ID         int `gorm:"primary_key" json:"id"`
	CreatedOn  int `json:"created_on"`
	ModifiedOn int `json:"modified_on"`
	DeletedOn  int `json:"deleted_on"`
}

type Tag

type Tag struct {
	Model

	Name       string `json:"name"`
	CreatedBy  string `json:"created_by"`
	ModifiedBy string `json:"modified_by"`
	State      int    `json:"state"`
}

用于GORM使用

func GetTags

func GetTags(pageNum int, pageSize int, maps interface{}) ([]Tag, error)

分页获取标签

func (*Tag) BeforeCreate

func (tag *Tag) BeforeCreate(scope *gorm.Scope) error

属于gorm的钩子机制 可以将回调方法定义为模型结构的指针,在创建、更新、查询、删除时将被调用 如果任何回调返回错误,gorm将停止未来操作并回滚所有更改

func (*Tag) BeforeUpdate

func (tag *Tag) BeforeUpdate(scope *gorm.Scope) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL