mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Add CollectionResource model and update collection handling methods
This commit is contained in:
@@ -13,7 +13,7 @@ func CreateCollection(uid uint, title string, article string, images []uint, pub
|
|||||||
UserID: uid,
|
UserID: uid,
|
||||||
Title: title,
|
Title: title,
|
||||||
Article: article,
|
Article: article,
|
||||||
Public: public, // 新增
|
Public: public,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Create(&collection).Error; err != nil {
|
if err := tx.Create(&collection).Error; err != nil {
|
||||||
@@ -73,7 +73,7 @@ func DeleteCollection(id uint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Model(collection).Association("Resources").Clear(); err != nil {
|
if err := tx.Model(&model.CollectionResource{}).Where("collection_id = ?", id).Delete(&model.CollectionResource{}).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,11 +97,12 @@ func AddResourceToCollection(collectionID uint, resourceID uint) error {
|
|||||||
return model.NewRequestError("Invalid resource ID")
|
return model.NewRequestError("Invalid resource ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Model(collection).Association("Resources").Append(&model.Resource{
|
collectionResource := &model.CollectionResource{
|
||||||
Model: gorm.Model{
|
CollectionID: collectionID,
|
||||||
ID: resourceID,
|
ResourceID: resourceID,
|
||||||
},
|
}
|
||||||
}); err != nil {
|
|
||||||
|
if err := tx.Save(collectionResource).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,11 +126,7 @@ func RemoveResourceFromCollection(collectionID uint, resourceID uint) error {
|
|||||||
return model.NewRequestError("Invalid resource ID")
|
return model.NewRequestError("Invalid resource ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Model(collection).Association("Resources").Delete(&model.Resource{
|
if err := tx.Where("collection_id = ? AND resource_id = ?", collectionID, resourceID).Delete(&model.CollectionResource{}).Error; err != nil {
|
||||||
Model: gorm.Model{
|
|
||||||
ID: resourceID,
|
|
||||||
},
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +140,7 @@ func RemoveResourceFromCollection(collectionID uint, resourceID uint) error {
|
|||||||
|
|
||||||
func GetCollectionByID(id uint) (*model.Collection, error) {
|
func GetCollectionByID(id uint) (*model.Collection, error) {
|
||||||
collection := &model.Collection{}
|
collection := &model.Collection{}
|
||||||
if err := db.Preload("Images").Preload("Resources").Preload("User").Where("id = ?", id).First(collection).Error; err != nil {
|
if err := db.Preload("Images").Preload("User").Where("id = ?", id).First(collection).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return collection, nil
|
return collection, nil
|
||||||
@@ -164,7 +161,6 @@ func ListUserCollections(uid uint, page int, pageSize int, showPrivate bool) ([]
|
|||||||
|
|
||||||
if err := query.
|
if err := query.
|
||||||
Preload("Images").
|
Preload("Images").
|
||||||
Preload("Resources").
|
|
||||||
Offset((page - 1) * pageSize).
|
Offset((page - 1) * pageSize).
|
||||||
Limit(pageSize).
|
Limit(pageSize).
|
||||||
Find(&collections).Error; err != nil {
|
Find(&collections).Error; err != nil {
|
||||||
@@ -180,9 +176,11 @@ func ListCollectionResources(collectionID uint, page int, pageSize int) ([]*mode
|
|||||||
var resources []*model.Resource
|
var resources []*model.Resource
|
||||||
var total int64
|
var total int64
|
||||||
|
|
||||||
if err := db.Raw(`
|
if err := db.
|
||||||
select count(*) from collection_resources
|
Model(&model.Resource{}).
|
||||||
where collection_id = ?`, collectionID).Scan(&total).Error; err != nil {
|
Joins("JOIN collection_resources ON collection_resources.resource_id = resources.id").
|
||||||
|
Where("collection_resources.collection_id = ?", collectionID).
|
||||||
|
Count(&total).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,6 +191,7 @@ func ListCollectionResources(collectionID uint, page int, pageSize int) ([]*mode
|
|||||||
Preload("Tags").
|
Preload("Tags").
|
||||||
Joins("JOIN collection_resources ON collection_resources.resource_id = resources.id").
|
Joins("JOIN collection_resources ON collection_resources.resource_id = resources.id").
|
||||||
Where("collection_resources.collection_id = ?", collectionID).
|
Where("collection_resources.collection_id = ?", collectionID).
|
||||||
|
Order("collection_resources.created_at DESC").
|
||||||
Offset((page - 1) * pageSize).
|
Offset((page - 1) * pageSize).
|
||||||
Limit(pageSize).
|
Limit(pageSize).
|
||||||
Find(&resources).Error; err != nil {
|
Find(&resources).Error; err != nil {
|
||||||
@@ -229,7 +228,6 @@ func SearchUserCollections(uid uint, keyword string, excludedRID uint, showPriva
|
|||||||
|
|
||||||
if err := query.
|
if err := query.
|
||||||
Preload("Images").
|
Preload("Images").
|
||||||
Preload("Resources").
|
|
||||||
Limit(10).
|
Limit(10).
|
||||||
Find(&collections).Error; err != nil {
|
Find(&collections).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -47,6 +47,7 @@ func init() {
|
|||||||
&model.Comment{},
|
&model.Comment{},
|
||||||
&model.Activity{},
|
&model.Activity{},
|
||||||
&model.Collection{},
|
&model.Collection{},
|
||||||
|
&model.CollectionResource{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,14 +4,13 @@ import "gorm.io/gorm"
|
|||||||
|
|
||||||
type Collection struct {
|
type Collection struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Title string `gorm:"not null"`
|
Title string `gorm:"not null"`
|
||||||
Article string `gorm:"not null"`
|
Article string `gorm:"not null"`
|
||||||
UserID uint `gorm:"not null"`
|
UserID uint `gorm:"not null"`
|
||||||
User User `gorm:"foreignKey:UserID;references:ID"`
|
User User `gorm:"foreignKey:UserID;references:ID"`
|
||||||
ResourcesCount int `gorm:"default:0"`
|
ResourcesCount int `gorm:"default:0"`
|
||||||
Images []Image `gorm:"many2many:collection_images;"`
|
Images []Image `gorm:"many2many:collection_images;"`
|
||||||
Resources []Resource `gorm:"many2many:collection_resources;"`
|
Public bool `gorm:"default:false"` // 新增公开/私有字段
|
||||||
Public bool `gorm:"default:false"` // 新增公开/私有字段
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollectionView struct {
|
type CollectionView struct {
|
||||||
|
9
server/model/collection_resource.go
Normal file
9
server/model/collection_resource.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type CollectionResource struct {
|
||||||
|
CollectionID uint `gorm:"primaryKey"`
|
||||||
|
ResourceID uint `gorm:"primaryKey"`
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
Reference in New Issue
Block a user