Add charactor api.

This commit is contained in:
2025-11-15 16:00:26 +08:00
parent 5cd708454a
commit c9c8eac734
5 changed files with 170 additions and 44 deletions

44
server/model/charactor.go Normal file
View File

@@ -0,0 +1,44 @@
package model
type Charactor struct {
ID uint `gorm:"primaryKey;autoIncrement"`
Name string `gorm:"type:varchar(100);not null"`
Alias []string `gorm:"serializer:json"`
CV string `gorm:"type:varchar(100)"`
ImageID uint
ResourceID uint
Image *Image `gorm:"foreignKey:ImageID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}
type CharactorView struct {
Id uint `json:"id"`
Name string `json:"name"`
Alias []string `json:"alias"`
CV string `json:"cv"`
Image uint `json:"image"`
}
func (c *Charactor) ToView() *CharactorView {
return &CharactorView{
Id: c.ID,
Name: c.Name,
Alias: c.Alias,
CV: c.CV,
Image: c.ImageID,
}
}
func (c *Charactor) Equal(other *Charactor) bool {
if c.Name != other.Name || c.CV != other.CV || c.ImageID != other.ImageID {
return false
}
if len(c.Alias) != len(other.Alias) {
return false
}
for i := range c.Alias {
if c.Alias[i] != other.Alias[i] {
return false
}
}
return true
}

View File

@@ -21,8 +21,9 @@ type Resource struct {
Downloads uint
Comments uint
ModifiedTime time.Time
Gallery []uint `gorm:"serializer:json"`
GalleryNsfw []uint `gorm:"serializer:json"`
Gallery []uint `gorm:"serializer:json"`
GalleryNsfw []uint `gorm:"serializer:json"`
Charactors []Charactor `gorm:"foreignKey:ResourceID"`
}
type Link struct {
@@ -40,22 +41,23 @@ type ResourceView struct {
}
type ResourceDetailView struct {
ID uint `json:"id"`
Title string `json:"title"`
AlternativeTitles []string `json:"alternativeTitles"`
Links []Link `json:"links"`
Article string `json:"article"`
CreatedAt time.Time `json:"createdAt"`
Tags []TagView `json:"tags"`
Images []ImageView `json:"images"`
Files []FileView `json:"files"`
Author UserView `json:"author"`
Views uint `json:"views"`
Downloads uint `json:"downloads"`
Comments uint `json:"comments"`
Related []ResourceView `json:"related"`
Gallery []uint `json:"gallery"`
GalleryNsfw []uint `json:"galleryNsfw"`
ID uint `json:"id"`
Title string `json:"title"`
AlternativeTitles []string `json:"alternativeTitles"`
Links []Link `json:"links"`
Article string `json:"article"`
CreatedAt time.Time `json:"createdAt"`
Tags []TagView `json:"tags"`
Images []ImageView `json:"images"`
Files []FileView `json:"files"`
Author UserView `json:"author"`
Views uint `json:"views"`
Downloads uint `json:"downloads"`
Comments uint `json:"comments"`
Related []ResourceView `json:"related"`
Gallery []uint `json:"gallery"`
GalleryNsfw []uint `json:"galleryNsfw"`
Charactors []CharactorView `json:"charactors"`
}
func (r *Resource) ToView() ResourceView {
@@ -94,6 +96,10 @@ func (r *Resource) ToDetailView() ResourceDetailView {
for i, file := range r.Files {
files[i] = *file.ToView()
}
charactors := make([]CharactorView, len(r.Charactors))
for i, charactor := range r.Charactors {
charactors[i] = *charactor.ToView()
}
return ResourceDetailView{
ID: r.ID,
Title: r.Title,
@@ -110,5 +116,6 @@ func (r *Resource) ToDetailView() ResourceDetailView {
Comments: r.Comments,
Gallery: r.Gallery,
GalleryNsfw: r.GalleryNsfw,
Charactors: charactors,
}
}