46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type Note struct {
|
|
Model
|
|
Key string `gorm:"unique_index; not null"` // 文章唯一标识
|
|
UserId int
|
|
User User
|
|
Title string
|
|
Summary string `gorm:"type:text"`
|
|
Content string `gorm:"type:text"`
|
|
Visit int `gorm:"default:0"`
|
|
Praise int `gorm:"default:0"`
|
|
}
|
|
|
|
func QueryNoteByKeyAndUserId(key string, userId int) (note Note, err error) {
|
|
err = db.Model(&Note{}).First(¬e, "`key` = ? AND `user_id` = ?", key, userId).Error
|
|
return note, err
|
|
}
|
|
|
|
func QueryNoteByPage(title string, page int, limit int) (notes []*Note, err error) {
|
|
err = db.Where("title like ?", fmt.Sprintf("%%%s%%", title)).Offset((page - 1) * limit).Limit(limit).Find(¬es).Error
|
|
return notes, err
|
|
}
|
|
|
|
func QueryNoteCount(title string) (count int, err error) {
|
|
err = db.Model(&Note{}).Where("title like ?", fmt.Sprintf("%%%s%%", title)).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func QueryNoteByKey(key string) (note Note, err error) {
|
|
err = db.Model(&Note{}).First(¬e, "`key` = ?", key).Error
|
|
return note, err
|
|
}
|
|
|
|
func SaveNote(n *Note) error {
|
|
return db.Save(n).Error
|
|
}
|
|
|
|
func DeleteNoteByUserIdAndKey(key string, userId int) (err error) {
|
|
err = db.Delete(&Note{}, "`key` = ? AND `user_id` = ?", key, userId).Error
|
|
return err
|
|
} |