public async Task <ActionResult <StoreChainResult> > CreateStoreChain(StoreChain chain) { var user = await _currentUserProvider.GetUserAsync(); chain.CreatedAt = DateTime.Now; chain.CreatorId = user.Id; StoreChainResult result = new StoreChainResult(); try { StoreChain created = await _storeChainRepository.CreateAsync(chain); result.IsSuccessful = true; result.ResultData.Add(created); } catch (ItemAlreadyExistsException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(Conflict(result)); } catch (Exception e) { throw e; } return(Ok(result)); }
public bool ItemAlreadyExists(StoreChain item) { var storeChains = _context.StoreChains.ToList(); return(storeChains.Any(s => s.StoreChainId == item.StoreChainId || s.Name == item.Name )); }
public bool ItemCanBeUpdated(StoreChain item) { var storeChains = _context.StoreChains.ToList(); var withOutItem = storeChains.Where(s => s.StoreChainId != item.StoreChainId).ToList(); return(!(withOutItem.Any(s => s.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase) ))); }
public static StoreChain ToEntity(this StoreChainCreateViewModel vm) { var chain = new StoreChain(); chain.Name = vm.Name; chain.PriceCategory = vm.Category; chain.Url = vm.Url; return(chain); }
public async Task <StoreChain> CreateAsync(StoreChain item) { if (ItemAlreadyExists(item)) { throw new ItemAlreadyExistsException(typeof(StoreChain), item.StoreChainId); } _context.StoreChains.Add(item); await _context.SaveChangesAsync(); return(item); }
public async Task <StoreChain> UpdateAsync(string id, StoreChain item) { if (!ItemCanBeUpdated(item)) { throw new ItemAlreadyExistsException(typeof(StoreChain), item.StoreChainId); } var existing = await GetAsync(id); existing.Name = item.Name; existing.PriceCategory = item.PriceCategory; existing.Logo = item.Logo; existing.Url = item.Url; await _context.SaveChangesAsync(); return(existing); }
public void TestFindCheapestSource2() { var shop1 = new Shop(1, "Дикси", "Сытнинская ул., 4"); shop1.DeliverProducts(101, "Pepsi", 36.5, 100); shop1.DeliverProducts(102, "Pepsi", 70, 90); var shop2 = new Shop(2, "Дикси", "Саблинская ул., 13/15"); shop2.DeliverProducts(100, "Black Monster Energy Ultra", 75.9, 22); shop2.DeliverProducts(101, "Pepsi", 36, 100); shop2.DeliverProducts(102, "Pepsi", 70, 90); shop2.DeliverProducts(103, "Snickers", 50, 45); var shop3 = new Shop(3, "Реалъ", "Кирочная ул., 18"); shop3.DeliverProducts(100, "Black Monster Energy Ultra", 90, 60); shop3.DeliverProducts(101, "Pepsi", 38, 111); shop3.DeliverProducts(102, "Pepsi", 69.9, 90); var shop4 = new Shop(4, "SPAR", "Фурштатская ул., 2/12, лит.А"); shop4.DeliverProducts(100, "Black Monster Energy Ultra", 100, 55); shop4.DeliverProducts(101, "Pepsi", 37, 35); shop4.DeliverProducts(102, "Pepsi", 69.9, 75); shop4.DeliverProducts(103, "Snickers", 49.9, 45); shop4.DeliverProducts(104, "M&M's", 130, 100); var myShops = new StoreChain(new List <Shop>() { shop1, shop2, shop3, shop4 }); var expected = new Dictionary <int, int>(); expected.Add(2, 101); expected.Add(3, 102); Assert.AreEqual(expected, myShops.FindCheapestSource("Pepsi")); }
public async Task <ActionResult <StoreChainResult> > UpdateStoreChain(string id, [FromBody] StoreChain chain) { StoreChainResult result = new StoreChainResult(); if (id != chain.StoreChainId) { result.IsSuccessful = false; result.ErrorMessages.Add("Id does not match with object id"); return(BadRequest(result)); } try { var update = await _storeChainRepository.UpdateAsync(id, chain); result.IsSuccessful = true; result.ResultData.Add(update); } catch (ItemNotFoundException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(NotFound(result)); } catch (ItemAlreadyExistsException e) { result.IsSuccessful = false; result.ErrorMessages.Add(e.Message); return(Conflict(result)); } catch (Exception e) { throw e; } return(Ok(result)); }
public UnifiedAPIStore(string storename, string brand, GeoCoordinate geoCoordinate, string zip, string city, string address, StoreChain storeChain) { _storeName = storename; _brand = brand; _storeLocation._latitude = geoCoordinate._latitude; _storeLocation._longitude = geoCoordinate._longitude; _zip = zip; _city = city; _address = address; _chainID = (int)storeChain; _logoURL = LogoURL(brand); }
public void Update(StoreChain storeChain) { dataContext.Update(storeChain); }
public void Add(StoreChain storeChain) { dataContext.Add(storeChain); }
public async Task Handle(StoreChainCreateCommand message, CancellationToken cancellationToken) { var e = new StoreChain(message.Id, message.Name); storeChain.Add(e); }
public void TestFindCheapestSource1() { var shop1 = new Shop(1, "Дикси", "Сытнинская ул., 4"); shop1.DeliverProducts(100, "Black Monster Energy Ultra", 75, 20); shop1.DeliverProducts(101, "Pepsi", 36.5, 100); shop1.DeliverProducts(102, "Pepsi", 70, 90); shop1.DeliverProducts(103, "Snickers", 50, 45); shop1.DeliverProducts(104, "M&M's", 120, 30); shop1.DeliverProducts(105, "Dr. Pepper", 57, 40); shop1.DeliverProducts(106, "AriZona", 129, 15); shop1.DeliverProducts(107, "Mars", 37, 50); shop1.DeliverProducts(108, "Pringles", 90, 25); shop1.DeliverProducts(109, "M1911", 15500, 10); var shop2 = new Shop(2, "Дикси", "Саблинская ул., 13/15"); shop2.DeliverProducts(100, "Black Monster Energy Ultra", 75.9, 22); shop2.DeliverProducts(101, "Pepsi", 36, 100); shop2.DeliverProducts(102, "Pepsi", 70, 90); shop2.DeliverProducts(103, "Snickers", 50, 45); shop2.DeliverProducts(104, "M&M's", 128.9, 30); shop2.DeliverProducts(105, "Dr. Pepper", 56.9, 40); shop2.DeliverProducts(106, "AriZona", 128.5, 15); shop2.DeliverProducts(107, "Mars", 37, 50); shop2.DeliverProducts(108, "Pringles", 90, 30); shop2.DeliverProducts(109, "M1911", 15490, 12); var shop3 = new Shop(3, "Реалъ", "Кирочная ул., 18"); shop3.DeliverProducts(100, "Black Monster Energy Ultra", 90, 60); shop3.DeliverProducts(101, "Pepsi", 38, 111); shop3.DeliverProducts(102, "Pepsi", 69.9, 90); shop3.DeliverProducts(103, "Snickers", 50, 45); shop3.DeliverProducts(104, "M&M's", 123, 30); shop3.DeliverProducts(105, "Dr. Pepper", 60, 70); shop3.DeliverProducts(106, "AriZona", 128, 25); shop3.DeliverProducts(107, "Mars", 37, 60); shop3.DeliverProducts(108, "Pringles", 100, 40); shop3.DeliverProducts(109, "M1911", 55490, 2); var shop4 = new Shop(4, "SPAR", "Фурштатская ул., 2/12, лит.А"); shop4.DeliverProducts(100, "Black Monster Energy Ultra", 100, 55); shop4.DeliverProducts(101, "Pepsi", 37, 35); shop4.DeliverProducts(102, "Pepsi", 69.9, 75); shop4.DeliverProducts(103, "Snickers", 49.9, 45); shop4.DeliverProducts(104, "M&M's", 130, 100); shop4.DeliverProducts(105, "Dr. Pepper", 65, 70); shop4.DeliverProducts(106, "AriZona", 128, 25); shop4.DeliverProducts(107, "Mars", 36, 80); shop4.DeliverProducts(108, "Pringles", 97, 60); shop4.DeliverProducts(109, "M1911", 45500, 1); var myShops = new StoreChain(new List <Shop>() { shop1, shop2, shop3, shop4 }); Assert.AreEqual(1, myShops.FindCheapestSource(100)); Assert.AreEqual(2, myShops.FindCheapestSource(101)); Assert.AreEqual(3, myShops.FindCheapestSource(102)); Assert.AreEqual(4, myShops.FindCheapestSource(103)); Assert.AreEqual(1, myShops.FindCheapestSource(104)); Assert.AreEqual(2, myShops.FindCheapestSource(105)); Assert.AreEqual(3, myShops.FindCheapestSource(106)); Assert.AreEqual(4, myShops.FindCheapestSource(107)); Assert.AreEqual(1, myShops.FindCheapestSource(108)); Assert.AreEqual(2, myShops.FindCheapestSource(109)); try { myShops.FindCheapestSource(200); Assert.Fail(); } catch (UserException e) { Assert.AreEqual("No Product in the Shop №1 with Code 200!", e.Message); } }