0
\$\begingroup\$

The calculatePage function - Calculates if a parent and child comment have more than 15 comments then consider it as page. The next 15 comments should be moved into the next page.

The logic works fine. The reason i posted here is there any better naming convention for variables.

Any improvement in my code flow is appreciated.

public static Dictionary<int,int> CalculatePage(IEnumerable<dynamic> pageCount) {
                var result = new Dictionary<int,int>();
                int offset = 0, lastOffset = 0;
                int sum = 0;
                foreach (var item in pageCount) {
                    sum += item.ParentChildSum;
                    if (sum >= Constants.DefaultCommentPageSize) {
                        result.Add(lastOffset, offset - lastOffset + 1);
                        sum = 0;
                        lastOffset = offset + 1;
                    }
                    offset++;
                }
    
                if (sum < Constants.DefaultCommentPageSize) {
                    result.Add(lastOffset, offset - lastOffset);
                    return result;
                }
                return result;
            }

The Index Discussion function index the comments posted in blog into solr for full text search. Is there any approach i can consider to avoid connecting with sql twice.

    private void IndexDiscussion(DiscussionSummary discussion, string baseUrl) {
        try {
            var pageCount = _connection.Query(
                @"SELECT ( ca1.content + ca2.childcontent ) AS ParentChildSum
                    FROM   comments c
                    CROSS apply (SELECT CASE
                    WHEN c.isdeleted = 1
                    OR c.approved = 0 THEN 0
                    ELSE 1
                    END AS content) ca1
                    CROSS apply (SELECT Count(c2.content) AS childcontent
                    FROM   comments c2
                    WHERE  c2.parentcommentid = c.id
                    AND c2.isdeleted = 0
                    AND c2.approved = 1) ca2
                    WHERE  c.discussionid = @discussionId
                    AND c.parentcommentid IS NULL
                    ORDER  BY c.pinned DESC, c.createddate; ",
                new { discussionId = discussion.Id });

            var Page = CalculatePage(pageCount);

            int pageNumber = 0;

            foreach (var item in Page) {
                if (item.Value > 0) {

                    var comments = _connection.Query(
                        @"SELECT 
                        IIF(c2.IsDeleted = 1 OR c2.Approved = 0, NULL, c2.Content) AS Content, 
                        (SELECT string_agg(c1.Content, ', ')
                        FROM Comments c1
                        WHERE c1.ParentCommentId = c2.Id
                        AND c1.IsDeleted = 0 AND c1.Approved = 1) ChildContent
                        FROM Comments c2
                        WHERE c2.DiscussionId = @discussionId
                        AND c2.ParentCommentId IS NULL
                        ORDER BY c2.Pinned DESC, c2.CreatedDate
                        OFFSET @skip ROWS
                        FETCH NEXT @take ROWS ONLY;",
                    new { discussionId = discussion.Id, skip = item.Key, take = item.Value },
                    commandTimeout: 120);

                    var content =
                        string.Join(" ",
                        comments.Select(c =>
                            string.Join(" ", string.Concat(c.Content, " ", c.ChildContent))));

                    if (pageNumber == 0) {
                        content = string.Concat(discussion.Content, " ", content);
                    }

                    // Remove HTML and new lines from textContent
                    content = MarkupRemovalPattern.Replace(content, " ");

                    string url = string.Format(
                        "{0}/forums/discuss/{1}-{2}", baseUrl, discussion.SluggedTitle, discussion.Id);

                    string title = discussion.Title;

                    if (pageNumber > 0) {
                        url += "?page=" + pageNumber;
                        title += " | Page " + (pageNumber + 1);
                    }

                    using (SearchServiceClient client = new SearchServiceClient()) {
                        client.Post(
                            "/index",
                            new PatientSearchIndexRequest {
                                ApplicationId = SearchServiceAppId,
                                Content = content,
                                ContentId = discussion.Id + "-" + pageNumber,
                                Title = title,
                                Url = url,
                                Keywords = string.Empty,
                                SecondaryTitles = string.Empty,
                                Type = "ForumDiscussion"
                            },
                            ex =>
                            Logger.Warning(ex,
                                string.Format("Unable to index discussion {0}, page {1}: {2}",
                                discussion.Id,
                                pageNumber,
                                ex.Message))
                        );
                    }

                    pageNumber++;
                }
            }
        }
        catch (Exception ex) {
            Logger.Warning("Failed to index discussion {0} ({1})" + discussion.Id, ex.Message);
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.