[MM-36711] nested sql query for reply_count in getSingle post method of post_sto… (#17845)

* nested sql query for reply_count in getSingle post method of post_store.go

* added tests for replyCount in GetSingle api

* updated the post object message field in GetSingle unit test to use model.NewRandomString
Этот коммит содержится в:
source-punk
2021-07-16 20:00:23 +05:30
коммит произвёл GitHub
родитель c88232d71e
Коммит a59d3a9fa6
2 изменённых файлов: 32 добавлений и 3 удалений

Просмотреть файл

@@ -598,13 +598,19 @@ func (s *SqlPostStore) Get(ctx context.Context, id string, skipFetchThreads, col
func (s *SqlPostStore) GetSingle(id string, inclDeleted bool) (*model.Post, error) {
query := s.getQueryBuilder().
Select("*").
Select("p.*").
From("Posts p").
Where(sq.Eq{"p.Id": id})
replyCountSubQuery := s.getQueryBuilder().
Select("COUNT(Posts.Id)").
From("Posts").
Where(sq.Eq{"Id": id})
Where(sq.Expr("Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0"))
if !inclDeleted {
query = query.Where(sq.Eq{"DeleteAt": 0})
query = query.Where(sq.Eq{"p.DeleteAt": 0})
}
query = query.Column(sq.Alias(replyCountSubQuery, "ReplyCount"))
queryString, args, err := query.ToSql()
if err != nil {