Esempio n. 1
0
        public IHttpActionResult PostNewCommentOnBug([FromUri] int id, [FromBody] CreateaCommentBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

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

            var bug = this.Data.Bugs.GetById(id);

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

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

            if (this.User.Identity.IsAuthenticated)
            {
                var userId = this.User.Identity.GetUserId();
                var user   = this.Data.Users.GetById(userId);
                comment.Author = user;
            }

            bug.Comments.Add(comment);
            this.Data.SaveChanges();
            if (comment.Author == null)
            {
                var viewModel = new CreateWithoutAuthorViewModel()
                {
                    Id      = comment.Id,
                    Message = "Added anonymous comment for bug #" + bug.Id
                };

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

                return(this.Ok(viewModel));
            }
        }
Esempio n. 2
0
        public IHttpActionResult PostNewBug([FromBody] CreateBugBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

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

            var bug = new Bug()
            {
                Title       = model.Title,
                Description = model.Description,
                DateCreated = DateTime.Now,
                Status      = Status.Open,
                Comments    = new List <Comment>(),
                Author      = null
            };

            if (this.User.Identity.IsAuthenticated)
            {
                var loggedUserId = this.User.Identity.GetUserId();
                var user         = this.Data.Users.GetById(loggedUserId);
                bug.Author = user;
            }

            this.Data.Bugs.Add(bug);
            this.Data.SaveChanges();

            if (bug.Author == null)
            {
                var viewModel = new CreateWithoutAuthorViewModel()
                {
                    Id      = bug.Id,
                    Message = "Anonymous bug submitted."
                };

                return(this.Created("http://localhost:5555/api/bugs/" + viewModel.Id, viewModel));
            }
            else
            {
                var viewModel = new CreateWithAuthorViewModel()
                {
                    Id      = bug.Id,
                    Author  = bug.Author.UserName,
                    Message = "User bug submitted."
                };

                return(this.Created("http://localhost:5555/api/bugs/" + viewModel.Id, viewModel));
            }
        }