From 93ceb60a42b458ff4d6e5e4d0bb867d7cf00c8ba Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Sat, 19 Oct 2019 16:58:37 +0200 Subject: [PATCH] Replace r.fetchSet with rh.FetchAll --- messaging/repository/repository.go | 19 ------------------- messaging/repository/unread.go | 21 +++++++++++---------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/messaging/repository/repository.go b/messaging/repository/repository.go index accd7f5dc..7dad143b6 100644 --- a/messaging/repository/repository.go +++ b/messaging/repository/repository.go @@ -3,7 +3,6 @@ package repository import ( "context" - "github.com/Masterminds/squirrel" "github.com/titpetric/factory" "github.com/cortezaproject/corteza-server/pkg/auth" @@ -46,21 +45,3 @@ func (r *repository) db() *factory.DB { } return DB(r.ctx) } - -// Fetches single row from table -func (r repository) fetchSet(set interface{}, q squirrel.SelectBuilder) (err error) { - var ( - sql string - args []interface{} - ) - - if sql, args, err = q.ToSql(); err != nil { - return - } - - if err = r.db().Select(set, sql, args...); err != nil { - return - } - - return -} diff --git a/messaging/repository/unread.go b/messaging/repository/unread.go index 55f1da0d3..5e7209bae 100644 --- a/messaging/repository/unread.go +++ b/messaging/repository/unread.go @@ -7,6 +7,7 @@ import ( "github.com/titpetric/factory" "github.com/cortezaproject/corteza-server/messaging/types" + "github.com/cortezaproject/corteza-server/pkg/rh" ) type ( @@ -73,14 +74,14 @@ func (r *unread) Count(userID, channelID uint64, threadIDs ...uint64) (types.Unr var ( uu = types.UnreadSet{} q = squirrel. - Select(). - From(r.table()). - Columns( + Select( "rel_channel", "rel_last_message", "rel_user", "rel_reply_to", - "count") + "count", + ). + From(r.table()) ) if userID > 0 { @@ -97,7 +98,7 @@ func (r *unread) Count(userID, channelID uint64, threadIDs ...uint64) (types.Unr q = q.Where(squirrel.Eq{"rel_reply_to": threadIDs}) } - return uu, r.fetchSet(&uu, q) + return uu, rh.FetchAll(r.db(), q, &uu) } // CountReplies counts unread thread info @@ -116,13 +117,13 @@ func (r unread) CountThreads(userID, channelID uint64) (types.UnreadSet, error) temp = []*u{} q = squirrel. - Select(). - From(r.table()). - Columns( + Select( "rel_channel", "rel_user", "sum(count) AS count", - "sum(CASE WHEN count > 0 THEN 1 ELSE 0 END) AS total"). + "sum(CASE WHEN count > 0 THEN 1 ELSE 0 END) AS total", + ). + From(r.table()). Where("rel_reply_to > 0 AND count > 0"). GroupBy("rel_channel", "rel_user") ) @@ -135,7 +136,7 @@ func (r unread) CountThreads(userID, channelID uint64) (types.UnreadSet, error) q = q.Where("rel_channel = ?", channelID) } - err = r.fetchSet(&temp, q) + err = rh.FetchAll(r.db(), q, &temp) if err != nil { return nil, err }