3
0

Fix record finder tests

This commit is contained in:
Denis Arh 2019-10-18 16:37:16 +02:00
parent cced35863c
commit 953cda26bb

View File

@ -4,10 +4,12 @@ import (
"context"
"time"
"github.com/jmoiron/sqlx"
"github.com/davecgh/go-spew/spew"
"github.com/titpetric/factory"
"gopkg.in/Masterminds/squirrel.v1"
"github.com/cortezaproject/corteza-server/messaging/types"
"github.com/cortezaproject/corteza-server/pkg/rh"
)
type (
@ -21,9 +23,6 @@ type (
DeleteAttachmentByID(id uint64) error
BindAttachment(attachmentId, messageId uint64) error
CountOwned(userID uint64) (c int, err error)
ChangeOwnership(userID, target uint64) error
}
attachment struct {
@ -32,22 +31,6 @@ type (
)
const (
sqlAttachmentColumns = `
a.id, a.rel_user,
a.url, a.preview_url,
a.name,
a.meta,
a.created_at, a.updated_at, a.deleted_at
`
sqlAttachmentScope = "deleted_at IS NULL"
sqlAttachmentByID = `SELECT ` + sqlAttachmentColumns + ` FROM messaging_attachment AS a WHERE id = ? AND ` + sqlAttachmentScope
sqlAttachmentByMessageID = `SELECT ` + sqlAttachmentColumns + `, rel_message
FROM messaging_attachment AS a
INNER JOIN messaging_message_attachment AS ma ON a.id = ma.rel_attachment
WHERE ma.rel_message IN (?) AND ` + sqlAttachmentScope
ErrAttachmentNotFound = repositoryError("AttachmentNotFound")
)
@ -55,62 +38,99 @@ func Attachment(ctx context.Context, db *factory.DB) AttachmentRepository {
return (&attachment{}).With(ctx, db)
}
func (r *attachment) With(ctx context.Context, db *factory.DB) AttachmentRepository {
func (r attachment) With(ctx context.Context, db *factory.DB) AttachmentRepository {
return &attachment{
repository: r.repository.With(ctx, db),
}
}
func (r *attachment) FindAttachmentByID(id uint64) (*types.Attachment, error) {
mod := &types.Attachment{}
return mod, isFound(r.db().Get(mod, sqlAttachmentByID, id), mod.ID > 0, ErrAttachmentNotFound)
func (r attachment) table() string {
return "messaging_attachment"
}
func (r *attachment) FindAttachmentByMessageID(IDs ...uint64) (rval types.MessageAttachmentSet, err error) {
rval = make([]*types.MessageAttachment, 0)
func (r attachment) tableMessage() string {
return "messaging_message_attachment"
}
func (r attachment) columns() []string {
return []string{
"a.id",
"a.rel_user",
"a.url",
"a.preview_url",
"a.name",
"a.meta",
"a.created_at",
"a.updated_at",
"a.deleted_at",
}
}
func (r attachment) query() squirrel.SelectBuilder {
return squirrel.
Select(r.columns()...).
From(r.table() + " AS a").
Where("a.deleted_at IS NULL")
}
func (r attachment) FindAttachmentByID(ID uint64) (*types.Attachment, error) {
return r.findOneBy("id", ID)
}
func (r attachment) findOneBy(field string, value interface{}) (*types.Attachment, error) {
var (
p = &types.Attachment{}
q = r.query().
Where(squirrel.Eq{field: value})
err = rh.FetchOne(r.db(), q, p)
)
if err != nil {
return nil, err
} else if p.ID == 0 {
return nil, ErrAttachmentNotFound
}
return p, nil
}
func (r attachment) FindAttachmentByMessageID(IDs ...uint64) (rval types.MessageAttachmentSet, err error) {
rval = types.MessageAttachmentSet{}
if len(IDs) == 0 {
return
}
if sql, args, err := sqlx.In(sqlAttachmentByMessageID, IDs); err != nil {
return nil, err
} else {
return rval, r.db().Select(&rval, sql, args...)
}
query := r.query().
Columns("ma.rel_message").
Join(r.tableMessage() + " AS ma ON (a.id = ma.rel_attachment)").
Where(squirrel.Eq{"rel_message": IDs})
spew.Dump(query.ToSql())
return rval, rh.FetchAll(r.db(), query, &rval)
}
func (r *attachment) CreateAttachment(mod *types.Attachment) (*types.Attachment, error) {
func (r attachment) CreateAttachment(mod *types.Attachment) (*types.Attachment, error) {
if mod.ID == 0 {
mod.ID = factory.Sonyflake.NextID()
}
mod.CreatedAt = time.Now()
return mod, r.db().Insert("messaging_attachment", mod)
return mod, r.db().Insert(r.table(), mod)
}
func (r *attachment) DeleteAttachmentByID(id uint64) error {
return r.updateColumnByID("messaging_attachment", "deleted_at", nil, id)
func (r attachment) DeleteAttachmentByID(id uint64) error {
return r.updateColumnByID(r.table(), "deleted_at", nil, id)
}
func (r *attachment) BindAttachment(attachmentId, messageId uint64) error {
func (r attachment) BindAttachment(attachmentId, messageId uint64) error {
bond := struct {
RelAttachment uint64 `db:"rel_attachment"`
RelMessage uint64 `db:"rel_message"`
}{attachmentId, messageId}
return r.db().Insert("messaging_message_attachment", bond)
}
func (r *attachment) CountOwned(userID uint64) (c int, err error) {
return c, r.db().Get(&c,
"SELECT COUNT(*) FROM messaging_attachment WHERE rel_user = ?",
userID)
}
func (r *attachment) ChangeOwnership(userID, target uint64) error {
_, err := r.db().Exec("UPDATE messaging_attachment SET rel_user = ? WHERE rel_user = ?", target, userID)
return err
return r.db().Insert(r.tableMessage(), bond)
}