diff --git a/model/line_message_log.go b/model/line_message_log.go index 7162629..825a902 100644 --- a/model/line_message_log.go +++ b/model/line_message_log.go @@ -32,8 +32,27 @@ func AddLineMessageLog(g, u, msg, t string) (msglog *LineMessageLog, err error) } // GetLineMessageLogCount - -func GetLineMessageLogCount() (c int, err error) { - err = x.Get(&c, `select count(*) as c from "public"."line_message_log"`) +func GetLineMessageLogCount(where ...interface{}) (c int, err error) { + query := `select count(*) as c from "public"."line_message_log"` + values := make([]string, 0) + if len(where) > 0 { + if whereMap, ok := where[0].(map[string]string); ok { + str := "" + idx := 1 + for k, v := range whereMap { + if len(str) > 0 { + str += " and " + } + str += fmt.Sprintf(` "%s" = $%d`, k, idx) + idx++ + values = append(values, v) + } + if len(str) > 0 { + query += " where " + str + } + } + } + err = x.Get(&c, query, values) return } diff --git a/router/api/line.go b/router/api/line.go index 3636621..a239a50 100644 --- a/router/api/line.go +++ b/router/api/line.go @@ -49,7 +49,15 @@ func GetLineMessageLog(c *context.Context) { g := c.DefaultQuery("group", "") u := c.DefaultQuery("user", "") - count, err := model.GetLineMessageLogCount() + where := make(map[string]string) + if len(g) > 0 { + where["group"] = g + } + if len(u) > 0 { + where["user"] = u + } + + count, err := model.GetLineMessageLogCount(where) if err != nil { c.ServerError(nil) return