mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 07:51:14 +00:00
feat: cover
This commit is contained in:
@@ -14,8 +14,9 @@ type Resource struct {
|
||||
ReleaseDate *time.Time
|
||||
Article string
|
||||
Images []Image `gorm:"many2many:resource_images;"`
|
||||
Tags []Tag `gorm:"many2many:resource_tags;"`
|
||||
Files []File `gorm:"foreignKey:ResourceID"`
|
||||
CoverID *uint
|
||||
Tags []Tag `gorm:"many2many:resource_tags;"`
|
||||
Files []File `gorm:"foreignKey:ResourceID"`
|
||||
UserID uint
|
||||
User User
|
||||
Views uint
|
||||
@@ -52,6 +53,7 @@ type ResourceDetailView struct {
|
||||
ReleaseDate *time.Time `json:"releaseDate,omitempty"`
|
||||
Tags []TagView `json:"tags"`
|
||||
Images []ImageView `json:"images"`
|
||||
CoverID *uint `json:"coverId,omitempty"`
|
||||
Files []FileView `json:"files"`
|
||||
Author UserView `json:"author"`
|
||||
Views uint `json:"views"`
|
||||
@@ -78,7 +80,18 @@ func (r *Resource) ToView() ResourceView {
|
||||
}
|
||||
|
||||
var image *ImageView
|
||||
if len(r.Images) > 0 {
|
||||
if r.CoverID != nil {
|
||||
// Use the cover image if specified
|
||||
for _, img := range r.Images {
|
||||
if img.ID == *r.CoverID {
|
||||
v := img.ToView()
|
||||
image = &v
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// If no cover is set or cover image not found, use the first image
|
||||
if image == nil && len(r.Images) > 0 {
|
||||
v := r.Images[0].ToView()
|
||||
image = &v
|
||||
}
|
||||
@@ -122,6 +135,7 @@ func (r *Resource) ToDetailView() ResourceDetailView {
|
||||
ReleaseDate: r.ReleaseDate,
|
||||
Tags: tags,
|
||||
Images: images,
|
||||
CoverID: r.CoverID,
|
||||
Files: files,
|
||||
Author: r.User.ToView(),
|
||||
Views: r.Views,
|
||||
|
||||
@@ -34,6 +34,7 @@ type ResourceParams struct {
|
||||
Tags []uint `json:"tags"`
|
||||
Article string `json:"article"`
|
||||
Images []uint `json:"images"`
|
||||
CoverID *uint `json:"cover_id"`
|
||||
Gallery []uint `json:"gallery"`
|
||||
GalleryNsfw []uint `json:"gallery_nsfw"`
|
||||
Characters []CharacterParams `json:"characters"`
|
||||
@@ -110,6 +111,14 @@ func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
||||
}
|
||||
date = &parsedDate
|
||||
}
|
||||
// Validate CoverID if provided
|
||||
var coverID *uint
|
||||
if params.CoverID != nil && *params.CoverID != 0 {
|
||||
if !slices.Contains(params.Images, *params.CoverID) {
|
||||
return 0, model.NewRequestError("Cover ID must be one of the resource images")
|
||||
}
|
||||
coverID = params.CoverID
|
||||
}
|
||||
r := model.Resource{
|
||||
Title: params.Title,
|
||||
AlternativeTitles: params.AlternativeTitles,
|
||||
@@ -117,6 +126,7 @@ func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
||||
Links: params.Links,
|
||||
ReleaseDate: date,
|
||||
Images: images,
|
||||
CoverID: coverID,
|
||||
Tags: tags,
|
||||
UserID: uid,
|
||||
Gallery: gallery,
|
||||
@@ -548,11 +558,21 @@ func UpdateResource(uid, rid uint, params *ResourceParams) error {
|
||||
date = &parsedDate
|
||||
}
|
||||
|
||||
// Validate CoverID if provided
|
||||
var coverID *uint
|
||||
if params.CoverID != nil && *params.CoverID != 0 {
|
||||
if !slices.Contains(params.Images, *params.CoverID) {
|
||||
return model.NewRequestError("Cover ID must be one of the resource images")
|
||||
}
|
||||
coverID = params.CoverID
|
||||
}
|
||||
|
||||
r.Title = params.Title
|
||||
r.AlternativeTitles = params.AlternativeTitles
|
||||
r.Article = params.Article
|
||||
r.Links = params.Links
|
||||
r.ReleaseDate = date
|
||||
r.CoverID = coverID
|
||||
r.Gallery = gallery
|
||||
r.GalleryNsfw = nsfw
|
||||
r.Characters = characters
|
||||
|
||||
Reference in New Issue
Block a user