public HttpResponseMessage Post(TreatmentViewModel model) { if (ModelState.IsValid) { TreatmentViewModel treatment; try { treatment = _repository.Add(model); } catch (NullReferenceException) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, Content = new StringContent("Can't add undefined object.") }); } var response = Request.CreateResponse<TreatmentViewModel>(HttpStatusCode.Created, treatment); string uri = Url.Link("DefaultApi", new { id = model.Id }); response.Headers.Location = new Uri(uri); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
/// <summary> /// Add the specified model. /// </summary> /// <param name="model">Model.</param> public TreatmentViewModel Add(TreatmentViewModel model) { Treatments treatment = new Treatments() { Id = GetNewId(), Description = model.Description, Duration = model.Duration, Active = 1, Name = model.Name, Price = model.Price }; context.Treatments.Add(treatment); context.SaveChanges(); model.Id = treatment.Id; return model; }
public ClientHistoryViewModel PutDoneTreatment(Guid id, TreatmentViewModel treat) { if (ModelState.IsValid) { var client = CheckIfClientExists(id); var treatment = RestApp.Controllers.TreatmentsController.CheckIfTreatExists(treat.Id); if (treat.Id == null) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("Can't change when id of treatment is empty.") }); } return _repository.ChangeTreatmentStatus(id, treat.Id); } else { throw new HttpResponseException(HttpStatusCode.BadRequest); } }