Beispiel #1
0
		public ActionResult CommentsAdmin(int id, CommentCommandOptions command, int[] commentIds)
		{
			if (commentIds == null || commentIds.Length == 0)
				ModelState.AddModelError("CommentIdsAreEmpty", "Not comments was selected.");

			var post = RavenSession.Load<Post>(id);
			if (post == null)
				return HttpNotFound();

			if (ModelState.IsValid == false)
			{
				if (Request.IsAjaxRequest())
					return Json(new {Success = false, message = ModelState.FirstErrorMessage()});

				return Details(id);
			}

			var comments = RavenSession.Load<PostComments>(post.CommentsId);
			switch (command)
			{
				case CommentCommandOptions.Delete:
					comments.Comments.RemoveAll(c => commentIds.Contains(c.Id));
					comments.Spam.RemoveAll(c => commentIds.Contains(c.Id));
					break;

				case CommentCommandOptions.MarkSpam:
					var spams = comments.Comments.Concat(comments.Spam)
						.Where(c => commentIds.Contains(c.Id))
						.ToArray();

					comments.Comments.RemoveAll(spams.Contains);
					comments.Spam.RemoveAll(spams.Contains);
					foreach (var comment in spams)
					{
						AkismetService.MarkSpam(comment);
					}
					break;

				case CommentCommandOptions.MarkHam:
					var ham = comments.Spam
						.Where(c => commentIds.Contains(c.Id))
						.ToArray();

					comments.Spam.RemoveAll(ham.Contains);
					comments.Comments.AddRange(ham);

					comments.Comments
						.Where(c => c.IsSpam)
						.ForEach(comment =>
						         	{
						         		comment.IsSpam = false;
						         		AkismetService.MarkHam(comment);
						         		ResetNumberOfSpamComments(comment);
						         	});
					break;
				default:
					throw new InvalidOperationException(command + " command is not recognized.");
			}

			post.CommentsCount = comments.Comments.Count;

			if (Request.IsAjaxRequest())
			{
				return Json(new {Success = true});
			}
			return RedirectToAction("Details", new {id});
		}
        public ActionResult CommentsAdmin(int id, CommentCommandOptions command, int[] commentIds)
        {
            if (commentIds == null || commentIds.Length == 0)
            {
                ModelState.AddModelError("CommentIdsAreEmpty", "Not comments was selected.");
            }

            var post = RavenSession.Load <Post>(id);

            if (post == null)
            {
                return(HttpNotFound());
            }

            if (ModelState.IsValid == false)
            {
                if (Request.IsAjaxRequest())
                {
                    return(Json(new { Success = false, message = ModelState.FirstErrorMessage() }));
                }

                return(Details(id));
            }

            var comments = RavenSession.Load <PostComments>(post.CommentsId);

            switch (command)
            {
            case CommentCommandOptions.Delete:
                comments.Comments.RemoveAll(c => commentIds.Contains(c.Id));
                comments.Spam.RemoveAll(c => commentIds.Contains(c.Id));
                break;

            case CommentCommandOptions.MarkSpam:
                var spams = comments.Comments.Concat(comments.Spam)
                            .Where(c => commentIds.Contains(c.Id))
                            .ToArray();

                comments.Comments.RemoveAll(spams.Contains);
                comments.Spam.RemoveAll(spams.Contains);
                foreach (var comment in spams)
                {
                    AkismetService.MarkSpam(comment);
                }
                break;

            case CommentCommandOptions.MarkHam:
                var ham = comments.Spam
                          .Where(c => commentIds.Contains(c.Id))
                          .ToArray();

                comments.Spam.RemoveAll(ham.Contains);
                comments.Comments.AddRange(ham);

                comments.Comments
                .Where(c => c.IsSpam)
                .ForEach(comment =>
                {
                    comment.IsSpam = false;
                    AkismetService.MarkHam(comment);
                    ResetNumberOfSpamComments(comment);
                });
                break;

            default:
                throw new InvalidOperationException(command + " command is not recognized.");
            }

            post.CommentsCount = comments.Comments.Count;

            if (Request.IsAjaxRequest())
            {
                return(Json(new { Success = true }));
            }
            return(RedirectToAction("Details", new { id }));
        }
        public ActionResult CommentsAdmin(int id, CommentCommandOptions command, int[] commentIds)
        {
            if (commentIds.Length < 1)
            {
                ModelState.AddModelError("CommentIdsAreEmpty", "Not comments was selected.");
            }

            var post = Session.Load <Post>(id);

            if (post == null)
            {
                return(HttpNotFound());
            }

            var slug = SlugConverter.TitleToSlag(post.Title);

            if (ModelState.IsValid == false)
            {
                if (Request.IsAjaxRequest())
                {
                    return(Json(new { Success = false, message = ModelState.GetFirstErrorMessage() }));
                }

                return(Details(id, slug));
            }

            var comments      = Session.Load <PostComments>(id);
            var requestValues = Request.MapTo <RequestValues>();

            switch (command)
            {
            case CommentCommandOptions.Delete:
                comments.Comments.RemoveAll(c => commentIds.Contains(c.Id));
                comments.Spam.RemoveAll(c => commentIds.Contains(c.Id));
                break;

            case CommentCommandOptions.MarkSpam:
                var spams = comments.Comments
                            .Where(c => commentIds.Contains(c.Id))
                            .ToArray();

                comments.Comments.RemoveAll(spams.Contains);
                comments.Spam.RemoveAll(spams.Contains);
                foreach (var comment in spams)
                {
                    new AskimetService(Session).MarkSpam(comment);
                }
                break;

            case CommentCommandOptions.MarkHam:
                var ham = comments.Spam
                          .Where(c => commentIds.Contains(c.Id))
                          .ToArray();

                comments.Spam.RemoveAll(ham.Contains);
                foreach (var comment in ham)
                {
                    comment.IsSpam = false;
                    new AskimetService(Session).MarkHam(comment);
                }
                comments.Comments.AddRange(ham);
                break;

            default:
                throw new InvalidOperationException(command + " command is not recognized.");
            }

            if (Request.IsAjaxRequest())
            {
                return(Json(new { Success = true }));
            }
            return(RedirectToAction("Details", new { id, slug }));
        }
        public ActionResult CommentsAdmin(int id, CommentCommandOptions command, int[] commentIds)
        {
            if (commentIds.Length < 1)
                ModelState.AddModelError("CommentIdsAreEmpty", "Not comments was selected.");

            var post = Session.Load<Post>(id);
            if (post == null)
                return HttpNotFound();

            var slug =  SlugConverter.TitleToSlag(post.Title);

            if (ModelState.IsValid == false)
            {
                if (Request.IsAjaxRequest())
                    return Json(new {Success = false, message = ModelState.GetFirstErrorMessage()});

                return Details(id, slug);
            }

            var comments = Session.Load<PostComments>(id);
            switch (command)
            {
                case CommentCommandOptions.Delete:
                    comments.Comments.RemoveAll(c => commentIds.Contains(c.Id));
                    comments.Spam.RemoveAll(c => commentIds.Contains(c.Id));
                    break;

                case CommentCommandOptions.MarkSpam:
                    var spams = comments.Comments.Concat(comments.Spam)
                        .Where(c => commentIds.Contains(c.Id))
                        .ToArray();

                    comments.Comments.RemoveAll(spams.Contains);
                    comments.Spam.RemoveAll(spams.Contains);
                    foreach (var comment in spams)
                    {
                        new AskimetService(Session).MarkSpam(comment);
                    }
                    break;

                case CommentCommandOptions.MarkHam:
                    var ham = comments.Spam
                        .Where(c => commentIds.Contains(c.Id))
                        .ToArray();

                    comments.Spam.RemoveAll(ham.Contains);
                    foreach (var comment in ham)
                    {
                        comment.IsSpam = false;
                        new AskimetService(Session).MarkHam(comment);
                    }
                    comments.Comments.AddRange(ham);
                    break;
                default:
                    throw new InvalidOperationException(command + " command is not recognized.");
            }

            post.CommentsCount = comments.Comments.Count;

            if (Request.IsAjaxRequest())
            {
                return Json(new {Success = true});
            }
            return RedirectToAction("Details", new { id, slug });
        }