From 5e76b12901b2ff8153a750aca4856f65a39aa78c Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Thu, 23 Aug 2018 11:32:47 +0000 Subject: [PATCH] add(crm): load fields from database --- crm/repository/constants.go | 7 ---- crm/repository/content.go | 6 +--- crm/repository/field.go | 62 ++++++------------------------------ crm/repository/field_test.go | 28 ++++++++++++++++ crm/repository/main_test.go | 8 +++-- crm/repository/module.go | 6 +--- crm/rest/field.go | 4 +-- crm/service/field.go | 6 ++-- 8 files changed, 50 insertions(+), 77 deletions(-) delete mode 100644 crm/repository/constants.go create mode 100644 crm/repository/field_test.go diff --git a/crm/repository/constants.go b/crm/repository/constants.go deleted file mode 100644 index 8799fedf6..000000000 --- a/crm/repository/constants.go +++ /dev/null @@ -1,7 +0,0 @@ -package repository - -const ( - // @todo root should be configurable - // @todo move this to db or stack it inside the binary or container - fieldPath = "crm/data/%s.json" -) diff --git a/crm/repository/content.go b/crm/repository/content.go index 5a357270e..d34e5b723 100644 --- a/crm/repository/content.go +++ b/crm/repository/content.go @@ -23,11 +23,7 @@ type ( ) func NewContent(ctx context.Context) Content { - return &content{ - repository: &repository{ - ctx: ctx, - }, - } + return (&content{}).With(ctx) } func (r *content) With(ctx context.Context) Content { diff --git a/crm/repository/field.go b/crm/repository/field.go index 43f5e1c34..3811dfab6 100644 --- a/crm/repository/field.go +++ b/crm/repository/field.go @@ -2,19 +2,14 @@ package repository import ( "context" - "encoding/json" - "fmt" "github.com/crusttech/crust/crm/types" - "os" - "path" - "path/filepath" ) type ( Field interface { With(ctx context.Context) Field - FindByName(name string) (*types.Field, error) + FindByType(t string) (*types.Field, error) Find() ([]*types.Field, error) } @@ -24,11 +19,7 @@ type ( ) func NewField(ctx context.Context) Field { - return &field{ - repository: &repository{ - ctx: ctx, - }, - } + return (&field{}).With(ctx) } func (r *field) With(ctx context.Context) Field { @@ -37,49 +28,14 @@ func (r *field) With(ctx context.Context) Field { } } -// Finds field by it's name and returns it -func (f *field) FindByName(name string) (*types.Field, error) { - return f.fieldDecode(fmt.Sprintf(fieldPath, name)) +// FindByName returns field with a given name +func (f *field) FindByType(t string) (*types.Field, error) { + res := &types.Field{} + return res, f.db().Get(res, "SELECT * from crm_fields where field_type=?", t) } -// Returns all known fields +// Find returns all known fields func (f *field) Find() ([]*types.Field, error) { - matches, err := filepath.Glob(fmt.Sprintf(fieldPath, "*")) - if err != nil { - return nil, err - } - - res := make([]*types.Field, len(matches)) - for i, match := range matches { - if res[i], err = f.fieldDecode(match); err != nil { - return nil, err - } - } - return res, nil -} - -func (f *field) fieldDecode(filepath string) (*types.Field, error) { - file, err := os.Open(filepath) - if err != nil { - // @todo wrap error - return nil, err - } - - defer file.Close() - - // Removes path and extension from full filename - fieldTypeFromPath := func(filepath string) string { - t := path.Base(filepath) - return t[:len(t)-5] - } - - // Preset field's type with name of the file (sans .json) - // if type is explicitly set within the file, it will be overwritten - field := &types.Field{Type: fieldTypeFromPath(filepath)} - if err := json.NewDecoder(file).Decode(&field); err != nil { - // @todo wrap error - return nil, err - } - - return field, nil + mod := make([]*types.Field, 0) + return mod, f.db().Select(&mod, "SELECT * FROM crm_fields ORDER BY field_name ASC") } diff --git a/crm/repository/field_test.go b/crm/repository/field_test.go new file mode 100644 index 000000000..85d736212 --- /dev/null +++ b/crm/repository/field_test.go @@ -0,0 +1,28 @@ +package repository + +import ( + "context" + "testing" +) + +func TestField(t *testing.T) { + + repository := NewField(context.TODO()).With(context.Background()) + + { + // fetch all fields + { + ms, err := repository.Find() + must(t, err, "Error when retrieving fields") + assert(t, len(ms) > 1, "Expected more than one field") + } + + // fetch named field + { + m, err := repository.FindByType("email") + must(t, err, "Error when retrieving field by name") + assert(t, m != nil, "Unexpected nil value for field by name") + assert(t, m.Type == "email", "Unexpected type, expected email, got %s", m.Type) + } + } +} diff --git a/crm/repository/main_test.go b/crm/repository/main_test.go index 6cb09da08..63592589e 100644 --- a/crm/repository/main_test.go +++ b/crm/repository/main_test.go @@ -38,9 +38,13 @@ func db() *factory.DB { return factory.Database.MustGet() } -func must(t *testing.T, err error) { +func must(t *testing.T, err error, message ...string) { + prefix := "Error" + if len(message) > 0 { + prefix = message[0] + } if err != nil { - t.Fatalf("Error: %v", err) + t.Fatalf(prefix + ": %+v", err) } } diff --git a/crm/repository/module.go b/crm/repository/module.go index f829c5521..cb1aafa24 100644 --- a/crm/repository/module.go +++ b/crm/repository/module.go @@ -23,11 +23,7 @@ type ( ) func NewModule(ctx context.Context) Module { - return &module{ - repository: &repository{ - ctx: ctx, - }, - } + return (&module{}).With(ctx) } func (r *module) With(ctx context.Context) Module { diff --git a/crm/rest/field.go b/crm/rest/field.go index 01a151212..acb906df7 100644 --- a/crm/rest/field.go +++ b/crm/rest/field.go @@ -17,7 +17,7 @@ type ( } FieldService interface { - FindByName(ctx context.Context, name string) (*types.Field, error) + FindByType(ctx context.Context, t string) (*types.Field, error) Find(ctx context.Context) ([]*types.Field, error) } ) @@ -33,5 +33,5 @@ func (s *Field) List(ctx context.Context, _ *server.FieldListRequest) (interface } func (s *Field) Type(ctx context.Context, r *server.FieldTypeRequest) (interface{}, error) { - return s.field.With(ctx).FindByName(r.ID) + return s.field.With(ctx).FindByType(r.ID) } diff --git a/crm/service/field.go b/crm/service/field.go index 70418ff66..14edbef9f 100644 --- a/crm/service/field.go +++ b/crm/service/field.go @@ -13,7 +13,7 @@ type ( FieldService interface { With(ctx context.Context) FieldService - FindByName(name string) (*types.Field, error) + FindByType(t string) (*types.Field, error) Find() ([]*types.Field, error) } ) @@ -30,8 +30,8 @@ func (s *field) With(ctx context.Context) FieldService { } } -func (s *field) FindByName(name string) (*types.Field, error) { - return s.repository.FindByName(name) +func (s *field) FindByType(t string) (*types.Field, error) { + return s.repository.FindByType(t) } func (s *field) Find() ([]*types.Field, error) {