public async Task <ActionResult> CommentEdit_Partial(EditModel model)
        {
            await model.UpdateDataAsync();

            using (BlogCommentDataProvider dataProvider = new BlogCommentDataProvider(model.EntryIdentity)) {
                BlogComment data = await dataProvider.GetItemAsync(model.Identity);

                if (data == null)
                {
                    throw new Error(this.__ResStr("alreadyDeleted", "The comment entry with id {0} has been removed and can no longer be updated", model.Identity));
                }
                if (!ModelState.IsValid)
                {
                    return(PartialView(model));
                }

                // save updated item
                data = model.GetData(data); // merge new data into original
                await model.SetData(data);  // and all the data back into model for final display

                switch (await dataProvider.UpdateItemAsync(data))
                {
                default:
                case UpdateStatusEnum.RecordDeleted:
                    throw new Error(this.__ResStr("alreadyDeleted", "The comment entry with id {0} has been removed and can no longer be updated", model.Identity));

                case UpdateStatusEnum.NewKeyExists:
                    throw new Error(this.__ResStr("alreadyExists", "A comment with id {0} already exists.", model.Identity));

                case UpdateStatusEnum.OK:
                    break;
                }
                return(FormProcessed(model, this.__ResStr("okSaved", "Comment saved")));
            }
        }
        public async Task <ActionResult> CommentAdd_Partial(AddModel model)
        {
            await model.UpdateDataAsync();

            using (BlogEntryDataProvider entryDP = new BlogEntryDataProvider()) {
                BlogEntry blogEntry = await entryDP.GetItemAsync(model.EntryIdentity);

                if (blogEntry == null)
                {
                    throw new Error(this.__ResStr("notFound", "Blog entry with id {0} not found."), model.EntryIdentity);
                }
                if (!blogEntry.OpenForComments)
                {
                    throw new InternalError("Can't add comments to this blog entry");
                }
                if (!ModelState.IsValid)
                {
                    return(PartialView(model));
                }
                using (BlogCommentDataProvider blogCommentDP = new BlogCommentDataProvider(model.EntryIdentity)) {
                    BlogComment blogComment = model.GetData();
                    if (!await blogCommentDP.AddItemAsync(blogComment))
                    {
                        throw new Error(this.__ResStr("alreadyExists", "An error occurred adding this new comment"));
                    }

                    // send notification email
                    BlogConfigData config = await BlogConfigDataProvider.GetConfigAsync();

                    if (config.NotifyNewComment)
                    {
                        SendEmail sendEmail = new SendEmail();
                        object    parms     = new {
                            Description = !blogComment.Approved ? this.__ResStr("needApproval", "This comment requires your approval.") : this.__ResStr("autoApproval", "This comment has been automatically approved."),
                            Category    = (await blogEntry.GetCategoryAsync()).ToString(),
                            Title       = blogEntry.Title.ToString(),
                            Url         = Manager.CurrentSite.MakeUrl(await BlogConfigData.GetEntryCanonicalNameAsync(blogEntry.Identity)),
                            Comment     = Utility.HtmlDecode(model.Comment),
                            UserName    = model.Name,
                            UserEmail   = model.Email,
                            UserWebsite = model.Website,
                        };
                        string subject = this.__ResStr("newComment", "New Blog Comment ({0} - {1})", blogEntry.Title.ToString(), Manager.CurrentSite.SiteDomain);
                        await sendEmail.PrepareEmailMessageAsync(config.NotifyEmail, subject, await sendEmail.GetEmailFileAsync(Package.GetCurrentPackage(this), "New Comment.txt"), Parameters : parms);

                        await sendEmail.SendAsync(true);
                    }

                    if (!blogComment.Approved)
                    {
                        return(FormProcessed(model, this.__ResStr("okSavedReview", "New comment saved - It will be reviewed before becoming publicly viewable"), OnClose: OnCloseEnum.ReloadPage, OnPopupClose: OnPopupCloseEnum.ReloadParentPage));
                    }
                    else
                    {
                        return(FormProcessed(model, this.__ResStr("okSaved", "New comment added"), OnClose: OnCloseEnum.ReloadPage, OnPopupClose: OnPopupCloseEnum.ReloadParentPage));
                    }
                }
            }
        }
        public async Task <ActionResult> Remove(int blogEntry, int comment)
        {
            using (BlogCommentDataProvider dataProvider = new BlogCommentDataProvider(blogEntry)) {
                BlogComment cmt = await dataProvider.GetItemAsync(comment);

                if (cmt == null)
                {
                    throw new InternalError("Can't find comment entry {0}", comment);
                }
                if (!await dataProvider.RemoveItemAsync(comment))
                {
                    throw new InternalError("Can't remove comment entry");
                }
                return(Reload(null, Reload: ReloadEnum.Page));
            }
        }
        public async Task <ActionResult> CommentEdit(int blogEntry, int comment)
        {
            using (BlogCommentDataProvider dataProvider = new BlogCommentDataProvider(blogEntry)) {
                EditModel model  = new EditModel {
                };
                BlogComment data = await dataProvider.GetItemAsync(comment);

                if (data == null)
                {
                    throw new Error(this.__ResStr("notFound", "Comment entry with id {0} not found."), comment);
                }
                await model.SetData(data);

                Module.Title = data.Title;
                return(View(model));
            }
        }
        public async Task <ActionResult> Approve(int blogEntry, int comment)
        {
            using (BlogCommentDataProvider dataProvider = new BlogCommentDataProvider(blogEntry)) {
                BlogComment cmt = await dataProvider.GetItemAsync(comment);

                if (cmt == null)
                {
                    throw new InternalError("Can't find comment entry {0}", comment);
                }
                cmt.Approved = true;
                UpdateStatusEnum status = await dataProvider.UpdateItemAsync(cmt);

                if (status != UpdateStatusEnum.OK)
                {
                    throw new InternalError("Can't update comment entry - {0}", status);
                }
                return(Reload(null, Reload: ReloadEnum.Page));
            }
        }
        public async Task <ActionResult> CommentsDisplay(int?blogEntry)
        {
            int            entryNum = blogEntry ?? 0;
            BlogConfigData config   = await BlogConfigDataProvider.GetConfigAsync();

            DisplayModel model = new DisplayModel()
            {
            };

            CommentEditModule editMod = new CommentEditModule();

            using (BlogEntryDataProvider entryDP = new BlogEntryDataProvider()) {
                BlogEntry entry = null;
                if (entryNum != 0)
                {
                    entry = await entryDP.GetItemAsync(entryNum);
                }
                if (entry == null)
                {
                    return(new EmptyResult());
                }
                model.OpenForComments = entry.OpenForComments;
            }
            using (BlogCommentDataProvider commentDP = new BlogCommentDataProvider(entryNum)) {
                DataProviderGetRecords <BlogComment> comments = await commentDP.GetItemsAsync(0, 0, null, null);

                if (!model.OpenForComments && comments.Total == 0)
                {
                    return(new EmptyResult());
                }

                model.ShowGravatars = config.ShowGravatar;
                model.CanApprove    = await Resource.ResourceAccess.IsResourceAuthorizedAsync(Info.Resource_AllowManageComments);

                model.CanRemove = await Resource.ResourceAccess.IsResourceAuthorizedAsync(Info.Resource_AllowManageComments);

                List <CommentData> list = new List <CommentData>();
                foreach (BlogComment d in comments.Data)
                {
                    ModuleAction editAction = await editMod.GetAction_EditAsync(Module.EditUrl, d.EntryIdentity, d.Identity);

                    ModuleAction approveAction = await Module.GetAction_ApproveAsync(d.CategoryIdentity, d.Identity);

                    ModuleAction removeAction = await Module.GetAction_RemoveAsync(d.CategoryIdentity, d.Identity);

                    if (model.CanApprove || model.CanRemove)
                    {
                        list.Add(new CommentData(d, editAction, approveAction, removeAction));
                    }
                    else
                    {
                        if (!d.Deleted && d.Approved)
                        {
                            list.Add(new CommentData(d, editAction, approveAction, removeAction));
                        }
                    }
                }
                model.Comments = list;

                int pending = (from d in comments.Data where !d.Deleted && !d.Approved select d).Count();
                model.PendingApproval = pending;
                int commentsTotal = (from c in comments.Data where !c.Deleted select c).Count();

                // set a module title
                string title;
                if (commentsTotal > 0 && pending > 0)
                {
                    if (commentsTotal > 1)
                    {
                        if (pending > 1)
                        {
                            title = this.__ResStr("commentsPs", "{0} Comments - {1} Comments Pending Approval", commentsTotal, pending);
                        }
                        else
                        {
                            title = this.__ResStr("commentsP", "{0} Comments - 1 Comment Pending Approval", commentsTotal);
                        }
                    }
                    else
                    {
                        title = this.__ResStr("commentP", "1 Comment Pending Approval");
                    }
                }
                else
                {
                    if (commentsTotal > 1)
                    {
                        title = this.__ResStr("comments", "{0} Comments", commentsTotal);
                    }
                    else if (commentsTotal == 1)
                    {
                        title = this.__ResStr("comment1", "1 Comment");
                    }
                    else
                    {
                        title = this.__ResStr("comment0", "No Comments");
                    }
                }
                Module.Title = title;

                return(View(model));
            }
        }