mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Add resources_count to collection.
This commit is contained in:
@@ -197,5 +197,6 @@ export interface Collection {
|
|||||||
title: string;
|
title: string;
|
||||||
article: string;
|
article: string;
|
||||||
user: User;
|
user: User;
|
||||||
|
resources_count: number;
|
||||||
images: Image[];
|
images: Image[];
|
||||||
}
|
}
|
||||||
|
@@ -80,7 +80,11 @@ func AddResourceToCollection(collectionID uint, resourceID uint) error {
|
|||||||
collection := &model.Collection{}
|
collection := &model.Collection{}
|
||||||
|
|
||||||
if err := tx.Where("id = ?", collectionID).First(collection).Error; err != nil {
|
if err := tx.Where("id = ?", collectionID).First(collection).Error; err != nil {
|
||||||
return err
|
return model.NewRequestError("Invalid collection ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Model(&model.Resource{}).Where("id = ?", resourceID).First(&model.Resource{}).Error; err != nil {
|
||||||
|
return model.NewRequestError("Invalid resource ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Model(collection).Association("Resources").Append(&model.Resource{
|
if err := tx.Model(collection).Association("Resources").Append(&model.Resource{
|
||||||
@@ -91,6 +95,10 @@ func AddResourceToCollection(collectionID uint, resourceID uint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := tx.Model(collection).UpdateColumn("resources_count", gorm.Expr("resources_count + ?", 1)).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -100,7 +108,11 @@ func RemoveResourceFromCollection(collectionID uint, resourceID uint) error {
|
|||||||
collection := &model.Collection{}
|
collection := &model.Collection{}
|
||||||
|
|
||||||
if err := tx.Where("id = ?", collectionID).First(collection).Error; err != nil {
|
if err := tx.Where("id = ?", collectionID).First(collection).Error; err != nil {
|
||||||
return err
|
return model.NewRequestError("Invalid collection ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Model(&model.Resource{}).Where("id = ?", resourceID).First(&model.Resource{}).Error; err != nil {
|
||||||
|
return model.NewRequestError("Invalid resource ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Model(collection).Association("Resources").Delete(&model.Resource{
|
if err := tx.Model(collection).Association("Resources").Delete(&model.Resource{
|
||||||
@@ -111,6 +123,10 @@ func RemoveResourceFromCollection(collectionID uint, resourceID uint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := tx.Model(collection).UpdateColumn("resources_count", gorm.Expr("resources_count - ?", 1)).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -4,28 +4,31 @@ 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"`
|
||||||
Images []Image `gorm:"many2many:collection_images;"`
|
ResourcesCount int `gorm:"default:0"`
|
||||||
Resources []Resource `gorm:"many2many:collection_resources;"`
|
Images []Image `gorm:"many2many:collection_images;"`
|
||||||
|
Resources []Resource `gorm:"many2many:collection_resources;"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollectionView struct {
|
type CollectionView struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Article string `json:"article"`
|
Article string `json:"article"`
|
||||||
User UserView `json:"user"`
|
User UserView `json:"user"`
|
||||||
Images []Image `json:"images"`
|
ResourcesCount int `json:"resources_count"`
|
||||||
|
Images []Image `json:"images"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Collection) ToView() *CollectionView {
|
func (c Collection) ToView() *CollectionView {
|
||||||
return &CollectionView{
|
return &CollectionView{
|
||||||
ID: c.ID,
|
ID: c.ID,
|
||||||
Title: c.Title,
|
Title: c.Title,
|
||||||
Article: c.Article,
|
Article: c.Article,
|
||||||
User: c.User.ToView(),
|
User: c.User.ToView(),
|
||||||
Images: c.Images,
|
ResourcesCount: c.ResourcesCount,
|
||||||
|
Images: c.Images,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user