From 4a40060fbe8a6496348aad97557b5d032b23581e Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Wed, 15 Aug 2018 20:31:38 +0200 Subject: [PATCH] upd(crm): test friendly refactoring, gofmt --- crm/repository/content.go | 26 +++++++------------------- crm/repository/module.go | 23 +++++------------------ crm/repository/repository.go | 2 +- crm/types/module.go | 5 +++-- 4 files changed, 16 insertions(+), 40 deletions(-) diff --git a/crm/repository/content.go b/crm/repository/content.go index 62faaed40..e6759d5a8 100644 --- a/crm/repository/content.go +++ b/crm/repository/content.go @@ -36,41 +36,29 @@ func (r *content) With(ctx context.Context) Content { } } +// @todo: update to accepted DeletedAt column semantics from SAM func (r *content) FindByID(id uint64) (*types.Content, error) { mod := &types.Content{} - if err := r.db().Get(mod, "SELECT * FROM crm_content WHERE id = ?", id); err != nil { - println(err.Error()) - return nil, ErrDatabaseError - } else { - return mod, nil - } + return mod, r.db().Get(mod, "SELECT * FROM crm_module_content WHERE id=?", id) } func (r *content) Find() ([]*types.Content, error) { mod := make([]*types.Content, 0) - if err := r.db().Select(&mod, "SELECT * FROM crm_content ORDER BY name ASC"); err != nil { - println(err.Error()) - return nil, ErrDatabaseError - } else { - return mod, nil - } + return mod, r.db().Select(&mod, "SELECT * FROM crm_module_content ORDER BY id DESC") } func (r *content) Create(mod *types.Content) (*types.Content, error) { mod.ID = factory.Sonyflake.NextID() - return mod, r.db().Insert("crm_content", mod) + return mod, r.db().Insert("crm_module_content", mod) } func (r *content) Update(mod *types.Content) (*types.Content, error) { - return mod, r.db().Replace("crm_content", mod) + return mod, r.db().Replace("crm_module_content", mod) } func (r *content) DeleteByID(id uint64) error { - if _, err := r.db().Exec("DELETE FROM crm_content WHERE ID = ?", id); err != nil { - return ErrDatabaseError - } else { - return nil - } + _, err := r.db().Exec("DELETE FROM crm_module_content WHERE id=?", id) + return err } diff --git a/crm/repository/module.go b/crm/repository/module.go index 14f2c5798..f829c5521 100644 --- a/crm/repository/module.go +++ b/crm/repository/module.go @@ -36,25 +36,16 @@ func (r *module) With(ctx context.Context) Module { } } +// @todo: update to accepted DeletedAt column semantics from SAM func (r *module) FindByID(id uint64) (*types.Module, error) { mod := &types.Module{} - if err := r.db().Get(mod, "SELECT * FROM crm_module WHERE id = ?", id); err != nil { - println(err.Error()) - return nil, ErrDatabaseError - } else { - return mod, nil - } + return mod, r.db().Get(mod, "SELECT * FROM crm_module WHERE id=?", id) } func (r *module) Find() ([]*types.Module, error) { mod := make([]*types.Module, 0) - if err := r.db().Select(&mod, "SELECT * FROM crm_module ORDER BY name ASC"); err != nil { - println(err.Error()) - return nil, ErrDatabaseError - } else { - return mod, nil - } + return mod, r.db().Select(&mod, "SELECT * FROM crm_module ORDER BY name ASC") } func (r *module) Create(mod *types.Module) (*types.Module, error) { @@ -64,13 +55,9 @@ func (r *module) Create(mod *types.Module) (*types.Module, error) { func (r *module) Update(mod *types.Module) (*types.Module, error) { return mod, r.db().Replace("crm_module", mod) - } func (r *module) DeleteByID(id uint64) error { - if _, err := r.db().Exec("DELETE FROM crm_module WHERE ID = ?", id); err != nil { - return ErrDatabaseError - } else { - return nil - } + _, err := r.db().Exec("DELETE FROM crm_module WHERE id=?", id) + return err } diff --git a/crm/repository/repository.go b/crm/repository/repository.go index ad50d76ee..c8c3827fd 100644 --- a/crm/repository/repository.go +++ b/crm/repository/repository.go @@ -19,7 +19,7 @@ type ( func (r *repository) With(ctx context.Context) *repository { return &repository{ ctx: ctx, - tx: r.db().With(r.ctx), + tx: r.db().With(r.ctx), } } diff --git a/crm/types/module.go b/crm/types/module.go index 2112bc7ce..f46f04731 100644 --- a/crm/types/module.go +++ b/crm/types/module.go @@ -9,8 +9,9 @@ import ( type ( // Modules - CRM module definitions Module struct { - ID uint64 `db:"id"` - Name string `db:"name"` + ID uint64 `db:"id"` + Name string `db:"name"` + Fields types.JSONText `db:"json"` } // Modules - CRM module definitions