예제 #1
0
        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);
        }
예제 #2
0
        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());
        }
예제 #3
0
        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>&nbsp;</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>&nbsp;</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));
        }
예제 #4
0
        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());
            }
        }
예제 #5
0
        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));
        }
예제 #6
0
        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));
            }
        }
예제 #7
0
        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));
        }
예제 #8
0
        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());
            }
        }