public IHttpActionResult SubmitNewBug(BugBindingModel model) { if (model == null || !this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var bug = new Bug { Title = model.Title, Description = model.Description }; if (this.User.Identity.IsAuthenticated) { var authorId = this.User.Identity.GetUserId(); bug.Author = this.Data.Users.FirstOrDefault(x => x.Id == authorId); } this.Data.Bugs.Add(bug); this.Data.SaveChanges(); if (this.User.Identity.IsAuthenticated) { return(this.CreatedAtRoute( "DefaultApi", new { controller = "bugs", id = bug.Id }, new { bug.Id, Author = bug.Author.UserName, Message = "User bug submitted." })); } return(this.CreatedAtRoute( "DefaultApi", new { controller = "bugs", id = bug.Id }, new { bug.Id, Message = "Anonymous bug submitted." })); }
public IHttpActionResult PostBug([FromBody] BugBindingModel bugModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var userId = this.User.Identity.GetUserId(); User user = null; if (userId != null) { user = this.Data.Users.Find(userId); } var bug = new Bug() { Title = bugModel.Title, Description = bugModel.Description, Author = user, Status = BugStatus.Open, PublishDate = DateTime.Now }; this.Data.Bugs.Add(bug); this.Data.SaveChanges(); if (user == null) { return(this.CreatedAtRoute( "DefaultApi", new { id = bug.Id, controller = "bugs" }, new { bug.Id, Message = "Anonymous bug submitted." })); } return(this.CreatedAtRoute( "DefaultApi", new { id = bug.Id, controller = "bugs" }, new { bug.Id, Author = User.Identity.GetUserName(), Message = "User bug submitted." })); }
public IHttpActionResult SubmitNewBug(BugBindingModel bugModel) { if (bugModel.Title == null) { return(BadRequest("Invalid bug title")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = User.Identity.GetUserId(); var user = db.Users.FirstOrDefault(u => u.Id == currentUserId); var bug = new Bug() { Title = bugModel.Title, Description = bugModel.Description, Status = Status.Open, SubmitDate = DateTime.Now, Author = user != null ? user : null }; db.Bugs.Add(bug); db.SaveChanges(); var bugOutput = new BugViewModel() { Id = bug.Id, Author = bug.Author != null ? bug.Author.UserName : null, Status = bug.Status.ToString(), Title = bug.Title, DateCreated = bug.SubmitDate }; if (currentUserId != null) { bugOutput.Author = bug.Author.UserName; return(CreatedAtRoute( "DefaultApi", new { id = bugOutput.Id, Author = bugOutput.Author, Message = "User bug submitted." }, new { id = bugOutput.Id, Author = bugOutput.Author, Message = "User bug submitted." })); } return(CreatedAtRoute( "DefaultApi", new { id = bugOutput.Id, Message = "Anonymous bug submitted." }, new { id = bugOutput.Id, Message = "Anonymous bug submitted." })); }
public IHttpActionResult PostBug(BugBindingModel bug) { if (bug == null || !ModelState.IsValid) { return BadRequest(); } var newBug = new Bug { Title = bug.Title, Description = bug.Description, DateCreated = DateTime.Now, Status = BugStatus.Open }; var author = this.User.Identity.GetUserId(); if (author != null) { newBug.AuthorId = this.User.Identity.GetUserId(); } data.Bugs.Add(newBug); data.SaveChanges(); if (author != null) { return CreatedAtRoute("DefaultApi", new { id = newBug.Id }, new { Id = newBug.Id, Author = this.User.Identity.GetUserName(), Message = "User bug submitted" }); } return CreatedAtRoute("DefaultApi", new { id = newBug.Id }, new { Id = newBug.Id, Message = "Anonymous bug submitted" }); }