diff --git a/crm/_gen.php b/crm/_gen.php index db1260216..ff7f61ec1 100755 --- a/crm/_gen.php +++ b/crm/_gen.php @@ -46,9 +46,9 @@ foreach (array("structs", "handlers", "interfaces", "request", "") as $type) { $tpl->assign("name", $name); $tpl->assign("self", strtolower(substr($name, 0, 1))); $tpl->assign("api", $api); - $tpl->assign("fields", $api['struct']); + $tpl->assign("structs", $api['struct']); $tpl->assign("calls", $api['apis']); - $contents = $tpl->get(); + $contents = str_replace("\n}", "}", $tpl->get()); $save = true; if ($type === "" && file_exists($filename)) { diff --git a/crm/docs/README.md b/crm/docs/README.md index fbc61a8a7..5874f37c8 100644 --- a/crm/docs/README.md +++ b/crm/docs/README.md @@ -65,7 +65,7 @@ CRM module definitions | id | uint64 | POST | Module ID | N/A | NO | | name | string | POST | Module Name | N/A | YES | -## List contents from module section +## List/read contents from module section #### Method @@ -77,6 +77,7 @@ CRM module definitions | Parameter | Type | Method | Description | Default | Required? | | --------- | ---- | ------ | ----------- | ------- | --------- | +| id | string | GET | Module ID | N/A | NO | ## Add/update contents in module section diff --git a/crm/docs/src/spec.json b/crm/docs/src/spec.json index e672920ed..522d35503 100644 --- a/crm/docs/src/spec.json +++ b/crm/docs/src/spec.json @@ -34,8 +34,13 @@ "path": "module", "authentication": [], "struct": [ - { "name": "ID", "type": "uint64" }, - { "name": "Name", "type": "string" } + { + "name": "Module", + "fields": [ + { "name": "ID", "type": "uint64" }, + { "name": "Name", "type": "string" } + ] + } ], "apis": [ { @@ -62,7 +67,12 @@ { "name": "content/list", "method": "GET", - "title": "List contents from module section" + "title": "List/read contents from module section", + "parameters": { + "get": [ + { "type": "string", "name": "id", "title": "Module ID" } + ] + } }, { "name": "content/edit", diff --git a/crm/docs/src/spec/module.json b/crm/docs/src/spec/module.json index 14e34b17f..b36f5fff5 100644 --- a/crm/docs/src/spec/module.json +++ b/crm/docs/src/spec/module.json @@ -5,12 +5,17 @@ "Interface": "Module", "Struct": [ { - "name": "ID", - "type": "uint64" - }, - { - "name": "Name", - "type": "string" + "fields": [ + { + "name": "ID", + "type": "uint64" + }, + { + "name": "Name", + "type": "string" + } + ], + "name": "Module" } ], "Protocol": "", @@ -57,9 +62,17 @@ { "Name": "content/list", "Method": "GET", - "Title": "List contents from module section", + "Title": "List/read contents from module section", "Path": "/content/list", - "Parameters": null + "Parameters": { + "get": [ + { + "name": "id", + "title": "Module ID", + "type": "string" + } + ] + } }, { "Name": "content/edit", diff --git a/crm/module.go b/crm/module.go index 902f12456..f09677385 100644 --- a/crm/module.go +++ b/crm/module.go @@ -2,20 +2,51 @@ package crm import ( "github.com/pkg/errors" + "github.com/titpetric/factory" ) var _ = errors.Wrap func (*Module) List(r *moduleListRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Module.list") + db, err := factory.Database.Get() + if err != nil { + return nil, err + } + + if r.id > 0 { + m := Module{}.new() + return m, db.Get(m, "select * from crm_module id=?", r.id) + } + + res := make([]Module, 0) + err = db.Select(&res, "select * from crm_module order by name asc") + return res, err } func (*Module) Edit(r *moduleEditRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Module.edit") + db, err := factory.Database.Get() + if err != nil { + return nil, err + } + + m := Module{}.new() + m.SetID(r.id).SetName(r.name) + if m.GetID() > 0 { + return m, db.Replace("crm_module", m) + } + m.SetID(factory.Sonyflake.NextID()) + return m, db.Insert("crm_module", m) } func (*Module) ContentList(r *moduleContentListRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Module.content/list") + if r.id > 0 { + m := ModuleContentRow{}.new() + return m, db.Get(m, "select * from crm_module id=?", r.id) + } + + res := make([]ModuleContentRow, 0) + err = db.Select(&res, "select * from crm_module order by name asc") + return res, err } func (*Module) ContentEdit(r *moduleContentEditRequest) (interface{}, error) { diff --git a/crm/module.request.go b/crm/module.request.go index 9710aa53d..0b198a1a1 100644 --- a/crm/module.request.go +++ b/crm/module.request.go @@ -66,6 +66,7 @@ var _ RequestFiller = moduleEditRequest{}.new() // Module content/list request parameters type moduleContentListRequest struct { + id string } func (moduleContentListRequest) new() *moduleContentListRequest { @@ -83,6 +84,8 @@ func (m *moduleContentListRequest) Fill(r *http.Request) error { for name, param := range postVars { post[name] = string(param[0]) } + + m.id = get["id"] return nil } diff --git a/crm/module.structs.go b/crm/module.structs.go index 82ada002e..414c4fa5c 100644 --- a/crm/module.structs.go +++ b/crm/module.structs.go @@ -1,17 +1,21 @@ package crm -// Modules -type Module struct { - ID uint64 - Name string +type ( + // Modules + Module struct { + ID uint64 + Name string - changed []string -} + changed []string + } +) +/* Constructors */ func (Module) new() *Module { return &Module{} } +/* Getters/setters */ func (m *Module) GetID() uint64 { return m.ID } diff --git a/crm/templates/http_structs.tpl b/crm/templates/http_structs.tpl index cdf6c8a7f..d54b6868d 100644 --- a/crm/templates/http_structs.tpl +++ b/crm/templates/http_structs.tpl @@ -1,31 +1,39 @@ package {package} +type ({foreach $structs as $struct} + {if strpos($name, "Literal") !== false} - {foreach $fields as $field} + {foreach $struct.fields as $field} {field}{newline} {/foreach} {else} // {api.title} - type {name} struct { -{foreach $fields as $field} + {struct.name} struct { +{foreach $struct.fields as $field} {field.name} {field.type}{if $field.tag} `{$field.tag}`{/if}{newline} {/foreach} changed []string } -func ({name}) new() *{name} { - return &{name}{} +{/if}{/foreach} +) + +/* Constructors */ +{foreach $structs as $struct} +func ({struct.name}) new() *{struct.name} { + return &{struct.name}{} } +{/foreach} -{/if} - -{foreach $fields as $field} -func ({self} *{name}) Get{field.name}() {field.type} { +/* Getters/setters */ +{foreach $structs as $struct} +{foreach $struct.fields as $field} +func ({self} *{struct.name}) Get{field.name}() {field.type} { return {self}.{field.name} } -func ({self} *{name}) Set{field.name}(value {field.type}) *{name} {{if !$field.complex} +func ({self} *{struct.name}) Set{field.name}(value {field.type}) *{struct.name} {{if !$field.complex} if {self}.{field.name} != value { {self}.changed = append({self}.changed, "{field.name|strtolower}") {self}.{field.name} = value @@ -36,3 +44,5 @@ func ({self} *{name}) Set{field.name}(value {field.type}) *{name} {{if !$field.c return {self} } {/foreach} + +{/foreach} \ No newline at end of file diff --git a/crm/types.request.go b/crm/types.request.go index 0cff98a47..bebc6631e 100644 --- a/crm/types.request.go +++ b/crm/types.request.go @@ -8,8 +8,7 @@ import ( var _ = chi.URLParam // Types list request parameters -type typesListRequest struct { -} +type typesListRequest struct{} func (typesListRequest) new() *typesListRequest { return &typesListRequest{} diff --git a/crm/types.structs.go b/crm/types.structs.go index 860a03fce..0df2abf20 100644 --- a/crm/types.structs.go +++ b/crm/types.structs.go @@ -1,10 +1,7 @@ package crm -// Types -type Types struct { - changed []string -} +type () -func (Types) new() *Types { - return &Types{} -} +/* Constructors */ + +/* Getters/setters */