public async Task <PurchaseOperationResult> HandleOperation(Guid clientId, Guid productId) { CustomValidator.ValidateId(clientId); CustomValidator.ValidateId(productId); var client = await _clientRepository.GetOne(clientId); var product = await _productRepository.GetOne(productId); if (client != null && product != null) { var clientBooks = await _bookRepository.GetClientBooks(client.Id); Book currentClientProductBook = null; foreach (var b in clientBooks) { if (b.ProductId.Equals(product.Id)) { currentClientProductBook = b; break; } } if (product.ProductStatus.Equals(ProductStatus.Booked) && currentClientProductBook != null) { await _bookRepository.DeleteOne(currentClientProductBook.Id); } var operationSucceed = client.Charge(product.PriceInUAH); if (!operationSucceed) { return(PurchaseOperationResult.InsufficientFunds); } var store = await _productRepository.GetStoreOfSpecificProduct(product.Id); store.GetProfit(product.PriceInUAH); product.ChangeStatus(ProductStatus.Purchased); var purchase = new Purchase(Guid.NewGuid(), clientId, productId, product.PriceInUAH); await _clientRepository.UpdateOne(client); await _storeRepository.UpdateOne(store); await _productRepository.UpdateOne(product); await _purchaseRepository.AddOne(purchase); return(PurchaseOperationResult.Success); } return(PurchaseOperationResult.Fail); }
public void HandleOperation(Guid customerId, Guid productId, bool useCashBack, bool usePoints) { CustomValidator.ValidateId(customerId); CustomValidator.ValidateId(productId); var customer = _customerRepository.GetOne(customerId); var product = _productRepository.GetOne(productId); if (customer == null || product == null) { return; } var books = _bookRepository.GetCustomerBooks(customer.Id); var bookToDel = books.FirstOrDefault(b => b.ProductId.Equals(product.Id)); if (product.ProductStatus.Equals(ProductStatus.Booked) && bookToDel != null) { _bookRepository.DeleteOne(bookToDel.Id); } var priceToCompareWith = product.PriceInUAH - product.PriceInUAH * _propertyGetter.GetProperty <int>(EntityNames.Customer, nameof(VipCustomer.DiscountPercent), EntityNames.CustomerId, customer.Id) / 100; var customerCashBack = _propertyGetter.GetProperty <double>(EntityNames.Customer, nameof(VipCustomer.CashBack), EntityNames.CustomerId, customer.Id); bool res; if (usePoints) { priceToCompareWith = 0; res = customer.Charge(product.PriceInUAH, false, true); } else if (useCashBack) { if (customerCashBack > priceToCompareWith) { priceToCompareWith = 0; } if (customerCashBack < priceToCompareWith) { priceToCompareWith -= customerCashBack; } res = customer.Charge(product.PriceInUAH, true, false); } else { res = customer.Charge(product.PriceInUAH, false, false); } if (!res) { return; } var store = _productRepository.GetStoreOfSpecificProduct(product.Id); store.GetProfit(priceToCompareWith); product.ChangeStatus(ProductStatus.Purchased); var purchase = new Purchase(Guid.NewGuid(), customerId, productId, priceToCompareWith); _customerRepository.UpdateOne(customer); _storeRepository.UpdateOne(store); _productRepository.UpdateOne(product); _purchaseRepository.AddOne(purchase); }