public IHttpActionResult GetPost(int id) { try { var postService = new PostService(); var votesService = new VoteService(); var commentaryService = new CommentaryService(); var post = postService.GetPostById(new GetPostByIdRequest() { Id = id }).Post; if (post == null) { return(NotFound()); } if (post.NullDate.HasValue || post.IsDraft) { return(NotFound()); } var result = GenerateGetPostModel(post); return(Ok(result)); } catch (Exception) { return(BadRequest()); } }
public IHttpActionResult GetPosts() { try { var postService = new PostService(); var votesService = new VoteService(); var commentaryService = new CommentaryService(); var posts = postService.SearchPosts(new SearchPostsRequest()).Posts; var result = new List <GetPostModel>(); if (posts.Any()) { foreach (var post in posts) { var postResult = GenerateGetPostModel(post); result.Add(postResult); } return(Ok(result)); } return(NotFound()); } catch (Exception) { return(BadRequest()); } }
private GetCommentsModel GenerateGetCommentsModel(int postId) { var commentaryService = new CommentaryService(); var comments = commentaryService.SearchCommentsByIdPost(new SearchCommentsByIdPostRequest() { IdPost = postId }).Comments; var commentsResult = new GetCommentsModel(); commentsResult.Comments = new List <CommentaryModel>(); foreach (var commentary in comments) { if (!commentary.IdUpperComment.HasValue) { var commentaryToAdd = TheModelFactory.CreateCommentaryModel(commentary); commentaryToAdd.Answers = new List <AnswerModel>(); foreach (var answer in comments) { if (answer.IdUpperComment.HasValue) { if (answer.IdUpperComment == commentary.Id) { var answerToAdd = TheModelFactory.CreateAnswerModel(answer); commentaryToAdd.Answers.Add(answerToAdd); } } } commentsResult.Comments.Add(commentaryToAdd); } } return(commentsResult); }
public ActionResult Details(int id) { var commentaryService = new CommentaryService(); var complaintService = new ComplaintService(); var postService = new PostService(); var userService = new UserService(); var votesService = new VoteService(); var post = postService.GetPostById(new GetPostByIdRequest() { Id = id }).Post; // Se obtienen los comentarios del post y se los separa en comentarios padre y respuestas. var allComments = commentaryService.SearchCommentsByIdPost(new SearchCommentsByIdPostRequest() { IdPost = id }).Comments.Where(x => !x.NullDate.HasValue).ToList(); ViewBag.Comments = allComments.Where(x => !x.IdUpperComment.HasValue).ToList(); ViewBag.Replies = allComments.Where(x => x.IdUpperComment.HasValue).ToList(); List <Complaint> userComplaints = new List <Complaint>(); if (User.Identity.IsAuthenticated && !User.IsInRole("Admin")) { // Se obtienen las denuncias/quejas realizadas por el usuario. userComplaints = complaintService.SearchComplaintsByUserId(new SearchComplaintsByUserIdRequest { AspNetUserId = User.Identity.GetUserId() }).Complaints; var user = userService.GetUserByAccountId(new GetUserByAccountIdRequest { AccountId = User.Identity.GetUserId() }).User; // Se obtienen los votos del post y del usuario var postVotes = votesService.GetVotesCountByPostId(new GetVotesCountByPostIdRequest { PostId = post.Id }); var userVotes = votesService.GetVoteByUserAndPostId(new GetVoteByUserAndPostIdRequest { PostId = post.Id, UserId = user.Id }); ViewBag.GoodVotes = postVotes.GoodVotes; ViewBag.BadVotes = postVotes.BadVotes; ViewBag.UserGoodVote = userVotes.Good ? "true" : "false"; ViewBag.UserBadVote = userVotes.Bad ? "true" : "false"; } if (User.Identity.IsAuthenticated && User.IsInRole("Admin")) { ViewBag.Complaints = complaintService.SearchComplaintsByPostId(new SearchComplaintsByPostIdRequest { PostId = post.Id }).Complaints; } ViewBag.UserComplaints = userComplaints; return(View(post)); }
public async Task GivenCommentaryService_WhenRemoveCommentary_ThenShouldCallRemoveMethodAndSaveMethod() { CommentaryService CommentaryService = new CommentaryService(_mockUnitofwork, _mapper); await CommentaryService.RemoveCommentary(new Guid()); _mockCommentaryRepository.Received(1).RemoveCommentary(Commentary); await _mockUnitofwork.Received(1).SaveAsync(); }
public async Task GivenCommentaryService_WhenGetCommentaryByID_ThenShouldGetThisCommentary() { CommentaryDto accpected = _mapper.Map <CommentaryDto>(Commentary); CommentaryService CommentaryService = new CommentaryService(_mockUnitofwork, _mapper); CommentaryDto CommentaryDto = await CommentaryService.GetCommentaryById(new Guid()); accpected.Should().BeEquivalentTo(CommentaryDto); }
public async Task GivenCommentaryService_WhenWeAddCommentary_ThenShouldCallAddCommentaryAndSaveMethods() { CommentaryService CommentaryService = new CommentaryService(_mockUnitofwork, _mapper); await CommentaryService.AddCommentary(new CommentaryDto { UserId = "1", VideoId = new Guid() }); _mockCommentaryRepository.Received(1).AddCommentary(Arg.Any <Commentary>()); await _mockUnitofwork.Received(1).SaveAsync(); }
public async Task GivenCommentaryService_WhenGetCommentaiesByVideoId_ThenShouldGetThisCommentaries() { IEnumerable <Commentary> CommentariesAccpected = new Commentary[] { Commentary }; IEnumerable <CommentaryDto> accpected = _mapper.Map <IEnumerable <CommentaryDto> >(CommentariesAccpected); CommentaryService CommentaryService = new CommentaryService(_mockUnitofwork, _mapper); IEnumerable <CommentaryDto> Commentaries = await CommentaryService.GetCommentariesByVideoId(Guid.NewGuid()); accpected.Should().BeEquivalentTo(Commentaries); }
public ActionResult CommentaryComplaints(int id) { var commentaryService = new CommentaryService(); var complaintService = new ComplaintService(); ViewBag.Comment = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest { Id = id }).Commentary; ViewBag.CommentaryComplaints = complaintService.SearchComplaintsByCommentaryId(new SearchComplaintsByCommentaryIdRequest { CommentaryId = id }).Complaints; return(View()); }
public JsonResult AddComment(string text, int post, int?idUpperComment) { if ((text.Length > 0) && (text.Length < 251)) { var commentaryService = new CommentaryService(); var userService = new UserService(); var commentaryId = 0; var idUser = userService.GetUserByAccountId(new GetUserByAccountIdRequest() { AccountId = User.Identity.GetUserId() }).User.Id; if (ModelState.IsValid) { var request = new CreateCommentaryRequest { CommentaryText = text, IdPost = post, IdUser = idUser, IdUpperComment = (idUpperComment == 0) ? null : idUpperComment }; var result = commentaryService.CreateCommentary(request); commentaryId = result.CommentaryId; } var htmlComment = ""; var userName = User.Identity.GetUserName(); var date = DateTime.Now.ToString("dd/MM/yyyy"); if (idUpperComment == 0) { //Es un comentario nuevo. htmlComment = "<article class=\"row\" id=\"" + commentaryId + "\"><div class=\"col-md-10 col-sm-10\"><div class=\"panel panel-default arrow left\"><div class=\"panel-body\"><header class=\"text-left\"><div class=\"comment-user\"><i class=\"fa fa-user\"></i> <a href=\"/Profile/Details/" + idUser + "\">" + userName + "</a> <time class=\"comment-date\"><i class=\"fa fa-clock-o\"></i> " + date + "</time></div></header><div class=\"comment-post\"><p style=\"margin-bottom: 5px;\">" + text + "<span> </span><small><a class=\"deleteCommentary text-danger\" href=\"#\" id=\"" + commentaryId + "\">Eliminar comentario</a></small></p></div><p class=\"text-right\"><a id=\"replyCommentary\" href=\"#\" class=\"btn btn-default btn-sm\" replyTo=\"" + userName + "\" commentary-id=\"" + commentaryId + "\"><i class=\"fa fa-reply\"></i> Responder</a></p></div></div></div></article><div id=\"newComment" + commentaryId + "\" class=\"media\"></div>"; } else { //Es una respuesta. var upperCommentWriterUserName = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest() { Id = idUpperComment.Value }).Commentary.WriterUserName; htmlComment = "<article class=\"row\" id=\"" + commentaryId + "\"> <div class=\"col-md-9 col-sm-9 col-md-offset-1 col-sm-offset-0 hidden-xs\"><div class=\"panel panel-default arrow left\"><div class=\"panel-heading right\">En respuesta a <i>"+ upperCommentWriterUserName + "</i></div><div class=\"panel-body\"><header class=\"text-left\"><div class=\"comment-user\"><i class=\"fa fa-user\"></i> <a href=\"/Profile/Details/" + idUser + "\">" + userName + "</a> <time class=\"comment-date\"><i class=\"fa fa-clock-o\"></i> " + date + "</time></div></header><div class=\"comment-post\"><p>" + text + "<span> </span><small><a class=\"deleteCommentary text-danger\" href=\"#\" id=\"" + commentaryId + "\">Eliminar comentario</a></small></p></div></div></div></div></article>"; } return(Json(new { success = htmlComment })); } return(Json(false, JsonRequestBehavior.AllowGet)); }
public IHttpActionResult DeleteCommentary(int id) { var userService = new UserService(); var commentaryService = new CommentaryService(); var commentary = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest() { Id = id }).Commentary; var currentUserId = userService.GetUserByAccountId(new GetUserByAccountIdRequest() { AccountId = User.Identity.GetUserId() }).User.Id; if (commentary == null) { return(NotFound()); } if (commentary.NullDate.HasValue) { return(NotFound()); } if (currentUserId != commentary.IdUser) { return(Unauthorized()); } try { var result = commentaryService.DeleteCommentary(new DeleteCommentaryRequest() { Id = id }); return(Ok()); } catch (Exception) { return(BadRequest()); } }
public JsonResult DeleteComment(int idCommentary) { var commentaryService = new CommentaryService(); var commentary = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest() { Id = idCommentary }).Commentary; if ((commentary != null) && (commentary.WriterUserName == User.Identity.Name)) { var result = commentaryService.DeleteCommentary(new DeleteCommentaryRequest() { Id = idCommentary }); return(Json(true, JsonRequestBehavior.AllowGet)); } return(Json(false, JsonRequestBehavior.AllowGet)); }
public IHttpActionResult GetCommentary(int id) { try { var commentaryService = new CommentaryService(); var comments = commentaryService.SearchCommentsByIdPost(new SearchCommentsByIdPostRequest() { IdPost = id }).Comments; if (!comments.Any()) { return(NotFound()); } var result = GenerateGetCommentsModel(id); return(Ok(result)); } catch (Exception) { return(BadRequest()); } }
public JsonResult CommentaryComplaint(CommentaryComplaintViewModel model) { var commentaryService = new CommentaryService(); var complaintService = new ComplaintService(); var userService = new UserService(); 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.IdComment == model.CommentaryId)) { return(Json(new { success = false, Message = "Ya se ha registrado una denuncia para esta cuenta en este comentario." }, JsonRequestBehavior.AllowGet)); } var commentary = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest { Id = model.CommentaryId }).Commentary; if (commentary.IdUser == user.Id) { return(Json(new { success = false, Message = "No puede denunciar su propio comentario." }, JsonRequestBehavior.AllowGet)); } var complaintResult = complaintService.CreateCommentaryComplaint(new CreateCommentaryComplaintRequest { CommentaryId = model.CommentaryId, UserId = user.Id, Commentary = model.Commentary }); if ((complaintResult.CommentaryComplaintsCount % (int)DividersToDeleteByComplaint.PostAndCommentaryDeletedDivider) == 0) { // Se da de baja el comentario. var deletePostResult = commentaryService.DeleteCommentary(new DeleteCommentaryRequest { Id = complaintResult.CommentaryId, IsComplaintOrVoteDifference = true }); var complaints = complaintService.SearchComplaintsByCommentaryId(new SearchComplaintsByCommentaryIdRequest { CommentaryId = commentary.Id }).Complaints.OrderByDescending(x => x.Id).Take(3).ToList(); // Se notifica la baja del comentario via correo electrónico al escritor. SendCommentaryDeletedEmailToWriter(commentary, complaints); // Se verifica y de ser necesario, se suspende temporalmente la cuenta del usuario. var verifyResult = userService.VerifyAndUpdateUserStateByComments(new VerifyAndUpdateUserStateByCommentsRequest { UserId = commentary.IdUser }); if (verifyResult.UserSuspended) { var reason = "La cantidad de comentarios dados de baja por denuncias ha alcanzando el número estipulado para suspender temporalmente su cuenta."; SendAccountBlockedToWriter(commentary.IdUser, 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>, el comentario será dado 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 PostCommentary(CreateCommentaryModel model) { if (ModelState.IsValid) { try { var commentaryService = new CommentaryService(); var postService = new PostService(); var userService = new UserService(); var currentUserId = userService.GetUserByAccountId(new GetUserByAccountIdRequest() { AccountId = User.Identity.GetUserId() }).User.Id; // Controla que exista la publicación asociada. var post = postService.GetPostById(new GetPostByIdRequest() { Id = model.IdPost }).Post; if (post == null) { return(BadRequest("Invalid post")); } if (post.NullDate.HasValue) { return(BadRequest("Invalid post")); } if (model.IdUpperComment.HasValue) { // Controla que exista el comentario padre. var upperCommentary = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest() { Id = model.IdUpperComment.Value }).Commentary; if (upperCommentary == null) { return(BadRequest("Invalid upper commentary")); } if (upperCommentary.NullDate.HasValue) { return(BadRequest("Invalid upper commentary")); } if (upperCommentary.IdUpperComment.HasValue) { return(BadRequest("You can't respond an answer")); } } var request = new CreateCommentaryRequest() { CommentaryText = model.TextComment, IdPost = model.IdPost, IdUpperComment = model.IdUpperComment, IdUser = currentUserId }; var result = commentaryService.CreateCommentary(request); return(Ok()); } catch (Exception) { return(BadRequest()); } } return(BadRequest(ModelState)); }
public IHttpActionResult PostComplaint(int id, CommentaryComplaintModel model) { try { if (model == null) { return(BadRequest()); } var userService = new UserService(); var complaintService = new ComplaintService(); var commentaryService = new CommentaryService(); 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.IdComment == id)) { return(BadRequest("Complaint has been registered before")); } var commentary = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest { Id = id }).Commentary; if (commentary.IdUser == user.Id) { return(BadRequest("You can not register a complaint to your own comment")); } if (commentary.NullDate.HasValue) { return(NotFound()); } var complaintResult = complaintService.CreateCommentaryComplaint(new CreateCommentaryComplaintRequest { CommentaryId = id, UserId = user.Id, Commentary = model.Commentary }); if ((complaintResult.CommentaryComplaintsCount % (int)DividersToDeleteByComplaint.PostAndCommentaryDeletedDivider) == 0) { // Se da de baja el comentario. var deletePostResult = commentaryService.DeleteCommentary(new DeleteCommentaryRequest { Id = complaintResult.CommentaryId, IsComplaintOrVoteDifference = true }); var complaints = complaintService.SearchComplaintsByCommentaryId(new SearchComplaintsByCommentaryIdRequest { CommentaryId = commentary.Id }).Complaints.OrderByDescending(x => x.Id).Take(3).ToList(); // Se notifica la baja del comentario via correo electrónico al escritor. SendCommentaryDeletedEmailToWriter(commentary, complaints); // Se verifica y de ser necesario, se suspende temporalmente la cuenta del usuario. var verifyResult = userService.VerifyAndUpdateUserStateByComments(new VerifyAndUpdateUserStateByCommentsRequest { UserId = commentary.IdUser }); if (verifyResult.UserSuspended) { var reason = "La cantidad de comentarios dados de baja por denuncias ha alcanzando el número estipulado para suspender temporalmente su cuenta."; SendAccountBlockedToWriter(commentary.IdUser, verifyResult.ActivationDate, reason); } ; } return(Ok()); } else { return(BadRequest()); } } catch (Exception) { return(BadRequest()); } }