72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
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)
|
|
}
|