コード例 #1
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));
            }
        }
コード例 #2
0
ファイル: CommentsController.cs プロジェクト: npelle/seia-gpv
        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());
            }
        }