diff --git a/model/commands.go b/model/commands.go index 6aced12..999b9c0 100644 --- a/model/commands.go +++ b/model/commands.go @@ -121,3 +121,17 @@ func GetCommands(where map[string]string, offset, limit int, order map[string]st return } + +// AddCommand - +func AddCommand(cmdkey, message, group string) (cmd *Commands, err error) { + if len(cmdkey) == 0 || len(message) == 0 { + return nil, errors.New("cmd or message is empty") + } + query := `insert into "public"."commands" ("cmd", "message", "group") values ($1, $2, $3) returning *` + cmd = &Commands{} + err = x.Get(cmd, query, cmdkey, message, group) + if err != nil { + return nil, err + } + return +} diff --git a/module/context/context.go b/module/context/context.go index f83c611..97992c2 100644 --- a/module/context/context.go +++ b/module/context/context.go @@ -15,7 +15,7 @@ type Context struct { type CustomMiddle func(*Context) // PatchCtx - patch ctx to custom middle -func PatchCtx(handler func(*Context)) gin.HandlerFunc { +func PatchCtx(handler CustomMiddle) gin.HandlerFunc { return func(c *gin.Context) { ctx := &Context{ Context: c, diff --git a/router/api/line.go b/router/api/line.go index 57ef575..313b0cc 100644 --- a/router/api/line.go +++ b/router/api/line.go @@ -157,3 +157,34 @@ func GetCommandList(c *context.Context) { }, }) } + +// AddLineGroupCommand - +func AddLineGroupCommand(c *context.Context) { + // TODO + bodyStruct := struct { + Cmd string `json:"cmd"` + Message string `json:"message"` + Group string `json:"group"` + }{} + + err := c.BindData(&bodyStruct) + if err != nil { + c.DataFormat(nil) + return + } + + if len(bodyStruct.Cmd) == 0 || len(bodyStruct.Message) == 0 { + c.DataFormat(nil) + return + } + + cmd, err := model.AddCommand(bodyStruct.Cmd, bodyStruct.Message, bodyStruct.Group) + if err != nil { + c.ServerError(nil) + return + } + + c.Success(map[string]interface{}{ + "cmd": utils.ToMap(cmd), + }) +} diff --git a/router/routes/routes.go b/router/routes/routes.go index 7139158..2c675c7 100644 --- a/router/routes/routes.go +++ b/router/routes/routes.go @@ -72,6 +72,7 @@ func SetRoutes(r *gin.Engine) { apiGroup.GET("/line/logs", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineMessageLog)) apiGroup.GET("/line/groups", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineList)) apiGroup.GET("/line/cmds", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetCommandList)) + apiGroup.POST("/line/cmd", context.PatchCtx(api.CheckSession), context.PatchCtx(api.AddLineGroupCommand)) apiGroup.GET("/session", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetSessionData)) apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus)) }