From c963dd7a2f6ba25bb838ed45ef0c9a2a8e2ec631 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Fri, 13 Jul 2018 13:37:55 +0200 Subject: [PATCH] support typed url params, add create handlers --- sam/channel.go | 2 +- sam/organisation.go | 21 +++++++++++++++------ sam/organisation.request.go | 6 +++--- sam/team.go | 22 ++++++++++++++++------ sam/team.request.go | 12 ++++++------ sam/templates/http_request.tpl | 2 +- 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/sam/channel.go b/sam/channel.go index 9c22a5414..d4bca3ddc 100644 --- a/sam/channel.go +++ b/sam/channel.go @@ -84,7 +84,7 @@ func (*Channel) Read(r *channelReadRequest) (interface{}, error) { return (&Channel{}).load(r.id) } -func (*Channel) Search(r *channelSearchRequest) (interface{}, error) { +func (*Channel) List(r *channelListRequest) (interface{}, error) { db, err := factory.Database.Get() if err != nil { return nil, err diff --git a/sam/organisation.go b/sam/organisation.go index e839cb853..43e539641 100644 --- a/sam/organisation.go +++ b/sam/organisation.go @@ -13,6 +13,19 @@ const ( sqlOrganisationSelect = "SELECT * FROM organisations WHERE " + sqlOrganisationScope ) +func (*Organisation) Create(r *organisationCreateRequest) (interface{}, error) { + db, err := factory.Database.Get() + if err != nil { + return nil, err + } + + // @todo: permission check if user can add/edit organisation + // @todo: make sure archived & deleted entries can not be edited + + o := Organisation{}.New().SetName(r.name).SetID(factory.Sonyflake.NextID()) + return o, db.Insert("organisation", o) +} + func (*Organisation) Edit(r *organisationEditRequest) (interface{}, error) { db, err := factory.Database.Get() if err != nil { @@ -23,11 +36,7 @@ func (*Organisation) Edit(r *organisationEditRequest) (interface{}, error) { // @todo: make sure archived & deleted entries can not be edited o := Organisation{}.New().SetID(r.id).SetName(r.name) - if o.GetID() > 0 { - return o, db.Replace("organisation", o) - } - o.SetID(factory.Sonyflake.NextID()) - return o, db.Insert("organisation", o) + return o, db.Replace("organisation", o) } func (*Organisation) Remove(r *organisationRemoveRequest) (interface{}, error) { @@ -59,7 +68,7 @@ func (*Organisation) Read(r *organisationReadRequest) (interface{}, error) { return o, db.Get(o, sqlOrganisationSelect+" AND id = ?", r.id) } -func (*Organisation) Search(r *organisationSearchRequest) (interface{}, error) { +func (*Organisation) List(r *organisationListRequest) (interface{}, error) { db, err := factory.Database.Get() if err != nil { return nil, err diff --git a/sam/organisation.request.go b/sam/organisation.request.go index b78dedb8d..0dac2eae9 100644 --- a/sam/organisation.request.go +++ b/sam/organisation.request.go @@ -101,7 +101,7 @@ func (o *organisationEditRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - o.id = chi.URLParam(r, "id") + o.id = parseUInt64(chi.URLParam(r, "id")) o.name = post["name"] return nil @@ -131,7 +131,7 @@ func (o *organisationRemoveRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - o.id = chi.URLParam(r, "id") + o.id = parseUInt64(chi.URLParam(r, "id")) return nil } @@ -187,7 +187,7 @@ func (o *organisationArchiveRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - o.id = chi.URLParam(r, "id") + o.id = parseUInt64(chi.URLParam(r, "id")) return nil } diff --git a/sam/team.go b/sam/team.go index 65e5226b1..cc09c1d98 100644 --- a/sam/team.go +++ b/sam/team.go @@ -13,6 +13,20 @@ const ( sqlTeamSelect = "SELECT * FROM teams WHERE " + sqlTeamScope ) +func (*Team) Create(r *teamCreateRequest) (interface{}, error) { + db, err := factory.Database.Get() + if err != nil { + return nil, err + } + + // @todo: permission check if user can add/edit the team + // @todo: make sure archived & deleted entries can not be edited + + t := Team{}.New() + t.SetName(r.name).SetMemberIDs(r.members).SetID(factory.Sonyflake.NextID()) + return t, db.Insert("team", t) +} + func (*Team) Edit(r *teamEditRequest) (interface{}, error) { db, err := factory.Database.Get() if err != nil { @@ -24,11 +38,7 @@ func (*Team) Edit(r *teamEditRequest) (interface{}, error) { t := Team{}.New() t.SetID(r.id).SetName(r.name).SetMemberIDs(r.members) - if t.GetID() > 0 { - return t, db.Replace("team", t) - } - t.SetID(factory.Sonyflake.NextID()) - return t, db.Insert("team", t) + return t, db.Replace("team", t) } func (*Team) Remove(r *teamRemoveRequest) (interface{}, error) { @@ -55,7 +65,7 @@ func (*Team) Read(r *teamReadRequest) (interface{}, error) { return t, db.Get(t, sqlTeamSelect+" AND id = ?", r.id) } -func (*Team) Search(r *teamSearchRequest) (interface{}, error) { +func (*Team) List(r *teamListRequest) (interface{}, error) { db, err := factory.Database.Get() if err != nil { return nil, err diff --git a/sam/team.request.go b/sam/team.request.go index 63dd5e88a..cb4dadf60 100644 --- a/sam/team.request.go +++ b/sam/team.request.go @@ -103,7 +103,7 @@ func (t *teamEditRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - t.id = chi.URLParam(r, "id") + t.id = parseUInt64(chi.URLParam(r, "id")) t.name = post["name"] return nil @@ -133,7 +133,7 @@ func (t *teamReadRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - t.id = chi.URLParam(r, "id") + t.id = parseUInt64(chi.URLParam(r, "id")) return nil } @@ -161,7 +161,7 @@ func (t *teamRemoveRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - t.id = chi.URLParam(r, "id") + t.id = parseUInt64(chi.URLParam(r, "id")) return nil } @@ -189,7 +189,7 @@ func (t *teamArchiveRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - t.id = chi.URLParam(r, "id") + t.id = parseUInt64(chi.URLParam(r, "id")) return nil } @@ -218,7 +218,7 @@ func (t *teamMoveRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - t.id = chi.URLParam(r, "id") + t.id = parseUInt64(chi.URLParam(r, "id")) t.organisation_id = parseUInt64(post["organisation_id"]) return nil @@ -249,7 +249,7 @@ func (t *teamMergeRequest) Fill(r *http.Request) error { post[name] = string(param[0]) } - t.id = chi.URLParam(r, "id") + t.id = parseUInt64(chi.URLParam(r, "id")) t.destination = parseUInt64(post["destination"]) return nil diff --git a/sam/templates/http_request.tpl b/sam/templates/http_request.tpl index b7576ee92..e0a30ebee 100644 --- a/sam/templates/http_request.tpl +++ b/sam/templates/http_request.tpl @@ -38,7 +38,7 @@ func ({self} *{name|lcfirst}{call.name|capitalize}Request) Fill(r *http.Request) {foreach $call.parameters as $method => $params} {foreach $params as $param} {if strtolower($method) === "path"} - {self}.{param.name} = chi.URLParam(r, "{param.name}") + {self}.{param.name} = {if ($param.type !== "string")}{$parsers[$param.type]}({/if}chi.URLParam(r, "{param.name}"){if ($param.type !== "string")}){/if}{newline} {elseif substr($param.type, 0, 2) !== '[]'} {self}.{param.name} = {if ($param.type !== "string")}{$parsers[$param.type]}({method|strtolower}["{param.name}"]){else}{method|strtolower}["{param.name}"]{/if}{newline} {/if}