public IHttpActionResult AddComment(CommentRequestModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                return this.BadRequest(InvalidModelMessage);
            }

            var entity = new Comment
            {
                Content = model.Content,
                UserName = this.identityProvider.GetIdentity().GetUserId(),
                CreatedOn = DateTime.Now,
                RealEstateId = model.RealEstateId
            };

            this.comments.Add(entity);

            CommentResponseModel response = this.comments
                .All()
                .Where(x => x.RealEstateId == entity.RealEstateId & x.Content == entity.Content)
                .ProjectTo<CommentResponseModel>()
                .FirstOrDefault();

            return this.Created("api/Comments", response);
        }
Beispiel #2
0
        public string AddComment(int issueId, string commentDescription)
        {
            // If there is no logged in user, the action returns There is no currently logged in user
            if (this.Data.CurrentUser == null)
            {
                return "There is no currently logged in user";
            }

            // If the issue ID is invalid (i. e., does not exist in the database), the action returns There is no issue with ID < id >
            if (!this.Data.IssueId_Issue.ContainsKey(issueId))
            {
                return string.Format("There is no issue with ID {0}", issueId);
            }

            // In case of success, the action returns Comment added successfully to issue <id>
            var issue = this.Data.IssueId_Issue[issueId];
            var comment = new Comment(this.Data.CurrentUser, commentDescription);
            issue.AddComment(comment);
            this.Data.User_Comments[this.Data.CurrentUser].Add(comment);
            return string.Format("Comment added successfully to issue {0}", issue.Id);
        }
        public ActionResult CreateComment(CommentViewModel inputComment, int postId)
        {
            var loggedUserId = this.User.Identity.GetUserId();
            var from =
                this.Data.Users.All()
                   .FirstOrDefault(us => us.Id == loggedUserId);

            if (inputComment == null)
            {
                return new EmptyResult();
            }

            var comment = new Comment()
            {
                Content = inputComment.Content,
                From = from,
                TargetId = postId,
                TargetType = TargetType.Post
            };

            var post = this.Data.Posts.All().FirstOrDefault(p => p.Id == postId);
            if (post == null)
            {
                return new EmptyResult();
            }

            post.Comments.Add(comment);
            this.Data.SaveChanges();

            var commentForView =
                this.Data.Comments.All()
                    .Project()
                    .To<CommentViewModel>()
                    .FirstOrDefault(c => c.Id == comment.Id);

            return PartialView("_DisplayComment", commentForView);
        }