Refactor attachment repo
This commit is contained in:
@@ -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 (
|
||||
@@ -46,41 +47,55 @@ func (r attachment) table() string {
|
||||
|
||||
func (r attachment) columns() []string {
|
||||
return []string{
|
||||
"a.id", "a.rel_namespace", "a.rel_owner", "a.kind",
|
||||
"a.url", "a.preview_url",
|
||||
"a.id",
|
||||
"a.rel_namespace",
|
||||
"a.rel_owner",
|
||||
"a.kind",
|
||||
"a.url",
|
||||
"a.preview_url",
|
||||
"a.name",
|
||||
"a.meta",
|
||||
"a.created_at", "a.updated_at", "a.deleted_at",
|
||||
"a.created_at",
|
||||
"a.updated_at",
|
||||
"a.deleted_at",
|
||||
}
|
||||
}
|
||||
|
||||
func (r attachment) query() squirrel.SelectBuilder {
|
||||
return squirrel.
|
||||
Select().
|
||||
Select(r.columns()...).
|
||||
From(r.table() + " AS a").
|
||||
Where("a.deleted_at IS NULL")
|
||||
|
||||
}
|
||||
|
||||
func (r attachment) FindByID(namespaceID, attachmentID uint64) (*types.Attachment, error) {
|
||||
var (
|
||||
query = r.query().
|
||||
Columns(r.columns()...).
|
||||
Where("a.id = ?", attachmentID)
|
||||
return r.findOneBy(namespaceID, "id", attachmentID)
|
||||
}
|
||||
|
||||
a = &types.Attachment{}
|
||||
func (r attachment) findOneBy(namespaceID uint64, field string, value interface{}) (*types.Attachment, error) {
|
||||
var (
|
||||
p = &types.Attachment{}
|
||||
|
||||
q = r.query().
|
||||
Where(squirrel.Eq{field: value, "rel_namespace": namespaceID})
|
||||
|
||||
err = rh.FetchOne(r.db(), q, p)
|
||||
)
|
||||
|
||||
if namespaceID > 0 {
|
||||
query = query.Where("a.rel_namespace = ?", namespaceID)
|
||||
if err == nil && p.ID == 0 {
|
||||
return nil, ErrAttachmentNotFound
|
||||
}
|
||||
|
||||
return a, isFound(r.fetchOne(a, query), a.ID > 0, ErrAttachmentNotFound)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (r attachment) Find(filter types.AttachmentFilter) (set types.AttachmentSet, f types.AttachmentFilter, err error) {
|
||||
f = filter
|
||||
// f.PerPage = normalizePerPage(f.PerPage, 5, 100, 50)
|
||||
|
||||
if f.Sort == "" {
|
||||
f.Sort = "id ASC"
|
||||
}
|
||||
|
||||
query := r.query().
|
||||
Where(squirrel.Eq{"a.kind": f.Kind})
|
||||
@@ -96,6 +111,7 @@ func (r attachment) Find(filter types.AttachmentFilter) (set types.AttachmentSet
|
||||
err = errors.New("filtering by pageID not implemented")
|
||||
return
|
||||
}
|
||||
|
||||
case types.RecordAttachment:
|
||||
query = query.
|
||||
Join("compose_record_value AS v ON (v.ref = a.id)")
|
||||
@@ -113,6 +129,7 @@ func (r attachment) Find(filter types.AttachmentFilter) (set types.AttachmentSet
|
||||
if f.FieldName != "" {
|
||||
query = query.Where(squirrel.Eq{"v.name": f.FieldName})
|
||||
}
|
||||
|
||||
default:
|
||||
err = errors.New("unsupported kind value")
|
||||
return
|
||||
@@ -123,15 +140,18 @@ func (r attachment) Find(filter types.AttachmentFilter) (set types.AttachmentSet
|
||||
return
|
||||
}
|
||||
|
||||
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("id 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 attachment) Create(mod *types.Attachment) (*types.Attachment, error) {
|
||||
|
||||
@@ -4,10 +4,6 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/titpetric/factory"
|
||||
"gopkg.in/Masterminds/squirrel.v1"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -22,11 +18,6 @@ func DB(ctx context.Context) *factory.DB {
|
||||
return factory.Database.MustGet("compose").With(ctx)
|
||||
}
|
||||
|
||||
// Identity returns the User ID from context
|
||||
func Identity(ctx context.Context) uint64 {
|
||||
return auth.GetIdentityFromContext(ctx).Identity()
|
||||
}
|
||||
|
||||
// With updates repository and database contexts
|
||||
func (r *repository) With(ctx context.Context, db *factory.DB) *repository {
|
||||
return &repository{
|
||||
@@ -47,37 +38,3 @@ func (r *repository) db() *factory.DB {
|
||||
}
|
||||
return DB(r.ctx)
|
||||
}
|
||||
|
||||
func (r repository) findOneInNamespaceBy(namespaceID uint64, q squirrel.SelectBuilder, eq squirrel.Eq, row interface{}) error {
|
||||
q = q.Where(eq)
|
||||
|
||||
if namespaceID > 0 {
|
||||
q = q.Where("rel_namespace = ?", namespaceID)
|
||||
}
|
||||
|
||||
if err := r.fetchOne(row, q); err != nil {
|
||||
row = nil
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Fetches single row from table
|
||||
func (r repository) fetchOne(one interface{}, q squirrel.SelectBuilder) (err error) {
|
||||
return rh.FetchOne(r.db(), q, one)
|
||||
}
|
||||
|
||||
// Counts all rows that match conditions from given query builder
|
||||
func (r repository) count(q squirrel.SelectBuilder) (uint, error) {
|
||||
return rh.Count(r.db(), q)
|
||||
}
|
||||
|
||||
// Fetches paged rows
|
||||
func (r repository) fetchPaged(set interface{}, q squirrel.SelectBuilder, page, perPage uint) error {
|
||||
return rh.FetchPaged(r.db(), q, page, perPage, set)
|
||||
}
|
||||
|
||||
func isFound(err error, valid bool, nerr error) error {
|
||||
return rh.IsFound(err, valid, nerr)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/cortezaproject/corteza-server/compose/service"
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
"github.com/cortezaproject/corteza-server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -52,10 +53,8 @@ func (ctrl Attachment) List(ctx context.Context, r *request.AttachmentList) (int
|
||||
ModuleID: r.ModuleID,
|
||||
RecordID: r.RecordID,
|
||||
FieldName: r.FieldName,
|
||||
// Filter: r.Filter,
|
||||
PerPage: r.PerPage,
|
||||
Page: r.Page,
|
||||
// Sort: r.Sort,
|
||||
|
||||
PageFilter: rh.Paging(r.Page, r.PerPage),
|
||||
}
|
||||
|
||||
set, filter, err := ctrl.attachment.With(ctx).Find(f)
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/rh"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -34,10 +36,11 @@ type (
|
||||
ModuleID uint64 `json:"moduleID,string,omitempty"`
|
||||
FieldName string `json:"fieldName,omitempty"`
|
||||
Filter string `json:"filter"`
|
||||
Page uint `json:"page"`
|
||||
PerPage uint `json:"perPage"`
|
||||
Sort string `json:"sort"`
|
||||
Count uint `json:"count"`
|
||||
|
||||
Sort string `json:"sort"`
|
||||
|
||||
// Standard paging fields & helpers
|
||||
rh.PageFilter
|
||||
}
|
||||
|
||||
attachmentImageMeta struct {
|
||||
|
||||
Reference in New Issue
Block a user