You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/model/post_star.go

99 lines
2.2 KiB

3 years ago
package model
import (
"time"
"gorm.io/gorm"
)
type PostStar struct {
*Model
Post *Post `json:"-"`
3 years ago
PostID int64 `json:"post_id"`
UserID int64 `json:"user_id"`
}
func (p *PostStar) Get(db *gorm.DB) (*PostStar, error) {
var star PostStar
tn := db.NamingStrategy.TableName("PostStar") + "."
3 years ago
if p.Model != nil && p.ID > 0 {
db = db.Where(tn+"id = ? AND "+tn+"is_del = ?", p.ID, 0)
3 years ago
}
if p.PostID > 0 {
db = db.Where(tn+"post_id = ?", p.PostID)
3 years ago
}
if p.UserID > 0 {
db = db.Where(tn+"user_id = ?", p.UserID)
3 years ago
}
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC")
if err := db.First(&star).Error; err != nil {
return nil, err
3 years ago
}
return &star, nil
}
func (p *PostStar) Create(db *gorm.DB) (*PostStar, error) {
err := db.Omit("Post").Create(&p).Error
3 years ago
return p, err
}
func (p *PostStar) Delete(db *gorm.DB) error {
return db.Model(&PostStar{}).Omit("Post").Where("id = ? AND is_del = ?", p.Model.ID, 0).Updates(map[string]interface{}{
3 years ago
"deleted_on": time.Now().Unix(),
"is_del": 1,
}).Error
}
func (p *PostStar) List(db *gorm.DB, conditions *ConditionsT, offset, limit int) ([]*PostStar, error) {
var stars []*PostStar
var err error
tn := db.NamingStrategy.TableName("PostStar") + "."
3 years ago
if offset >= 0 && limit > 0 {
db = db.Offset(offset).Limit(limit)
}
if p.UserID > 0 {
db = db.Where(tn+"user_id = ?", p.UserID)
3 years ago
}
for k, v := range *conditions {
if k == "ORDER" {
db = db.Order(v)
} else {
db = db.Where(tn+k, v)
3 years ago
}
}
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate).Order("Post.id DESC")
if err = db.Find(&stars).Error; err != nil {
3 years ago
return nil, err
}
return stars, nil
}
func (p *PostStar) Count(db *gorm.DB, conditions *ConditionsT) (int64, error) {
var count int64
tn := db.NamingStrategy.TableName("PostStar") + "."
3 years ago
if p.PostID > 0 {
db = db.Where(tn+"post_id = ?", p.PostID)
3 years ago
}
if p.UserID > 0 {
db = db.Where(tn+"user_id = ?", p.UserID)
3 years ago
}
for k, v := range *conditions {
if k != "ORDER" {
db = db.Where(tn+k, v)
3 years ago
}
}
db = db.Joins("Post").Where("Post.visibility <> ?", PostVisitPrivate)
3 years ago
if err := db.Model(p).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}