feat(API): media field type
This commit is contained in:
1 parent
ad5faf3d19
commit
a2d596f4ca
63 files changed
+2567
-259
No files matched your search
@@ -3,16 +3,57 @@ package useCases
|
||||
import (
|
||||
"RenewCMS/internal/domain/contenttype"
|
||||
"RenewCMS/internal/domain/entry"
|
||||
"RenewCMS/internal/domain/image"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type TypedEntry struct {
|
||||
entry.Entry
|
||||
ContentTypeKey string
|
||||
Media map[string]EntryMedia
|
||||
}
|
||||
|
||||
func typedEntry(owner contenttype.ContentType, record entry.Entry) TypedEntry {
|
||||
func typedEntry(owner contenttype.ContentType, record entry.Entry, resolver *mediaResolver) (TypedEntry, error) {
|
||||
record.Data = entry.Project(owner.Fields, record.Data)
|
||||
return TypedEntry{Entry: record, ContentTypeKey: owner.Key}
|
||||
media, err := resolver.resolve(owner, record)
|
||||
if err != nil {
|
||||
return TypedEntry{}, err
|
||||
}
|
||||
return TypedEntry{Entry: record, ContentTypeKey: owner.Key, Media: media}, nil
|
||||
}
|
||||
|
||||
func ensureMediaExists(images image.Repository, owner contenttype.ContentType, data map[string]any, stored map[string]any) error {
|
||||
storedIDs := entry.MediaIDs(owner.Fields, stored)
|
||||
for _, field := range contenttype.MediaFields(owner.Fields) {
|
||||
id, set := contenttype.MediaID(data[field.Key])
|
||||
if !set {
|
||||
continue
|
||||
}
|
||||
unchanged := stored != nil && storedIDs[field.Key] == id
|
||||
if unchanged && !field.Required {
|
||||
continue
|
||||
}
|
||||
exists, err := imageExists(images, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
if unchanged {
|
||||
return entry.InvalidField(field.Key, entry.RuleRequired)
|
||||
}
|
||||
return entry.InvalidField(field.Key, contenttype.RuleType)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func imageExists(images image.Repository, id uint32) (bool, error) {
|
||||
_, err := images.Get(id)
|
||||
if errors.Is(err, image.ErrImageNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
func ensureUnique(entries entry.Repository, owner contenttype.ContentType, data map[string]any, excludeID uint32) error {
|
||||
|
||||
Reference in new issue
Block a user