コード例 #1
0
        public IHttpActionResult CreateComment(Comment comment)
        {
            var user = db.Users.Find(User.Identity.GetUserId());

            // Set posterId
            comment.PosterId = user.Id;

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Not a reply
            if (comment.ParentId == null)
            {
                db.Comments.Add(comment);
            }

            // Is reply - ***Perhaps add logic checks here, try catch? ***
            else
            {
                var parent = db.Comments.Find(comment.ParentId);
                if (parent == null)
                {
                    return(NotFound());
                }

                parent.Replies.Add(comment);
            }

            db.SaveChanges();

            return(Ok(TheModelFactory.CreateComment(comment, user.UserName, true, user.PictureId != null ? user.Image.ImageUrl : "/Client/assets/img/signup_male.png"))); //Identity.Get vs comment.ApplicationUser.UserName ? ? ?
            //return CreatedAtRoute("DefaultApi", new { id = comment.CommentId }, comment);
        }