Refactor page repo
This commit is contained in:
@@ -230,6 +230,12 @@
|
||||
"type": "uint",
|
||||
"required": false,
|
||||
"title": "Returned items per page (default 50)"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"name": "sort",
|
||||
"required": false,
|
||||
"title": "Sort"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -59,6 +59,12 @@
|
||||
"required": false,
|
||||
"title": "Returned items per page (default 50)",
|
||||
"type": "uint"
|
||||
},
|
||||
{
|
||||
"name": "sort",
|
||||
"required": false,
|
||||
"title": "Sort",
|
||||
"type": "string"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/titpetric/factory"
|
||||
@@ -103,8 +102,6 @@ func (r module) findOneBy(namespaceID uint64, field string, value interface{}) (
|
||||
err = rh.FetchOne(r.db(), q, m)
|
||||
)
|
||||
|
||||
spew.Dump(m, err)
|
||||
|
||||
if m.ID == 0 {
|
||||
return nil, ErrModuleNotFound
|
||||
} else if err != nil {
|
||||
@@ -271,11 +268,7 @@ func (r module) FindFields(moduleIDs ...uint64) (ff types.ModuleFieldSet, err er
|
||||
ORDER BY rel_module, place`
|
||||
|
||||
query = fmt.Sprintf(query, r.tableFields())
|
||||
if moduleIDs[0] == 0 {
|
||||
spew.Dump(moduleIDs)
|
||||
panic("foo")
|
||||
|
||||
}
|
||||
if sql, args, err := sqlx.In(query, moduleIDs); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
|
||||
+38
-18
@@ -9,6 +9,7 @@ import (
|
||||
"gopkg.in/Masterminds/squirrel.v1"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -53,10 +54,19 @@ func (r page) table() string {
|
||||
|
||||
func (r page) columns() []string {
|
||||
return []string{
|
||||
"id", "rel_namespace", "self_id", "rel_module",
|
||||
"handle", "title",
|
||||
"blocks", "description", "visible", "weight",
|
||||
"created_at", "updated_at", "deleted_at",
|
||||
"id",
|
||||
"rel_namespace",
|
||||
"self_id",
|
||||
"rel_module",
|
||||
"handle",
|
||||
"title",
|
||||
"blocks",
|
||||
"description",
|
||||
"visible",
|
||||
"weight",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,13 +90,13 @@ func (r page) FindByModuleID(namespaceID, moduleID uint64) (*types.Page, error)
|
||||
}
|
||||
|
||||
func (r page) findOneBy(namespaceID uint64, field string, value interface{}) (*types.Page, error) {
|
||||
var p = &types.Page{}
|
||||
var (
|
||||
p = &types.Page{}
|
||||
|
||||
err := r.findOneInNamespaceBy(
|
||||
namespaceID,
|
||||
r.query().Columns(r.columns()...),
|
||||
squirrel.Eq{field: value},
|
||||
p,
|
||||
q = r.query().
|
||||
Where(squirrel.Eq{field: value, "rel_namespace": namespaceID})
|
||||
|
||||
err = rh.FetchOne(r.db(), q, p)
|
||||
)
|
||||
|
||||
if err == nil && p.ID == 0 {
|
||||
@@ -99,6 +109,10 @@ func (r page) findOneBy(namespaceID uint64, field string, value interface{}) (*t
|
||||
func (r page) Find(filter types.PageFilter) (set types.PageSet, f types.PageFilter, err error) {
|
||||
f = filter
|
||||
|
||||
if f.Sort == "" {
|
||||
f.Sort = "id ASC"
|
||||
}
|
||||
|
||||
query := r.query()
|
||||
|
||||
if filter.NamespaceID > 0 {
|
||||
@@ -116,23 +130,29 @@ func (r page) Find(filter types.PageFilter) (set types.PageSet, f types.PageFilt
|
||||
}
|
||||
|
||||
if f.Query != "" {
|
||||
q := "%" + f.Query + "%"
|
||||
query = query.Where("title LIKE ? OR description LIKE ?", q, q)
|
||||
q := "%" + strings.ToLower(f.Query) + "%"
|
||||
query = query.Where(squirrel.Or{
|
||||
squirrel.Like{"LOWER(title)": q},
|
||||
squirrel.Like{"LOWER(description)": q},
|
||||
})
|
||||
}
|
||||
|
||||
if f.IsReadable != nil {
|
||||
query = query.Where(f.IsReadable)
|
||||
}
|
||||
|
||||
if f.Count, err = r.count(query); err != nil || f.Count == 0 {
|
||||
var orderBy []string
|
||||
if orderBy, err = rh.ParseOrder(f.Sort, r.columns()...); err != nil {
|
||||
return
|
||||
} else {
|
||||
query = query.OrderBy(orderBy...)
|
||||
}
|
||||
|
||||
if f.Count, err = rh.Count(r.db(), query); err != nil || f.Count == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
query = query.
|
||||
Columns(r.columns()...).
|
||||
OrderBy("weight ASC")
|
||||
|
||||
return set, f, r.fetchPaged(&set, query, f.Page, f.PerPage)
|
||||
return set, f, rh.FetchPaged(r.db(), query, f.Page, f.PerPage, &set)
|
||||
}
|
||||
|
||||
func (r page) Reorder(namespaceID, parentID uint64, pageIDs []uint64) error {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/compose/service"
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/payload"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -54,10 +55,12 @@ func (ctrl *Page) List(ctx context.Context, r *request.PageList) (interface{}, e
|
||||
NamespaceID: r.NamespaceID,
|
||||
ParentID: r.SelfID,
|
||||
|
||||
Handle: r.Handle,
|
||||
Query: r.Query,
|
||||
PerPage: r.PerPage,
|
||||
Page: r.Page,
|
||||
Handle: r.Handle,
|
||||
Query: r.Query,
|
||||
|
||||
Sort: r.Sort,
|
||||
|
||||
PageFilter: rh.Paging(r.Page, r.PerPage),
|
||||
}
|
||||
|
||||
set, filter, err := ctrl.page.With(ctx).Find(f)
|
||||
|
||||
@@ -39,6 +39,7 @@ type PageList struct {
|
||||
Handle string
|
||||
Page uint
|
||||
PerPage uint
|
||||
Sort string
|
||||
NamespaceID uint64 `json:",string"`
|
||||
}
|
||||
|
||||
@@ -54,6 +55,7 @@ func (r PageList) Auditable() map[string]interface{} {
|
||||
out["handle"] = r.Handle
|
||||
out["page"] = r.Page
|
||||
out["perPage"] = r.PerPage
|
||||
out["sort"] = r.Sort
|
||||
out["namespaceID"] = r.NamespaceID
|
||||
|
||||
return out
|
||||
@@ -101,6 +103,9 @@ func (r *PageList) Fill(req *http.Request) (err error) {
|
||||
if val, ok := get["perPage"]; ok {
|
||||
r.PerPage = parseUint(val)
|
||||
}
|
||||
if val, ok := get["sort"]; ok {
|
||||
r.Sort = val
|
||||
}
|
||||
r.NamespaceID = parseUInt64(chi.URLParam(req, "namespaceID"))
|
||||
|
||||
return err
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/permissions"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -57,9 +58,11 @@ type (
|
||||
Root bool `json:"root,omitempty"`
|
||||
Handle string `json:"handle"`
|
||||
Query string `json:"query"`
|
||||
Page uint `json:"page"`
|
||||
PerPage uint `json:"perPage"`
|
||||
Count uint `json:"count"`
|
||||
|
||||
Sort string `json:"sort"`
|
||||
|
||||
// Standard paging fields & helpers
|
||||
rh.PageFilter
|
||||
|
||||
// Resource permission check filter
|
||||
IsReadable *permissions.ResourceFilter `json:"-"`
|
||||
|
||||
@@ -750,6 +750,7 @@ Compose pages
|
||||
| handle | string | GET | Search by handle | N/A | NO |
|
||||
| page | uint | GET | Page number (0 based) | N/A | NO |
|
||||
| perPage | uint | GET | Returned items per page (default 50) | N/A | NO |
|
||||
| sort | string | GET | Sort | N/A | NO |
|
||||
| namespaceID | uint64 | PATH | Namespace ID | N/A | YES |
|
||||
|
||||
## Create page
|
||||
|
||||
Reference in New Issue
Block a user