Esempio n. 1
0
        public IHttpActionResult CreateBug([FromBody] NewBugBindingModel bugsData)
        {
            if (bugsData == null)
            {
                return(BadRequest());
            }

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

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.db.Users.Find(currentUserId);
            var bug           = new Bug
            {
                Title       = bugsData.Title,
                Author      = currentUser,
                SubmitDate  = DateTime.Now,
                Description = bugsData.Description,
                Status      = BugStatus.Open
            };

            db.Bugs.Add(bug);
            db.SaveChanges();

            if (bug.Author == null)
            {
                return(this.CreatedAtRoute(
                           "DefaultApi",
                           new { controller = "bugs", id = bug.Id },
                           new { bug.Id, Message = "Anonymous bug submitted." }
                           ));
            }

            return(this.CreatedAtRoute(
                       "DefaultApi",
                       new { controller = "bugs", id = bug.Id },
                       new { bug.Id, Author = bug.Author.UserName, Message = "User bug submitted." }
                       ));
        }
Esempio n. 2
0
        public IHttpActionResult CreateCommentsByBugId(int id, [FromBody] CommentInputModel commentInputData)
        {
            if (commentInputData == null)
            {
                return(BadRequest("Missing comment data."));
            }

            var bug = db.Bugs.Find(id);

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

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

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.db.Users.Find(currentUserId);
            var comment       = new Comment()
            {
                BugId       = bug.Id,
                Text        = commentInputData.Text,
                Author      = currentUser,
                PublishDate = DateTime.Now,
            };

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

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

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