// PUT api/Evaluation/5 public HttpResponseMessage Put(int id, Evaluation viewModel) { viewModel.User = this.User.Identity.Name; if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != viewModel.EvaluationID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(viewModel).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// GET api/Evaluation public Evaluation Get() { // get current user and see if he is a SubAccount var user = db.Users.Where(u => u.UserName == this.User.Identity.Name).FirstOrDefault(); if (user == null) return null; var Evaluation = new Evaluation(); if (user.IsSubAccount.GetValueOrDefault()) { Evaluation = db.Evaluations.Where(b => b.User == user.ParentUserName).FirstOrDefault(); } else Evaluation = db.Evaluations.Where(b => b.User == this.User.Identity.Name).FirstOrDefault(); return Evaluation; }
// POST api/Evaluation public HttpResponseMessage Post(Evaluation viewModel) { viewModel.User = this.User.Identity.Name; if (ModelState.IsValid) { db.Evaluations.Add(viewModel); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, viewModel); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = viewModel.EvaluationID })); return response; } else return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); }