public async Task <LendBasketCommandResult> Handle(LendBasketCommand request, CancellationToken cancellationToken) { if (!_cache.TryGetValue(request.EmployeeName, out Basket basket)) { return(new LendBasketCommandResult(false, new List <string>() { "Basket not exists" })); } if (basket.Customer is null) { return(new LendBasketCommandResult(false, new List <string>() { "Customer is required for lend basket" })); } if (!basket.StockWithBooks.Any()) { return(new LendBasketCommandResult(false, new List <string>() { "Stocks are required for lend basket" })); } //TODO check if customer exists var customerMessageResponse = await _customerClient.GetResponse <CustomerInfoResult>( new { Email = basket.Customer.Email.EmailAddress }, cancellationToken); if (customerMessageResponse.Message.Customer is null) { return(new LendBasketCommandResult(false, new List <string>() { $"Customer with email {basket.Customer.Email.EmailAddress} not exist" })); } //TODO check if all stocks exists var stockMessageResponse = await _stockClient.GetResponse <StocksExistanceResult>(new { StocksIds = basket.StockWithBooks.Select(s => s.StockId).ToList() }); if (!stockMessageResponse.Message.IsAllExists) { return(new LendBasketCommandResult(false, new List <string>() { "Not all Stocks exists in system" })); } var stocksInBasket = basket.StockWithBooks.Select(s => s.StockId).ToList(); var borrowedStocks = (await _lendedBasketRepository.GetAllByStocksIds(stocksInBasket)).ToList(); if (borrowedStocks.Any()) { return(new LendBasketCommandResult(false, new List <string>() { $"Stocks:{string.Join(",", borrowedStocks.Select(s => s.Stocks))} are already borrowed by someone" })); } var check = await CheckIfCustomerAndBasketMatchAllStrategies(basket); if (!check.Item1) { return(new LendBasketCommandResult(false, new List <string>() { check.Item2.FirstOrDefault() })); } var lendedBasket = new LendedBasket(basket, _intStrategies, _booleanStrategies); _lendedBasketRepository.Add(lendedBasket); if (await _lendedBasketRepository.UnitOfWork.SaveChangesAsync(cancellationToken) < 0) { return(new LendBasketCommandResult(false, new List <string>() { "Something went wrong during saving" })); } _cache.Remove(request.EmployeeName); return(new LendBasketCommandResult(true)); }
public LendedBasket Add(LendedBasket lendedBasket) { return(_lendDbContext.LendedBaskets.Add(lendedBasket).Entity); }