public JsonResult PostComplaint(PostComplaintViewModel model) { var complaintService = new ComplaintService(); var userService = new UserService(); var postService = new PostService(); try { var user = userService.GetUserByAccountId(new GetUserByAccountIdRequest { AccountId = User.Identity.GetUserId() }).User; if (user != null) { var userComplaints = complaintService.SearchComplaintsByUserId(new SearchComplaintsByUserIdRequest { UserId = user.Id }).Complaints; if (userComplaints.Any(x => x.IdPost == model.PostId)) { return(Json(new { success = false, Message = "Ya se ha registrado una denuncia para esta cuenta en esta publicación." }, JsonRequestBehavior.AllowGet)); } var post = postService.GetPostById(new GetPostByIdRequest { Id = model.PostId }).Post; if (post.IdWriter == user.Id) { return(Json(new { success = false, Message = "No puede denunciar su propia publicación." }, JsonRequestBehavior.AllowGet)); } var complaintResult = complaintService.CreatePostComplaint(new CreatePostComplaintRequest { PostId = model.PostId, UserId = user.Id, Commentary = model.Commentary }); if ((complaintResult.PostComplaintsCount % (int)DividersToDeleteByComplaint.PostAndCommentaryDeletedDivider) == 0) { // Se da de baja la publicación. var deletePostResult = postService.DeletePost(new DeletePostRequest { Id = complaintResult.PostId, IsComplaintOrVoteDifference = true }); var complaints = complaintService.SearchComplaintsByPostId(new SearchComplaintsByPostIdRequest { PostId = post.Id }).Complaints.OrderByDescending(x => x.Id).Take(3).ToList(); // Se notifica la baja del post via correo electrónico al escritor. SendPostDeletedEmailToWriter(post, complaints); // Se verifica y de ser necesario, se suspende temporalmente la cuenta del usuario. var verifyResult = userService.VerifyAndUpdateUserStateByPosts(new VerifyAndUpdateUserStateByPostsRequest { UserId = post.IdWriter }); if (verifyResult.UserSuspended) { var reason = "La cantidad de publicaciones dadas de baja por denuncias ha alcanzando el número estipulado para suspender temporalmente su cuenta."; SendAccountBlockedToWriter(post.IdWriter, verifyResult.ActivationDate, reason); } ; } return(Json(new { success = true, Message = "<div style='text-align:justify;'>Su denuncia ha sido registrada y será colocada junto a la de otros usuarios. Si se alcanza el límite establecido en nuestros <a href='/Home/TermsAndConditions' target='_blank'>Términos y Condiciones</a>, la publicación será dada de baja.<br><br>Gracias por contribuir con nuestra comunidad :)</div>" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, Message = "Ha ocurrido un error al procesar la solicitud." }, JsonRequestBehavior.AllowGet)); } } catch (Exception e) { return(Json(new { success = false, Message = "Ha ocurrido un error al procesar la solicitud." }, JsonRequestBehavior.AllowGet)); } }
public IHttpActionResult PostComplaint(int id, PostComplaintModel model) { try { if (model == null) { return(BadRequest()); } var userService = new UserService(); var complaintService = new ComplaintService(); var postService = new PostService(); var userAccountSuspended = userService.VerifyIfIsSuspendedAndUpdateUser(new VerifyIfIsSuspendedAndUpdateUserRequest { AspNetUserId = User.Identity.GetUserId() }).UserSuspended; if (userAccountSuspended) { return(BadRequest("User account suspended")); } var user = userService.GetUserByAccountId(new GetUserByAccountIdRequest { AccountId = User.Identity.GetUserId() }).User; if (user != null) { var userComplaints = complaintService.SearchComplaintsByUserId(new SearchComplaintsByUserIdRequest { UserId = user.Id }).Complaints; if (userComplaints.Any(x => x.IdPost == id)) { return(BadRequest("Complaint has been registered before")); } var post = postService.GetPostById(new GetPostByIdRequest { Id = id }).Post; if (post.IdWriter == user.Id) { return(BadRequest("You can not register a complaint to your own post")); } if (post.NullDate.HasValue || post.IsDraft) { return(NotFound()); } var complaintResult = complaintService.CreatePostComplaint(new CreatePostComplaintRequest { PostId = id, UserId = user.Id, Commentary = model.Commentary }); if ((complaintResult.PostComplaintsCount % (int)DividersToDeleteByComplaint.PostAndCommentaryDeletedDivider) == 0) { // Se da de baja la publicación. var deletePostResult = postService.DeletePost(new DeletePostRequest { Id = complaintResult.PostId, IsComplaintOrVoteDifference = true }); var complaints = complaintService.SearchComplaintsByPostId(new SearchComplaintsByPostIdRequest { PostId = post.Id }).Complaints.OrderByDescending(x => x.Id).Take(3).ToList(); // Se notifica la baja del post via correo electrónico al escritor. SendPostDeletedEmailToWriter(post, complaints); // Se verifica y de ser necesario, se suspende temporalmente la cuenta del usuario. var verifyResult = userService.VerifyAndUpdateUserStateByPosts(new VerifyAndUpdateUserStateByPostsRequest { UserId = post.IdWriter }); if (verifyResult.UserSuspended) { var reason = "La cantidad de publicaciones dadas de baja por denuncias ha alcanzando el número estipulado para suspender temporalmente su cuenta."; SendAccountBlockedToWriter(post.IdWriter, verifyResult.ActivationDate, reason); } ; } return(Ok()); } else { return(BadRequest()); } } catch (Exception) { return(BadRequest()); } }