// PUT api/Goal/5 public HttpResponseMessage PutGoal(int id, Goal goal) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != goal.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(goal).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// POST api/Goal public GoalViewModel PostGoal(Goal goal) { if (ModelState.IsValid) { db.Goals.Add(goal); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, goal); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = goal.Id })); var savedGoal = db.Goals.Find(goal.Id); //TODO check out virtual properties and when are filled. PLAYER IS NULL. var newGoal = Mapper.Map<GoalViewModel>(savedGoal); newGoal.Player = Mapper.Map<PlayerViewModel>(db.Players.Find(goal.PlayerId)); return newGoal; } return null; //else //{ // return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); //} }