private static void Main(string[] args) { InstanciateDependancies(); SetSession(); // TODO: from storage ShopArticleFilter articleFilter = null; var firstArticle = articleService.GetFirst().Result; if (firstArticle != null) { articleFilter = new ShopArticleFilter(firstArticle.Id, 500); shopService.ShopArticle(articleFilter); // separate in 2 methods - FindArticle and BuyArticle } ShowAllArticles(); Console.WriteLine("Enter Article ID to find in inventory:"); Guid articleId; Guid.TryParse(Console.ReadLine(), out articleId); Console.WriteLine("Enter MAX PRICE:"); int maxPrice; Int32.TryParse(Console.ReadLine(), out maxPrice); articleFilter = new ShopArticleFilter(articleId, maxPrice); shopService.ShopArticle(articleFilter); Console.ReadKey(); }
public OperationResult <string> ShopArticle(ShopArticleFilter filter) { var articleId = filter.ArticleId; try { Pricing bestOffer = this.GetBestPricingOffer(filter.ArticleId, filter.MaxPrice); if (bestOffer != null) { this._logger.Debug($"Trying to sell article with ID = {articleId}."); OrderItem order = new OrderItem() { ArticleId = bestOffer.ArticleId, BuyerId = Session.LoggedBuyer.Id, SupplierId = bestOffer.SupplierId }; this._orderRepository.Insert(order); this._pricingRepository.UpdateAsSold(bestOffer.Id); var message = $"Article with ID = ${articleId} is sold."; this._logger.Info(message); return(OperationResult <string> .OK(message)); } else { var message = $"Don't have best offer for article with ID: {articleId}, " + $"MAX_PRICE: ${filter.MaxPrice}."; this._logger.Info(message); return(OperationResult <string> .Error(Enums.ErrorType.NotFound, message)); } } catch (Exception ex) { var message = $"Error on inserting order: { ex.Message } "; this._logger.Error(message); return(OperationResult <string> .Error(Enums.ErrorType.ServerError, message)); } }
public void ShopArticle() { InMemoryStorage.Instance.Pricings = new List <Pricing>(); InMemoryStorage.Instance.Articles = new List <Article>(); InMemoryStorage.Instance.Suppliers = new List <Supplier>(); Article article1 = new Article() { Name = "Article 1", }; Supplier supplier3 = new Supplier() { Name = "Supplier 3", }; var expenctedGuid = Guid.NewGuid(); InMemoryStorage.Instance.Pricings.Add(new Pricing() { Id = expenctedGuid, Article = article1, Supplier = supplier3, ArticleId = article1.Id, SupplierId = supplier3.Id, Price = 10, IsSold = false }); article1.Suppliers = InMemoryStorage.Instance.Pricings.Where(x => x.ArticleId == article1.Id).Select(x => x.Supplier); supplier3.Articles = InMemoryStorage.Instance.Pricings.Where(x => x.SupplierId == supplier3.Id).Select(x => x.Article); InMemoryStorage.Instance.Articles.Add(article1); InMemoryStorage.Instance.Suppliers.Add(supplier3); var filter = new ShopArticleFilter(article1.Id, 500); var result = this._shopService.ShopArticle(filter); Assert.True(result.Success); Assert.True(result.Result != null); Assert.True(InMemoryStorage.Instance.Pricings.FirstOrDefault(x => x.Id == expenctedGuid).IsSold); }