add(crm): content links table (one to many)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -133,11 +133,22 @@ func (r *content) Create(mod *types.Content) (*types.Content, error) {
|
||||
return nil, errors.Wrap(err, "No content")
|
||||
}
|
||||
|
||||
r.db().Exec("delete from crm_content_links where content_id=?", mod.ID)
|
||||
for _, v := range fields {
|
||||
v.ContentID = mod.ID
|
||||
if err := r.db().Replace("crm_content_column", v); err != nil {
|
||||
return nil, errors.Wrap(err, "Error adding columns")
|
||||
}
|
||||
for _, related := range v.Related {
|
||||
row := types.Related{
|
||||
ContentID: v.ContentID,
|
||||
Name: v.Name,
|
||||
RelatedContentID: related,
|
||||
}
|
||||
if err := r.db().Replace("crm_content_links", row); err != nil {
|
||||
return nil, errors.Wrap(err, "Error adding column links")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := r.db().Insert("crm_content", mod); err != nil {
|
||||
@@ -156,11 +167,22 @@ func (r *content) Update(mod *types.Content) (*types.Content, error) {
|
||||
return nil, errors.Wrap(err, "Error when saving content, no content")
|
||||
}
|
||||
|
||||
r.db().Exec("delete from crm_content_links where content_id=?", mod.ID)
|
||||
for _, v := range fields {
|
||||
v.ContentID = mod.ID
|
||||
if err := r.db().Replace("crm_content_column", v); err != nil {
|
||||
return nil, errors.Wrap(err, "Error adding columns to database")
|
||||
}
|
||||
for _, related := range v.Related {
|
||||
row := types.Related{
|
||||
ContentID: v.ContentID,
|
||||
Name: v.Name,
|
||||
RelatedContentID: related,
|
||||
}
|
||||
if err := r.db().Replace("crm_content_links", row); err != nil {
|
||||
return nil, errors.Wrap(err, "Error adding column links")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mod, r.db().Replace("crm_content", mod)
|
||||
|
||||
@@ -41,6 +41,10 @@ func TestContent(t *testing.T) {
|
||||
Name: "email",
|
||||
Type: "email",
|
||||
},
|
||||
types.Field{
|
||||
Name: "options",
|
||||
Type: "select_multi",
|
||||
},
|
||||
})
|
||||
assert(t, err == nil, "Error when encoding JSON fields: %+v", err)
|
||||
|
||||
@@ -56,20 +60,26 @@ func TestContent(t *testing.T) {
|
||||
assert(t, module.ID > 0, "Expected auto generated ID")
|
||||
}
|
||||
|
||||
columns := []types.ContentColumn{
|
||||
types.ContentColumn{
|
||||
Name: "name",
|
||||
Value: "Tit Petric",
|
||||
},
|
||||
types.ContentColumn{
|
||||
Name: "email",
|
||||
Value: "tit.petric@example.com",
|
||||
},
|
||||
types.ContentColumn{
|
||||
Name: "options",
|
||||
Related: []string{"1", "2", "3"},
|
||||
},
|
||||
}
|
||||
|
||||
content := &types.Content{
|
||||
ModuleID: module.ID,
|
||||
}
|
||||
(&content.Fields).Scan(func() []byte {
|
||||
b, _ := json.Marshal([]types.ContentColumn{
|
||||
types.ContentColumn{
|
||||
Name: "name",
|
||||
Value: "Tit Petric",
|
||||
},
|
||||
types.ContentColumn{
|
||||
Name: "email",
|
||||
Value: "tit.petric@example.com",
|
||||
},
|
||||
})
|
||||
b, _ := json.Marshal(columns)
|
||||
return b
|
||||
}())
|
||||
|
||||
@@ -94,17 +104,19 @@ func TestContent(t *testing.T) {
|
||||
// fields := make([]testContentRow, 0)
|
||||
// err = json.Unmarshal(ms.Fields, &fields)
|
||||
assert(t, err == nil, "%+v", errors.Wrap(err, "Didn't expect error when unmarshalling"))
|
||||
assert(t, len(fields) == 2, "Expected different field count: %d != %d", 2, len(fields))
|
||||
assert(t, fields[0].Name == "name", "Expected field.0 type = name, got %s", fields[0].Name)
|
||||
assert(t, fields[1].Name == "email", "Expected field.1 type = email, got %s", fields[1].Name)
|
||||
assert(t, len(fields) == len(columns), "Expected different field count: %d != %d", 2, len(fields))
|
||||
for k, v := range columns {
|
||||
assert(t, fields[k].Name == v.Name, "Expected fields[%d].Name = %s, got %s", k, fields[k].Name, v.Name)
|
||||
}
|
||||
}
|
||||
{
|
||||
fields := make([]types.ContentColumn, 0)
|
||||
err := json.Unmarshal(ms.Fields, &fields)
|
||||
assert(t, err == nil, "%+v", errors.Wrap(err, "Didn't expect error when unmarshalling"))
|
||||
assert(t, len(fields) == 2, "Expected different field count: %d != %d", 2, len(fields))
|
||||
assert(t, fields[0].Name == "name", "Expected field.0 type = name, got %s", fields[0].Name)
|
||||
assert(t, fields[1].Name == "email", "Expected field.1 type = email, got %s", fields[1].Name)
|
||||
assert(t, len(fields) == len(columns), "Expected different field count: %d != %d", 2, len(fields))
|
||||
for k, v := range columns {
|
||||
assert(t, fields[k].Name == v.Name, "Expected fields[%d].Name = %s, got %s", k, fields[k].Name, v.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-2
@@ -28,9 +28,18 @@ type (
|
||||
|
||||
// ContentColumn is a stored row in the `content_column` table
|
||||
ContentColumn struct {
|
||||
ContentID uint64 `json:"-" db:"content_id"`
|
||||
Name string `json:"name" db:"column_name"`
|
||||
Value string `json:"value" db:"column_value"`
|
||||
Related []string `json:"related" db:"-"`
|
||||
}
|
||||
|
||||
Related struct {
|
||||
ContentID uint64 `json:"-" db:"content_id"`
|
||||
Name string `json:"name" db:"column_name"`
|
||||
Value string `json:"value" db:"column_value"`
|
||||
Name string `json:"-" db:"column_name"`
|
||||
|
||||
// RelatedContentID isn't necessarily a content ID (multiple-select anything goes options)
|
||||
RelatedContentID string `json:"-" db:"rel_content_id"`
|
||||
}
|
||||
|
||||
// Field - CRM input field definitions
|
||||
|
||||
Reference in New Issue
Block a user