public BudgetItem UpdateItem(BudgetItem item) { BudgetItem oldItem = db.BudgetItems.Find(item.Id); if (oldItem != null) { db.Entry(oldItem).CurrentValues.SetValues(item); } Save(); return item; }
public IHttpActionResult Post(BudgetItem item) { if (ModelState.IsValid) { _itemRepository.CreateItem(item); return CreatedAtRoute("DefaultApi", new { id = item.Id }, item); } else { return BadRequest(ModelState); } }
// PUT: api/Items/5 public IHttpActionResult Put(int id, BudgetItem item) { if (!ModelState.IsValid) { return BadRequest(ModelState); } BudgetItem oldItem = _itemRepository.GetItem(id); if (oldItem == null) { _itemRepository.CreateItem(item); return CreatedAtRoute("DefaultApi", new { id = item.Id }, item); } else { _itemRepository.UpdateItem(item); // return Content(HttpStatusCode.Accepted, item); return Ok(item); } }
public void WhenPut_ValidItem_ReturnsOk() { // Arrange int itemId = 2; var newItem = new BudgetItem { Id = itemId, Notes = "Office Lunch", BudgetLocationId = 2, BudgetSubCategoryId = 2, Amount = 15, DateOccured = DateTime.Now, IsExpense = true, IsFixed = false }; IEnumerable<BudgetItem> fakeItems = GetFakeItems(); _itemRepository.Setup(x => x.GetItem(itemId)).Returns(fakeItems.FirstOrDefault(p => p.Id == itemId)); _itemRepository.Setup(x => x.UpdateItem(newItem)).Returns(newItem); var controller = new ItemsController(_itemRepository.Object); // Act IHttpActionResult actionResult = controller.Put(2, newItem); // Assert Assert.IsNotNull(actionResult); var item = actionResult as OkNegotiatedContentResult<BudgetItem>; Assert.IsNotNull(item); Assert.AreEqual(item.Content.BudgetLocationId, 2); }
public void WhenPut_InValidItem_CreatesNewItem() { // Arrange var fakeId = 100; var newItem = new BudgetItem { Id = fakeId, Notes = "Office Lunch", BudgetLocationId = 2, BudgetSubCategoryId = 2, Amount = 15, DateOccured = DateTime.Now, IsExpense = true, IsFixed = false }; IEnumerable<BudgetItem> fakeItems = GetFakeItems(); _itemRepository.Setup(x => x.GetItem(fakeId)).Returns(fakeItems.FirstOrDefault(p => p.Id == fakeId)); var controller = new ItemsController(_itemRepository.Object); // Act IHttpActionResult actionResult = controller.Put(fakeId, newItem); // Assert Assert.IsNotNull(actionResult); var createdResult = actionResult as CreatedAtRouteNegotiatedContentResult<BudgetItem>; // Assert Assert.IsNotNull(createdResult); Assert.AreEqual("DefaultApi", createdResult.RouteName); Assert.AreEqual(fakeId, createdResult.RouteValues["id"]); }
public void CreateItem(BudgetItem item) { db.BudgetItems.Add(item); Save(); }