public HttpResponseMessage Post(Home home) { try { if (ModelState.IsValid) { this._unit.Homes.Add(home); this._unit.SaveChanges(); HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.Created, home); result.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = home.Id })); return result; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }
private static Home House2() { Home h1 = new Home { StreetAdress = "Str 23 44 12", City = "Manizales", ZipCode = 1212, Bedrooms = 5, Bathrooms = 2, SquareFeet = 1500, Price = 1500, Description = "My Second House", ImageName = "img2.jpg", }; return h1; }
private static Home House1() { Home h1 = new Home { StreetAdress = "FR 45 Str 23", City = "Manizales", ZipCode = 12345, Bedrooms = 3, Bathrooms = 2, SquareFeet = 1000, Price = 1000, Description = "My First House", ImageName = "img1.jpg", }; return h1; }
public HttpResponseMessage Put(int id, Home home) { if (!ModelState.IsValid) { return Request. CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != home.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } Home existingHome = this._unit.Homes.GetById(id); _unit.Homes.Detach(existingHome); // Keep original CreatedOn value home.CreatedOn = existingHome.CreatedOn; this._unit.Homes.Update(home); try { this._unit.SaveChanges(); // Return an explicit value to avoid the fail callback // being incorrectly invoked. return Request. CreateResponse(HttpStatusCode.OK, "{success:'true', verb:'PUT'}"); } catch (DbUpdateConcurrencyException ex) { return Request. CreateErrorResponse(HttpStatusCode.NotFound, ex); } catch (Exception ex) { return Request. CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }
public HomeViewModel() { Home = new Home(); }