From a7581c74b5cff5642c43d307919c68bd7d606862 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 10 Dec 2018 16:48:22 +0800 Subject: [PATCH] add delete command api --- model/commands.go | 26 ++++++++++++++++++++++++++ router/api/line.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/model/commands.go b/model/commands.go index 999b9c0..1028f65 100644 --- a/model/commands.go +++ b/model/commands.go @@ -135,3 +135,29 @@ func AddCommand(cmdkey, message, group string) (cmd *Commands, err error) { } return } + +// CheckCommand - +func CheckCommand(cmd, group string) (exist bool, err error) { + if len(cmd) == 0 { + return false, errors.New("cmd is empty") + } + query := `select count(*) as c from "public"."commands" where "cmd" = $1 and "group" = $2` + c := 0 + + err = x.Get(&c, query, cmd, group) + if err != nil { + return false, err + } + + return c > 0, nil +} + +// DeleteCommand - +func DeleteCommand(cmd, group string) (err error) { + if len(cmd) == 0 { + return errors.New("cmd is empty") + } + query := `delete from "public"."commands" where "cmd" = $1 and "group" = $2` + _, err = x.Exec(query, cmd, group) + return +} diff --git a/router/api/line.go b/router/api/line.go index 313b0cc..afa07fa 100644 --- a/router/api/line.go +++ b/router/api/line.go @@ -160,7 +160,6 @@ func GetCommandList(c *context.Context) { // AddLineGroupCommand - func AddLineGroupCommand(c *context.Context) { - // TODO bodyStruct := struct { Cmd string `json:"cmd"` Message string `json:"message"` @@ -188,3 +187,31 @@ func AddLineGroupCommand(c *context.Context) { "cmd": utils.ToMap(cmd), }) } + +// DeleteLineGroupCommand - +func DeleteLineGroupCommand(c *context.Context) { + cmd, ok := c.Params.Get("cmd") + if !ok { + c.DataFormat(nil) + return + } + g := c.DefaultQuery("group", "") + + exist, err := model.CheckCommand(cmd, g) + if err != nil { + c.ServerError(nil) + return + } + if !exist { + c.NotFound(nil) + return + } + // TODO + err = model.DeleteCommand(cmd, g) + if err != nil { + c.ServerError(nil) + return + } + + c.Success(nil) +}