feat: relation field and picker
This commit is contained in:
1 parent
b45c99be3b
commit
37a77c9c22
92 files changed
+6826
-80
No files matched your search
@@ -2,8 +2,11 @@ package entry
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/contenttype"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func SameValue(left any, right any) bool {
|
||||
@@ -49,7 +52,7 @@ func Validate(fields []contenttype.Field, data map[string]any) error {
|
||||
|
||||
func validateField(field contenttype.Field, data map[string]any) error {
|
||||
value := data[field.Key]
|
||||
if value == nil {
|
||||
if value == nil || contenttype.EmptyRelation(field, value) {
|
||||
if field.Required {
|
||||
return InvalidField(field.Key, RuleRequired)
|
||||
}
|
||||
@@ -82,7 +85,7 @@ func firstUnknownField(fields []contenttype.Field, data map[string]any) error {
|
||||
func UniqueValues(fields []contenttype.Field, data map[string]any) []UniqueValue {
|
||||
var values []UniqueValue
|
||||
for _, field := range fields {
|
||||
if value := data[field.Key]; field.Unique && value != nil {
|
||||
if value := data[field.Key]; (field.Unique || field.IsExclusiveRelation()) && value != nil {
|
||||
values = append(values, UniqueValue{Key: field.Key, Value: value})
|
||||
}
|
||||
}
|
||||
@@ -103,3 +106,38 @@ func MissingMedia(field contenttype.Field, unchanged bool) error {
|
||||
}
|
||||
return InvalidField(field.Key, contenttype.RuleType)
|
||||
}
|
||||
|
||||
func RelationIDs(fields []contenttype.Field, data map[string]any) map[string]map[uint32]bool {
|
||||
ids := map[string]map[uint32]bool{}
|
||||
for _, ref := range contenttype.RelationRefs(fields, data) {
|
||||
held := make(map[uint32]bool, len(ref.IDs))
|
||||
for _, id := range ref.IDs {
|
||||
held[id] = true
|
||||
}
|
||||
ids[ref.Field.Key] = held
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func MissingRelation(field contenttype.Field, unchanged bool) error {
|
||||
return MissingMedia(field, unchanged)
|
||||
}
|
||||
|
||||
func MatchesQuery(fields []contenttype.Field, data map[string]any, query string) bool {
|
||||
if len(fields) == 0 {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(strings.ToLower(displayText(data[fields[0].Key])), strings.ToLower(query))
|
||||
}
|
||||
|
||||
func displayText(value any) string {
|
||||
switch typed := value.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return typed
|
||||
case float64:
|
||||
return strconv.FormatFloat(typed, 'f', -1, 64)
|
||||
}
|
||||
return fmt.Sprint(value)
|
||||
}
|
||||
Reference in new issue
Block a user