api.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package presets
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "github.com/qor5/ui/vuetifyx"
  7. "github.com/qor5/web"
  8. h "github.com/theplant/htmlgo"
  9. )
  10. type ComponentFunc func(ctx *web.EventContext) h.HTMLComponent
  11. type ObjectComponentFunc func(obj interface{}, ctx *web.EventContext) h.HTMLComponent
  12. type EditingTitleComponentFunc func(obj interface{}, defaultTitle string, ctx *web.EventContext) h.HTMLComponent
  13. type FieldComponentFunc func(obj interface{}, field *FieldContext, ctx *web.EventContext) h.HTMLComponent
  14. type ActionComponentFunc func(id string, ctx *web.EventContext) h.HTMLComponent
  15. type ActionUpdateFunc func(id string, ctx *web.EventContext) (err error)
  16. type BulkActionComponentFunc func(selectedIds []string, ctx *web.EventContext) h.HTMLComponent
  17. type BulkActionUpdateFunc func(selectedIds []string, ctx *web.EventContext) (err error)
  18. type BulkActionSelectedIdsProcessorFunc func(selectedIds []string, ctx *web.EventContext) (processedSelectedIds []string, err error)
  19. type BulkActionSelectedIdsProcessorNoticeFunc func(selectedIds []string, processedSelectedIds []string, unactionableIds []string) string
  20. type MessagesFunc func(r *http.Request) *Messages
  21. // Data Layer
  22. type DataOperator interface {
  23. Search(obj interface{}, params *SearchParams, ctx *web.EventContext) (r interface{}, totalCount int, err error)
  24. // return ErrRecordNotFound if record not found
  25. Fetch(obj interface{}, id string, ctx *web.EventContext) (r interface{}, err error)
  26. Save(obj interface{}, id string, ctx *web.EventContext) (err error)
  27. Delete(obj interface{}, id string, ctx *web.EventContext) (err error)
  28. }
  29. type SetterFunc func(obj interface{}, ctx *web.EventContext)
  30. type FieldSetterFunc func(obj interface{}, field *FieldContext, ctx *web.EventContext) (err error)
  31. type ValidateFunc func(obj interface{}, ctx *web.EventContext) (err web.ValidationErrors)
  32. type SearchFunc func(model interface{}, params *SearchParams, ctx *web.EventContext) (r interface{}, totalCount int, err error)
  33. type FetchFunc func(obj interface{}, id string, ctx *web.EventContext) (r interface{}, err error)
  34. type SaveFunc func(obj interface{}, id string, ctx *web.EventContext) (err error)
  35. type DeleteFunc func(obj interface{}, id string, ctx *web.EventContext) (err error)
  36. type SQLCondition struct {
  37. Query string
  38. Args []interface{}
  39. }
  40. type SearchParams struct {
  41. KeywordColumns []string
  42. Keyword string
  43. SQLConditions []*SQLCondition
  44. PerPage int64
  45. Page int64
  46. OrderBy string
  47. PageURL *url.URL
  48. }
  49. type SlugDecoder interface {
  50. PrimaryColumnValuesBySlug(slug string) map[string]string
  51. }
  52. func RecoverPrimaryColumnValuesBySlug(dec SlugDecoder, slug string) (r map[string]string, err error) {
  53. defer func() {
  54. if e := recover(); e != nil {
  55. r = nil
  56. err = fmt.Errorf("wrong slug: %v", slug)
  57. }
  58. }()
  59. r = dec.PrimaryColumnValuesBySlug(slug)
  60. return r, nil
  61. }
  62. type SlugEncoder interface {
  63. PrimarySlug() string
  64. }
  65. type FilterDataFunc func(ctx *web.EventContext) vuetifyx.FilterData
  66. type FilterTab struct {
  67. ID string
  68. Label string
  69. // render AdvancedLabel if it is not nil
  70. AdvancedLabel h.HTMLComponent
  71. Query url.Values
  72. }
  73. type FilterTabsFunc func(ctx *web.EventContext) []*FilterTab