示例#1
0
        public IActionResult AddComment([FromBody] CommentForPostModel model)
        {
            var cities = _postService.GetCities();

            if (!_workContext.CurrentCustomer.IsRegistered())
            {
                return(Unauthorized());
            }

            var currentUserId = _workContext.CurrentCustomer.Id;

            ViewBag.UserName = _workContext.CurrentCustomer.Username;

            var notifyModel = new NotificationModel();

            if (_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Consultant, true))
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (_postService.IsClosed(model.PostId))
                {
                    return(BadRequest("Post is closed"));
                }

                ViewBag.UserRole = "Consultant";

                if (_postService.IsReserved(model.PostId))
                {
                    if (_postService.IsConsultantAuthToPost(model.PostId, currentUserId))
                    {
                        List <string> errors         = new List <string>();
                        var           commentCreated = _commentService.AddCommentByConsultant(model, currentUserId, model.Files, errors);
                        _postService.SetPostAnsweredByConsultant(model.PostId, currentUserId);
                        string userName        = _workContext.CurrentCustomer.Username;
                        var    commentToReturn = commentCreated.ToCommentModel();
                        commentToReturn.CommentOwner = userName;
                        //--Notification--

                        var post = _postService.GetPostById(commentToReturn.PostId);

                        if (post != null)
                        {
                            notifyModel.OwnerId = post.CustomerId;
                        }

                        notifyModel.PostId = commentCreated.PostId;
                        notifyModel.UserId = _workContext.CurrentCustomer.Id;
                        notifyModel.Type   = 2;

                        _notificationtService.AddCommentNotification(notifyModel);
                        //--End Notification
                        return(CreatedAtRoute("Consultant.Comment.GetComment", new { CommentId = commentToReturn.Id }, commentToReturn));
                    }
                    else
                    {
                        return(Forbid());
                    }
                }
                else
                {
                    List <string> errors = new List <string>();

                    var commentCreated = _commentService.AddCommentByConsultant(model, currentUserId, model.Files, errors);
                    _postService.SetPostAnsweredByConsultant(model.PostId, currentUserId);
                    string userName        = _workContext.CurrentCustomer.Username;
                    var    commentToReturn = commentCreated.ToCommentModel();
                    commentToReturn.CommentOwner = userName;

                    //--Notification--

                    var post = _postService.GetPostById(commentToReturn.PostId);

                    if (post != null)
                    {
                        notifyModel.OwnerId = post.Customer.Id;
                    }

                    notifyModel.PostId = commentCreated.PostId;
                    notifyModel.UserId = _workContext.CurrentCustomer.Id;
                    notifyModel.Type   = 2;

                    _notificationtService.AddCommentNotification(notifyModel);
                    //--End Notification

                    return(GetComment(commentToReturn.Id));
                }
            }
            else if (_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Registered, true))
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (_postService.IsClosed(model.PostId))
                {
                    return(BadRequest("Post is closed"));
                }

                ViewBag.UserRole = "Registered";

                if (_postService.IsCustomerAuthToPost(model.PostId, currentUserId))
                {
                    List <string> errors          = new List <string>();
                    var           commentCreated  = _commentService.AddCommentByCustomer(model, currentUserId, model.Files, errors);
                    string        userName        = _workContext.CurrentCustomer.Username;
                    var           commentToReturn = commentCreated.ToCommentModel();
                    commentToReturn.CommentOwner = userName;

                    //--Notification--

                    var post = _postService.GetPostById(commentToReturn.PostId);

                    if (post != null)
                    {
                        notifyModel.OwnerId = post.Consultant.Id;
                    }

                    notifyModel.PostId = commentCreated.PostId;
                    notifyModel.UserId = _workContext.CurrentCustomer.Id;
                    notifyModel.Type   = 2;

                    _notificationtService.AddCommentNotification(notifyModel);
                    //--End Notification


                    return(GetComment(commentToReturn.Id));
                }
                else
                {
                    return(Forbid());
                }
            }

            else
            {
                return(Unauthorized());
            }
        }