private bool Validate(CommentBanDTO commentDTO, ICollection <string> errors) { bool isValid = true; if (String.IsNullOrEmpty(commentDTO.ModeratorLogin)) { isValid = false; errors.Add("Moderator must be specified"); } return(isValid); }
public ActionResult Ban(CommentBanViewModel model) { bool succeeded = false; DataServiceMessage <IEnumerable <BanReasonListDTO> > dataServiceMessage = banReasonService.GetEnabled(); model.BanReasons = GetSelectListItems(dataServiceMessage.Data); if (ModelState.IsValid) { model.Id = HttpUtility.UrlDecode(model.Id); CommentBanDTO commentDTO = Mapper.Map <CommentBanViewModel, CommentBanDTO>(model); ServiceMessage serviceMessage = commentService.Ban(commentDTO); if (!serviceMessage.Succeeded) { AddModelErrors(serviceMessage.Errors); } succeeded = serviceMessage.Succeeded; } return(JsonOnFormPost(succeeded, "~/Views/Comment/Ban.cshtml", model)); }
public ServiceMessage Ban(CommentBanDTO commentDTO) { int commentId; string decryptedCommentId = encryptor.Decrypt(commentDTO.Id); int banReasonId; string decryptedBanReasonId = encryptor.Decrypt(commentDTO.BanReasonId); List <string> errors = new List <string>(); bool succeeded = Validate(commentDTO, errors); if (!Int32.TryParse(decryptedCommentId, out commentId)) { succeeded = false; errors.Add("Comment was not found"); } if (!Int32.TryParse(decryptedBanReasonId, out banReasonId)) { succeeded = false; errors.Add("Ban reason was not found"); } if (succeeded && (succeeded = Validate(commentDTO, errors))) { try { ModeratorEntity moderatorEntity = unitOfWork.Moderators.GetByLogin(commentDTO.ModeratorLogin); if (moderatorEntity == null) { succeeded = false; errors.Add("Moderator with such login was not found"); } BanReasonEntity banReasonEntity = unitOfWork.Bans.Get(banReasonId); if (moderatorEntity == null) { succeeded = false; errors.Add("Moderator with such login was not found"); } CommentEntity commentEntity = unitOfWork.Comments.Get(commentId); if (commentEntity == null) { succeeded = false; errors.Add("Comment was not found"); } if (succeeded) { commentEntity.BanReason = banReasonEntity; commentEntity.DateBanned = DateTime.Now; commentEntity.ModeratorWhoBanned = moderatorEntity; unitOfWork.Commit(); } } catch (Exception ex) { ExceptionMessageBuilder.FillErrors(ex, errors); succeeded = false; } } return(new ServiceMessage { Errors = errors, Succeeded = succeeded }); }