add(crm): load fields from database

This commit is contained in:
Tit Petric
2018-08-23 11:32:47 +00:00
parent 53014f9b24
commit 5e76b12901
8 changed files with 50 additions and 77 deletions
-7
View File
@@ -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"
)
+1 -5
View File
@@ -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 {
+9 -53
View File
@@ -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")
}
+28
View File
@@ -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)
}
}
}
+6 -2
View File
@@ -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)
}
}
+1 -5
View File
@@ -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 {
+2 -2
View File
@@ -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)
}
+3 -3
View File
@@ -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) {