feat: custom content types backend
This commit is contained in:
1 parent
ed67b7e70d
commit
58d58f5a04
41 files changed
+4086
-2
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package entry
|
||||
|
||||
import "strings"
|
||||
|
||||
type SortField string
|
||||
|
||||
type SortDirection string
|
||||
|
||||
const (
|
||||
SortByID SortField = "id"
|
||||
SortByCreatedAt SortField = "created_at"
|
||||
SortByUpdatedAt SortField = "updated_at"
|
||||
|
||||
SortAsc SortDirection = "asc"
|
||||
SortDesc SortDirection = "desc"
|
||||
)
|
||||
|
||||
type Sort struct {
|
||||
Field SortField
|
||||
Direction SortDirection
|
||||
}
|
||||
|
||||
func (s Sort) Valid() bool {
|
||||
switch s.Field {
|
||||
case SortByID, SortByCreatedAt, SortByUpdatedAt:
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return s.Direction == SortAsc || s.Direction == SortDesc
|
||||
}
|
||||
|
||||
func (s Sort) String() string {
|
||||
return string(s.Field) + ":" + string(s.Direction)
|
||||
}
|
||||
|
||||
func ParseSort(value string) (Sort, error) {
|
||||
field, direction, found := strings.Cut(value, ":")
|
||||
parsed := Sort{Field: SortField(field), Direction: SortDirection(direction)}
|
||||
if !found || !parsed.Valid() {
|
||||
return Sort{}, ErrInvalidSort
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func DefaultSort() Sort {
|
||||
return Sort{Field: SortByUpdatedAt, Direction: SortDesc}
|
||||
}
|
||||
|
||||
type Filters struct {
|
||||
ContentTypeID uint32
|
||||
Page int
|
||||
Size int
|
||||
Sort Sort
|
||||
Unpaged bool
|
||||
}
|
||||
|
||||
type PageResult struct {
|
||||
Items []Entry
|
||||
Total int
|
||||
}
|
||||
|
||||
type Repository interface {
|
||||
Create(entry Entry) (Entry, error)
|
||||
Get(contentTypeID uint32, id uint32) (Entry, error)
|
||||
ListPage(filters Filters) (PageResult, error)
|
||||
Update(entry Entry) (Entry, error)
|
||||
Delete(contentTypeID uint32, id uint32) error
|
||||
DeleteByContentType(contentTypeID uint32) error
|
||||
CountByContentType(contentTypeID uint32) (int, error)
|
||||
FindByFieldValue(contentTypeID uint32, fieldKey string, value any, excludeID uint32) ([]Entry, error)
|
||||
}
|
||||
Reference in new issue
Block a user