public IHttpActionResult GetAccomodation(int id) { Accomodation accomodation = db.Accomodations.Find(id); if (accomodation == null) { return(NotFound()); } return(Ok(ViewModelMapper.ToViewModelAccomodations(accomodation))); }
public IHttpActionResult DeleteAccomodation(int id) { Accomodation accomodation = db.Accomodations.Find(id); if (accomodation == null) { return(NotFound()); } db.Accomodations.Remove(accomodation); db.SaveChanges(); return(Ok(ViewModelMapper.ToViewModelAccomodations(accomodation))); }
public IHttpActionResult PostAccomodation(AccomodationViewModel accomodationViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Client c = db.Clients.SingleOrDefault(x => x.FirstName + " " + x.LastName == accomodationViewModel.ClientName); if (c == null) { string[] parts = accomodationViewModel.ClientName.Split(' '); Client newClient = new Client() { FirstName = parts.FirstOrDefault(), LastName = parts.LastOrDefault() }; c = db.Clients.Add(newClient); } Accomodation accomodation = ViewModelMapper.ToModelAccomodations(accomodationViewModel); accomodation.Client = c; db.Accomodations.Add(accomodation); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = accomodation.AccomodationId }, ViewModelMapper.ToViewModelAccomodations(accomodation))); }
// GET: api/AccomodationsApi public List <AccomodationViewModel> GetAccomodations() { return(db.Accomodations.OrderByDescending(x => x.AccomodationId).ToList().Select(x => ViewModelMapper.ToViewModelAccomodations(x)).ToList()); }