From 871ba9300c6ef32c0db9a4291521fed8c24faab1 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 15 Oct 2018 16:36:06 +0800 Subject: [PATCH] fix line log count --- model/line_message_log.go | 23 +++++++++++++++++++++-- router/api/line.go | 10 +++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) 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