コード例 #1
0
        public IHttpActionResult PostAddComment(int id, CommentBindingModel commentData)
        {
            if (!this.Data.Bugs.All().Any(b => b.Id == id))
            {
                return this.NotFound();
            }

            if (commentData == null)
            {
                return this.BadRequest();
            }

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

            var comment = new Comment() { Text = commentData.Text, BugId = id, DateCreated = DateTime.Now };
            if (this.User.Identity.IsAuthenticated)
            {
                comment.AuthorId = this.User.Identity.GetUserId();
            }

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

            if (this.User.Identity.IsAuthenticated)
            {
                return this.Ok(new { Id = comment.Id, Author = this.User.Identity.GetUserName(), Message = "User comment added for bug #" + id });
            }

            return this.Ok(new { Id = comment.Id, Message = "Added anonymous comment for bug #" + id });
        }
コード例 #2
0
        public IHttpActionResult AddCommentForGivenBug(int id, AddCommentBindingModel addCommentModel)
        {
            if (addCommentModel == null)
            {
                return this.BadRequest();
            }

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

            var bug = db.Bugs.FirstOrDefault(b => b.Id == id);

            if (bug == null)
            {
                return this.NotFound();
            }

            var currentUserId = User.Identity.GetUserId();
            var author = db.Users.FirstOrDefault(u => u.Id == currentUserId);

            Comment comment = new Comment()
            {
                Text = addCommentModel.Text,
                PublishDate = DateTime.Now,
                Author = author
            };

            bug.Comments.Add(comment);
            db.SaveChanges();

            if (author == null)
            {
                return this.Ok(new
                {
                    Id = comment.Id,
                    Message = "Added anonymous comment for bug #" + bug.Id
                });
            }

            return this.Ok(new
            {
                Id = comment.Id,
                Author = author.UserName,
                Message = "User comment added for bug #" + bug.Id
            });
        }
コード例 #3
0
        public IHttpActionResult AddCommentForGivenBug(int id, PostCommentBindingModel model)
        {
            if (model == null)
            {
                return BadRequest("Text cannot be empty.");
            }

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

            var bug = this.Data.Bugs.Find(id);
            if (bug == null)
            {
                return this.NotFound();
            }

            var currentUserId = User.Identity.GetUserId();
            var comment = new Comment
            {
                Text = model.Text,
                AuthorId = currentUserId != null ? currentUserId : null,
                BugId = bug.Id,
                DateCreated = DateTime.Now
            };

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

            if (this.User.Identity.IsAuthenticated)
            {
                var currentUserName = User.Identity.GetUserName();
                return this.Ok(new
                {
                    Id = comment.Id,
                    Author = currentUserName,
                    Message = "User comment added for bug #" + bug.Id
                });
            }

            return this.Ok(new 
            {
                Id = comment.Id,
                Message = "Added anonymous comment for bug #" + bug.Id
            });
        }
コード例 #4
0
        public IHttpActionResult AddNewComment([FromBody]AddCommentBindingModel model, int id)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null.");
            }

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

            string loggedUserId = this.User.Identity.GetUserId();
            var user = this.Data.Users.Find(loggedUserId);
            var bug = this.Data.Bugs.Find(id);
            if (bug==null)
            {
                return this.NotFound();
            }   
            var comment = new Comment()
            {
                Text = model.Message,
                CreatedOn = DateTime.Now,
                CommentAuthor = user,
                Bug = bug
            };
            this.Data.Comments.Add(comment);
            this.Data.SaveChanges();
            if (user == null)
            {
                return this.Ok(new
                {
                    Id = comment.Id, 
                    Message = string.Format("Added anonymous comment for bug #{0}.", bug.Id)
                });
            }
            else
            {
                return this.Ok(new
                {
                    Id = comment.Id, 
                    Author = user.UserName, 
                    Message = string.Format("User comment added for bug #{0}.", bug.Id)
                });
            }
        }
コード例 #5
0
 private static void Seed()
 {
     var context = new BugTrackerDbContext();
     var comment =new Comment()
     {
         Text = "First comment",
         CreatedOn = DateTime.Now
     };
     context.Comments.Add(comment);
     var bug = new Bug()
     {
         Title = "First bug",
         Description = "Bug 1",
         SubmitDate = DateTime.Now
     };
     bug.Comments.Add(comment);
     context.Bugs.Add(bug);
     context.SaveChanges();
 }
コード例 #6
0
        public IHttpActionResult PostCommentForBug(int id, CommentInputModel commentData)
        {
            var currentUser = this.User.Identity.GetUserId();
            var bugCheck = this.db.Bugs.Find(id);

            if (bugCheck == null)
            {
                return this.NotFound();
            }
            if (commentData.Text == null || commentData.Text.Length <= 1)
            {
                return this.BadRequest();
            }

            var comment = new Comment()
                {
                    Text = commentData.Text,
                    AuthorId = currentUser,
                    BugId = id,
                    Date = DateTime.Now,
                };
            this.db.Comments.Add(comment);
            this.db.SaveChanges();

            if (currentUser == null)
            {
                return this.Ok(new 
                {
                    Id = comment.Id,
                    Message = "Added anonymous comment for bug #"+ id
                });
            }

            return this.Ok(new
                {
                    Id = comment.Id,
                    Author = User.Identity.GetUserName(), 
                    Message =  "User comment added for bug #"+ id
                });

        }
コード例 #7
0
        public IHttpActionResult AddComment(int id, CommentInputModel model)
        {
            var bug = this.Data.Bugs.Find(id);
            if (bug == null)
            {
                return this.NotFound();
            }

            if (model == null || !this.ModelState.IsValid)
            {
                return this.BadRequest("The comment text is required.");
            }

            var newComment = new Comment
            {
                Text = model.Text,
                DateCreated = DateTime.Now,
                BugId = bug.Id,
                UserId = this.User.Identity.GetUserId()
            };

            this.Data.Comments.Add(newComment);
            this.Data.SaveChanges();

            object infoToReturn = new { newComment.Id, Message = "Added anonymous comment for bug #" + bug.Id };
            if (newComment.UserId != null)
            {
                infoToReturn = new
                {
                    newComment.Id,
                    Author = this.User.Identity.GetUserName(),  
                    Message = "User comment added for bug #" + bug.Id
                };
            }

            return this.Ok(infoToReturn);
        }
コード例 #8
0
        public IHttpActionResult AddComment(int id, CommentInputModel commentData)
        {
            if (commentData == null)
            {
                return BadRequest("Missing comment data.");
            }

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

            var bug = db.Bugs.Find(id);
            if (bug == null)
            {
                return this.NotFound();
            }

            var currentUserId = User.Identity.GetUserId();

            var comment = new Comment()
            {
                Text = commentData.Text,
                AuthorId = currentUserId,
                DateCreated = DateTime.Now,
                BugId = bug.Id
            };
            db.Comments.Add(comment);
            db.SaveChanges();

            if (currentUserId != null)
            {
                var currentUserName = User.Identity.GetUserName();
                return this.Ok(new
                {
                    comment.Id,
                    Author = currentUserName,
                    Message = "User comment added   for bug #" + id
                });
            }
        
            return this.Ok(new
            {
                comment.Id,
                Message = "Added anonymous comment for bug #" + id
            });
        }
コード例 #9
0
        public IHttpActionResult AddCommentForGivenBug(int id, CommentBindingModel model)
        {
            if (model == null)
            {
                return BadRequest();
            }

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

            var bug = db.Bugs.Find(id);
            if (bug == null)
            {
                return NotFound();
            }

            var currUserId = User.Identity.GetUserId();
            var currUser = db.Users.Find(currUserId);

            var comment = new Comment()
            {
                Text = model.Text,
                Author = currUser,
                DateCreated = DateTime.Now,
                Bug = bug
            };

            db.Comments.Add(comment);
            db.SaveChanges();

            if (currUser == null)
            {
                return this.Ok(new
                {
                    Id = comment.Id,
                    Message = "Added anonymous comment for bug #" + bug.Id
                });
            }

            return this.Ok(new
            {
                Id = comment.Id,
                Author = comment.Author.UserName,
                Message = "User comment added for bug #" + bug.Id
            });
        }
コード例 #10
0
        public IHttpActionResult PostCommentInBug(int id, CommentCreateBindingModel model)
        {
            var bug = Data.Bugs.Find(id);

            if (bug == null)
            {
                return this.NotFound();
            }

            if (model == null)
            {
                return this.BadRequest();
            }

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

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.Data.Users.Find(currentUserId);

            var comment = new Comment()
            {
                Text = model.Text,
                BugId = bug.Id,
                PublishDate = DateTime.Now,
                Author = currentUser
            };

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

            if (currentUser == null)
            {
                return this.Ok(new
                {
                    Id = comment.Id,
                    Message = "Added anonymous comment for bug #" + id
                });
            }

            return this.Ok(new
            {
                Id = comment.Id,
                Author = currentUser.UserName,
                Message = "User comment added for bug #" + id
            });
        }
コード例 #11
0
        public IHttpActionResult PostComment(int id, [FromBody]CommentBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (model == null)
            {
                return this.BadRequest("Missing comment data");
            }

            var bug = db.Bugs.Find(id);
            if (bug == null)
            {
                return this.NotFound();
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.db.Users.Find(currentUserId);

            var commentToAdd = new Comment()
            {
                Text = model.Text,
                Author = currentUser,
                PublishDate = DateTime.Now,
                BugId = id
            };

            db.Comments.Add(commentToAdd);
            db.SaveChanges();

            if (commentToAdd.Author == null)
            {
                return this.Ok(new
                {
                    Id = commentToAdd.Id,
                    Message = "Added anonymous comment for bug # " + id
                });

            }
            return this.Ok(new
            {
                Id = commentToAdd.Id,
                Author = commentToAdd.Author.UserName,
                Message = "Added anonymous comment for bug # " + id
            });

        }
コード例 #12
0
        public IHttpActionResult PostNewCommentToGivenBug(int id, PostNewCommentBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model is null.");
            }

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

            var bugInDb = this.Data.Bugs.All().FirstOrDefault(b => b.Id == id);
            if (bugInDb == null)
            {
                return this.NotFound();
            }

            var newComment = new Comment
            {
                Text = model.Text,
                DateCreated = DateTime.Now,
                BugId = bugInDb.Id
            };

            var loggedUserId = this.User.Identity.GetUserId();
            if (loggedUserId != null)
            {

                var userInDb = this.Data.Users.All()
                    .FirstOrDefault(u => u.Id == loggedUserId);
                if (userInDb == null)
                {
                    return this.BadRequest("Invalid token");
                }

                newComment.AuthorId = loggedUserId;
                this.Data.Comments.Add(newComment);
                this.Data.SaveChanges();

                bugInDb.Comments.Add(newComment);
                userInDb.Comments.Add(newComment);
                this.Data.SaveChanges();

                return this.Ok(new
                {
                    Id = newComment.Id,
                    Author = userInDb.UserName,
                    Message = "User comment added for bug #" + newComment.Id
                });
            }

            this.Data.Comments.Add(newComment);
            this.Data.SaveChanges();

            bugInDb.Comments.Add(newComment);
            this.Data.SaveChanges();

            return this.Ok(new
            {
                Id = newComment.Id,
                Message = "Added anonymous comment for bug #" + newComment.Id
            });
        }