public IHttpActionResult PutShoppingList(int id, Models.ShoppingList shoppingList) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != shoppingList.Id) { return(BadRequest()); } db.Entry(shoppingList).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ShoppingListExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PutItem(int id, Item item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != item.Id) { return(BadRequest()); } db.Entry(item).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ItemExists(id)) { return(NotFound()); } else { throw; } } return(Ok(item)); }
public ActionResult Create([Bind(Include = "ID,ListId,Name,Checked")] Item item) { if (ModelState.IsValid) { db.Items.Add(item); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(item)); }
public ActionResult Create([Bind(Include = "ID,Name")] List list) { if (ModelState.IsValid) { db.Lists.Add(list); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(list)); }
public IActionResult Create([FromBody] ShoppingListItem item) { if (item == null) { return(BadRequest()); } _context.ShoppingListItems.Add(item); _context.SaveChanges(); return(CreatedAtRoute("GetItem", new { id = item.Id }, item)); }
public override async Task Execute(Message message, TelegramBotClient botClient) { ShoppingListContext context = new ShoppingListContext(); string listName = GetListName(message.Text); ShopList shopList = context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id).FirstOrDefault(s => s.NameOfList == listName); if (shopList == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There's no such list"); return; } foreach (string i in GetItems(message.Text)) { if (context.BuyItems.Where(b => b.ShopListId == shopList.ShopListId).FirstOrDefault(b => b.Item == i) != null) { await botClient.SendTextMessageAsync(message.Chat.Id, i + " already in list."); continue; } BuyItem item = new BuyItem { Item = i, ShopList = shopList, ShopListId = shopList.ShopListId }; context.BuyItems.Add(item); } context.SaveChanges(); await botClient.SendTextMessageAsync(message.Chat.Id, "Items was succesfully added"); }
public void Post_Existing_Item_Returns_Conflict() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var testObject = mockData.FirstOrDefault(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); //Act var result = controller.Post(testObject); //Assert Assert.IsNotNull(result); Assert.AreEqual(409, result.StatusCode); Assert.AreEqual(result.Value, $"Drink {testObject.Name} already exists in the shopping list."); } }
public override async Task Execute(Message message, TelegramBotClient botClient) { string listName = GetListName(message.Text); ShoppingListContext context = new ShoppingListContext(); ShopList shopList = context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id).FirstOrDefault(s => s.NameOfList == listName); if (shopList == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There's no such list."); return; } foreach (string item in GetItems(message.Text)) { BuyItem buyItem = context.BuyItems.Where(b => b.ShopListId == shopList.ShopListId).FirstOrDefault(b => b.Item == item); if (buyItem == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There is no element '" + item + "' in list '" + listName + "'"); continue; } context.BuyItems.Remove(buyItem); } context.SaveChanges(); await botClient.SendTextMessageAsync(message.Chat.Id, "Items was succesfully removed"); }
public void DeleteListItem(string listName, long itemId, int quantity) { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { List targetList = context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .FirstOrDefault(); if (targetList != null) { ListItem itemToDelete = targetList.ListItems.Where(item => item.Id == itemId).FirstOrDefault(); if (itemToDelete != null) { if (itemToDelete.Quantity <= quantity) { context.ListItems.Remove(itemToDelete); } else { itemToDelete.Quantity -= quantity; } context.SaveChanges(); } } } }
public void Get_Item_Returns_Expected_Item() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var expectedObject = mockData.FirstOrDefault(); var expectedResult = new OkObjectResult(expectedObject); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get(expectedObject.Name) as OkObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); Assert.IsTrue(expectedResult.Value.Equals(result.Value)); } }
public void AddItemToList(string listName, long itemId, int quantity) { // Add a ListItem entry for the specified ID to the specified list using (ShoppingListContext context = ShoppingListContextFactory.Create()) { List targetList = context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .FirstOrDefault(); if (targetList != null) { // If there is already an item with the same identity in the list then simply update its quantity ListItem itemToUpdate = targetList.ListItems.Where(item => item.ItemId == itemId).FirstOrDefault(); if (itemToUpdate != null) { itemToUpdate.Quantity += quantity; } else { context.ListItems.Add(new ListItem { ItemId = itemId, ListId = targetList.Id, Quantity = quantity }); } context.SaveChanges(); } } }
public IActionResult Update(long id, [FromBody] ShoppingItem item) { if (item == null) { return(NotFound()); } var entity = _context.ShoppingList.FirstOrDefault(t => t.Id == id); entity.Title = item.Title; entity.Description = item.Description; entity.Url = item.Url; _context.SaveChanges(); return(new ObjectResult(entity)); }
public void Delete_Removes_Existing_Drink_From_Shopping_List() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var expectedResult = mockData.FirstOrDefault(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get(expectedResult.Name); //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); Assert.AreEqual(result.Value, expectedResult); } }
public void Get_Non_Existant_Item_Returns_Not_Found() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get("Does Not Exist"); //Assert Assert.IsNotNull(result); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual(result.Value, "Drink: Does Not Exist not found on the shopping list."); } }
public void Get_All_Return_Expected_List() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var expectedResult = new OkObjectResult(mockData); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get(); //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); CollectionAssert.Equals(expectedResult.Value, result.Value); } }
public Cart Create(Cart cart) { using (var _context = new ShoppingListContext()) { _context.Cart.Add(cart); DbContextHelper.HandleUniqueKeyViolation(() => _context.SaveChanges()); return(cart); } }
public ShoppingListController(ShoppingListContext context) { _context = context; if (_context.ShoppingListItems.Count() == 0) { _context.ShoppingListItems.Add(new ShoppingListItem { Name = "Bananas" }); _context.SaveChanges(); } }
protected override void Seed(ShoppingListContext context) { context.UserVerificationStatuses.AddOrUpdate( b => b.UserVerificationStatusId, UserVerificationStatus.GetDefaults().ToArray()); context.UserVerificationReasons.AddOrUpdate(uv => uv.UserVerificationReasonId, UserVerificationReason.GetDefaults().ToArray()); context.Roles.AddOrUpdate(r => r.Name, RoleNames.GetRolesWithCaptions().Select(r => new IdentityRole { Name = r.Key, ArabicName = r.Value, EnglishName = r.Value }).ToArray()); context.SaveChanges(); }
public bool RemoveFromCart(long productId, long cartId) { using (var _context = new ShoppingListContext()) { var productCart = _context.ProductCart.FirstOrDefault(x => x.Cart_CartId == cartId && x.Product_ProductId == productId); if (productCart != null) { _context.ProductCart.Remove(productCart); _context.SaveChanges(); return(true); } return(false); } }
public override async Task Execute(Message message, TelegramBotClient botClient) { ShoppingListContext context = new ShoppingListContext(); string listName = message.Text.Replace("/removelist ", "").Trim(); ShopList list = context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id).FirstOrDefault(s => s.NameOfList == listName); if (list == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There's no such list."); return; } context.ShopLists.Remove(list); context.SaveChanges(); await botClient.SendTextMessageAsync(message.Chat.Id, "List was succesfully removed"); }
public bool Delete(UserDeleteRequest request) { using (var _context = new ShoppingListContext()) { var user = _context.User.FirstOrDefault(u => u.Id == request.Id); if (user == null) { return(false); } _context.User.Remove(user); _context.SaveChanges(); return(true); } }
public bool Delete(ProductDeleteRequest request) { using (var _context = new ShoppingListContext()) { Product product = _context.Product.FirstOrDefault(p => p.Id == request.Id); if (product != null) { _context.Product.Remove(product); _context.SaveChanges(); return(true); } else { return(false); } } }
public override async Task Execute(Message message, TelegramBotClient botClient) { var chatId = message.Chat.Id; WebApiApplication.current_userId = message.From.Id; ShoppingListContext context = new ShoppingListContext(); User user = await context.Users.FirstOrDefaultAsync(u => u.UserTelegramId == message.From.Id); if (user == null) { user = new User(message.From.Id, message.From.FirstName); context.Users.Add(user); context.SaveChanges(); } await botClient.SendTextMessageAsync(chatId, "Hello I'm ShoppingListBot. Current user id" + WebApiApplication.current_userId); if (user.Catalog.Count == 0) { await botClient.SendTextMessageAsync(chatId, "You don't have any shopping lists, lets create your first list. Enter '/addList' + name of list. "); } }
public bool ToCart(Product product, Cart cart, int quantity) { using (var _context = new ShoppingListContext()) { var entity = _context.ProductCart.FirstOrDefault(p => p.Product_ProductId == product.Id); if (entity == null) { entity = new ProductCart() { Cart_CartId = cart.Id, Product_ProductId = product.Id, Quantity = quantity }; _context.ProductCart.Add(entity); } entity.Quantity += quantity; DbContextHelper.HandleUniqueKeyViolation(() => _context.SaveChanges()); return(true); } }
public void Put_To_Existing_Item_Updates_Quantity() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); context.Dispose(); }; var expectedObject = new DrinkOrder { Name = "Pepsi", Quantity = 4 }; var expectedResult = new OkObjectResult(expectedObject); using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Put(expectedObject) as OkObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); Assert.AreEqual(result.Value, expectedResult.Value); //Check context is updated Assert.AreEqual(expectedObject.Quantity, context.shoppingList.FirstOrDefault(x => x.Name == expectedObject.Name).Quantity); } }
public override async Task Execute(Message message, TelegramBotClient botClient) { var chatId = message.Chat.Id; string nameOfList = message.Text.Replace(@"/addlist", "").Trim(); ShoppingListContext context = new ShoppingListContext(); ShopList shopList = context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id).FirstOrDefault(s => s.NameOfList == nameOfList); if (shopList != null) { await botClient.SendTextMessageAsync(chatId, "List with that name already exist, choose another name."); return; } User user = context.Users.FirstOrDefault(u => u.UserTelegramId == message.From.Id); shopList = new ShopList { NameOfList = nameOfList, User = user, UserId = user.UserId }; context.ShopLists.Add(shopList); context.SaveChanges(); WebApiApplication.current_list = nameOfList; await botClient.SendTextMessageAsync(chatId, "Shopping list with name '" + nameOfList + "' was created."); }
internal void AddItemToCurrentShopList(string itemName, double count) { try { Item item = _ctx.Items.FirstOrDefault(i => i.Name == itemName); if (item == null) { item = new Item(itemName); _ctx.Items.Add(item); } _ctx.ShopListItems.Add(new ShopListItem(item, count) { User = CurrentUser, ShopList = CurrentShopList.InnerValue }); _ctx.SaveChanges(); Mediator.NotifyCollegues(PageNames.ShopListItemChanged, null); } catch (Exception ex) { MessageBox.Show($"Error:{ex.Message}"); } }
public void Put_To_Non_Existing_Item_Returns_Not_Found() { DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); context.Dispose(); }; var expectedObject = new DrinkOrder { Name = "Does Not Exist", Quantity = 4 }; var expectedResult = new OkObjectResult(expectedObject); using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Put(expectedObject) as NotFoundObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual(result.Value, "Drink: Does Not Exist not found on the shopping list."); } }
private void Save() { dbContext.SaveChanges(); }
public bool Save() { return(_context.SaveChanges() >= 0); }