Beispiel #1
0
        public IHttpActionResult CreateBug(BugsBindingModel bugData)
        {
            if (bugData == null)
            {
                return(BadRequest("Missing bug data."));
            }

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

            User user = null;

            if (User.Identity.IsAuthenticated)
            {
                user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);
            }

            var bug = new Bug()
            {
                Title       = bugData.Title,
                Description = bugData.Description,
                Status      = BugStatus.Open,
                Author      = user,
                DateCreated = DateTime.Now
            };

            db.Bugs.Add(bug);
            db.SaveChanges();
            if (user != null)
            {
                return(this.CreatedAtRoute(
                           "DefaultApi",
                           new { controller = "bugs", id = bug.Id },
                           new { Id = bug.Id, Author = user.UserName, Message = "User bug submitted." }));
            }
            return(this.CreatedAtRoute(
                       "DefaultApi",
                       new { controller = "bugs", id = bug.Id },
                       new { Id = bug.Id, Message = "Anonymous bug submitted." }));
        }
Beispiel #2
0
        public IHttpActionResult EditBug(int id, BugsBindingModel bugData)
        {
            if (bugData == null)
            {
                return(BadRequest("Missing bug data."));
            }

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

            if (bug == null)
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (bugData.Title != null)
            {
                bug.Title = bugData.Title;
            }
            if (bugData.Description != null)
            {
                bug.Description = bugData.Description;
            }

            bug.Status = bugData.Status;
            db.SaveChanges();

            return(this.Ok(
                       new
            {
                Message = "Bug #" + id + " patched."
            }
                       ));
        }