From ef5e56717b1e6e642646d55ad3bd143cfe0337b7 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Tue, 16 Oct 2018 12:31:20 +0200 Subject: [PATCH] add(crm): page info for modules --- crm/repository/module.go | 14 +++++++++++++- crm/repository/page.go | 15 +++++++++++---- crm/types/types.go | 2 ++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/crm/repository/module.go b/crm/repository/module.go index c554eb28e..64676a988 100644 --- a/crm/repository/module.go +++ b/crm/repository/module.go @@ -44,7 +44,13 @@ func (r *module) With(ctx context.Context, db *factory.DB) ModuleRepository { func (r *module) FindByID(id uint64) (*types.Module, error) { mod := &types.Module{} - return mod, r.db().Get(mod, "SELECT * FROM crm_module WHERE id=?", id) + if err := r.db().Get(mod, "SELECT * FROM crm_module WHERE id=?", id); err != nil { + return nil, err + } + if err := r.fillPage(mod); err != nil { + return nil, err + } + return mod, nil } func (r *module) Find() ([]*types.Module, error) { @@ -110,3 +116,9 @@ func (r *module) FieldNames(mod *types.Module) ([]string, error) { return result, nil } } + +func (r *module) fillPage(mod *types.Module) (err error) { + api := Page(r.Context(), r.db()) + mod.Page, err = api.FindByModuleID(mod.ID) + return +} diff --git a/crm/repository/page.go b/crm/repository/page.go index ca612957b..bbe0970a8 100644 --- a/crm/repository/page.go +++ b/crm/repository/page.go @@ -3,6 +3,7 @@ package repository import ( "context" + "github.com/pkg/errors" "github.com/titpetric/factory" "github.com/crusttech/crust/crm/types" @@ -50,10 +51,7 @@ func (r *page) FindByID(id uint64) (*types.Page, error) { func (r *page) FindByModuleID(id uint64) (*types.Page, error) { page := &types.Page{} if err := r.db().Get(page, "SELECT * FROM crm_page WHERE module_id=?", id); err != nil { - return page, err - } - if err := r.fillModule(page); err != nil { - return page, err + return nil, err } return page, nil } @@ -73,6 +71,15 @@ func (r *page) Find() ([]*types.Page, error) { func (r *page) Create(page *types.Page) (*types.Page, error) { page.ID = factory.Sonyflake.NextID() + if page.ModuleID > 0 { + check, err := r.FindByModuleID(page.ModuleID) + if err != nil { + return nil, err + } + if check.ID > 0 { + return nil, errors.New("Page for module already exists") + } + } return page, r.db().Insert("crm_page", page) } diff --git a/crm/types/types.go b/crm/types/types.go index 800488756..8f5f6704a 100644 --- a/crm/types/types.go +++ b/crm/types/types.go @@ -39,6 +39,8 @@ type ( Name string `json:"name" db:"name"` Fields types.JSONText `json:"fields" db:"json"` + Page *Page `json:"page,omitempty"` + CreatedAt time.Time `db:"created_at" json:"createdAt,omitempty"` UpdatedAt *time.Time `db:"updated_at" json:"updatedAt,omitempty"` DeletedAt *time.Time `db:"deleted_at" json:"deletedAt,omitempty"`