From 53669a298f3e7c725c1b8e47df19d47db0b596cf Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 15 Oct 2018 16:12:11 +0800 Subject: [PATCH 1/4] add update sql mtime --- model/donate_setting.go | 2 +- model/facebook_page.go | 2 +- model/twitch_channel.go | 8 ++++---- model/youtube_channel.go | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/model/donate_setting.go b/model/donate_setting.go index bd90949..3064bac 100644 --- a/model/donate_setting.go +++ b/model/donate_setting.go @@ -46,7 +46,7 @@ func (p *DonateSetting) InsertOrUpdate() (err error) { "end_date" = :end_date, "target_amount" = :target_amount, "title" = :title, - "start_amount" = :start_amount` + "start_amount" = :start_amount, "mtime" = now()` _, err = x.NamedExec(query, p) return } diff --git a/model/facebook_page.go b/model/facebook_page.go index 7f6c27e..6711ba3 100644 --- a/model/facebook_page.go +++ b/model/facebook_page.go @@ -51,7 +51,7 @@ func (p *FacebookPage) AddPage() (err error) { // UpdatePost - func (p *FacebookPage) UpdatePost(postID string) (err error) { - query := `update "public"."facebook_page" set "lastpost" = $1 where id = $2` + query := `update "public"."facebook_page" set "lastpost" = $1, "mtime" = now() where id = $2` _, err = x.Exec(query, postID, p.ID) if err != nil { return diff --git a/model/twitch_channel.go b/model/twitch_channel.go index 079fa1e..51eeb65 100644 --- a/model/twitch_channel.go +++ b/model/twitch_channel.go @@ -85,7 +85,7 @@ func (p *TwitchChannel) Add() (err error) { // UpdateStream - func (p *TwitchChannel) UpdateStream(streamID string) (err error) { - query := `update "public"."twitch_channel" set "laststream" = $1 where "id" = $2` + query := `update "public"."twitch_channel" set "laststream" = $1, "mtime" = now() where "id" = $2` _, err = x.Exec(query, streamID, p.ID) if err != nil { return @@ -96,7 +96,7 @@ func (p *TwitchChannel) UpdateStream(streamID string) (err error) { // UpdateName - func (p *TwitchChannel) UpdateName(name string) (err error) { - _, err = x.Exec(`update "public"."twitch_channel" set "name" = $1 where "id" = $2`, name, p.ID) + _, err = x.Exec(`update "public"."twitch_channel" set "name" = $1, "mtime" = now() where "id" = $2`, name, p.ID) if err != nil { return } @@ -107,14 +107,14 @@ func (p *TwitchChannel) UpdateName(name string) (err error) { // UpdateJoin - func (p *TwitchChannel) UpdateJoin(join bool) (err error) { p.Join = join - _, err = x.NamedExec(`update "public"."twitch_channel" set "join" = :join where "id" = :id`, p) + _, err = x.NamedExec(`update "public"."twitch_channel" set "join" = :join, "mtime" = now() where "id" = :id`, p) return } // UpdateOpayID - func (p *TwitchChannel) UpdateOpayID(id string) (err error) { p.OpayID = id - _, err = x.NamedExec(`update "public"."twitch_channel" set "opayid" = :opayid where "id" = :id`, p) + _, err = x.NamedExec(`update "public"."twitch_channel" set "opayid" = :opayid, "mtime" = now() where "id" = :id`, p) return } diff --git a/model/youtube_channel.go b/model/youtube_channel.go index b9cdba0..92001d4 100644 --- a/model/youtube_channel.go +++ b/model/youtube_channel.go @@ -57,14 +57,14 @@ func (p *YoutubeChannel) Add() (err error) { // UpdateLastVideo - func (p *YoutubeChannel) UpdateLastVideo(vid string) (err error) { p.LastVideo = vid - _, err = x.NamedExec(`update "public"."youtube_channel" set "lastvideo" = :lastvideo where "id" = :id`, p) + _, err = x.NamedExec(`update "public"."youtube_channel" set "lastvideo" = :lastvideo, "mtime" = now() where "id" = :id`, p) return } // UpdateExpire - func (p *YoutubeChannel) UpdateExpire(t int64) (err error) { p.Expire = t - _, err = x.NamedExec(`update "public"."youtube_channel" set "expire" = :expire where "id" = :id`, p) + _, err = x.NamedExec(`update "public"."youtube_channel" set "expire" = :expire, "mtime" = now() where "id" = :id`, p) if err != nil { return err } From 871ba9300c6ef32c0db9a4291521fed8c24faab1 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 15 Oct 2018 16:36:06 +0800 Subject: [PATCH 2/4] 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 From b24625235d1eebd3a9335ce4d17c22233903d1d4 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 15 Oct 2018 16:39:06 +0800 Subject: [PATCH 3/4] add log --- router/api/line.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/router/api/line.go b/router/api/line.go index a239a50..ab093aa 100644 --- a/router/api/line.go +++ b/router/api/line.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "strconv" "git.trj.tw/golang/mtfosbot/model" @@ -59,6 +60,7 @@ func GetLineMessageLog(c *context.Context) { count, err := model.GetLineMessageLogCount(where) if err != nil { + fmt.Println("count error :::: ", err) c.ServerError(nil) return } From 77a7be52243e3f159bf362ef7978e2e68aa780cb Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 15 Oct 2018 16:46:00 +0800 Subject: [PATCH 4/4] fix count --- model/line_message_log.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/line_message_log.go b/model/line_message_log.go index 825a902..f60e10b 100644 --- a/model/line_message_log.go +++ b/model/line_message_log.go @@ -34,7 +34,7 @@ func AddLineMessageLog(g, u, msg, t string) (msglog *LineMessageLog, err error) // GetLineMessageLogCount - func GetLineMessageLogCount(where ...interface{}) (c int, err error) { query := `select count(*) as c from "public"."line_message_log"` - values := make([]string, 0) + values := make([]interface{}, 0) if len(where) > 0 { if whereMap, ok := where[0].(map[string]string); ok { str := "" @@ -52,7 +52,7 @@ func GetLineMessageLogCount(where ...interface{}) (c int, err error) { } } } - err = x.Get(&c, query, values) + err = x.Get(&c, query, values...) return }