// Add to user's purchase history public bool UserAddHistory(int userId, PurchaseHistoryAdd obj) { var user = ds.Users.SingleOrDefault(i => i.UserId == userId); var seller = ds.Users.SingleOrDefault(i => i.UserId == obj.SellerId); var item = ds.Items.SingleOrDefault(i => i.ItemId == obj.ItemId); if (user == null || seller == null || item == null) { return(false); } else { // Set item status item.Status = "Unavailable"; // Add to history var history = new PurchaseHistory() { Item = item, Seller = seller }; ds.PurchaseHistories.Add(history); user.PurchaseHistories.Add(history); ds.SaveChanges(); return(true); } }
public IHttpActionResult AddHistory(int?id, [FromBody] PurchaseHistoryAdd obj) { if (obj == null) { return(BadRequest("Must send an entity body with the request")); } var added = m.UserAddHistory(id.GetValueOrDefault(), obj); if (!added) { return(BadRequest("Cannot add to history")); } else { return(Ok()); } }
public void UserAddHistory() { // Arrange User user = SetupUserData(); Item item = GetItemData(); SetupController(controller, HttpMethod.Post); var history = new PurchaseHistoryAdd { ItemId = item.ItemId, SellerId = user.UserId }; // Act controller.AddHistory(user.UserId, history); // Assert var result = user.PurchaseHistories.FirstOrDefault(); Assert.IsTrue(result.Item.ItemId == item.ItemId); }