Last active
September 5, 2018 09:57
-
-
Save zzandy/5efefd3e34b2600b57a04a7d116fc10c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// all required usings | |
public sealed class OrderManager: IOrderManager | |
{ | |
private IOrderValidator _validator; | |
private IDeliveryManager _deliveryManager; | |
private IRateExchange _rateExchange; | |
private IAccountManager _accountManager; | |
private IProductManager _productManager; | |
private IEmailService _emailService; | |
public OrderManager( | |
IOrderValidator validator, | |
IDeliveryManager deliveryManager, | |
IRateExchange rateExchange, | |
IAccountManager accountManager, | |
IProductManager productManager, | |
IEmailService emailService) | |
{ | |
_validator = validator; | |
_deliveryManager = deliveryManager; | |
_rateExchange = rateExchange; | |
_accountManager = accountManager; | |
_productManager = productManager; | |
_emailService = emailService; | |
} | |
public void ProcessOrder(Order order) | |
{ | |
if (!validator.Validate(order)) | |
{ | |
throw new Exception("Order is invalid"); | |
} | |
var user = _accountManager.GetUserById(order.UserId); | |
var mailToUser = GenerateMailMessage(order, user); | |
_emailService.Send(mailToUser); | |
if (order.IsDeliverable) | |
{ | |
_deliveryManager.Deliver(order, user); | |
} | |
} | |
private MailMessage GenerateMailMessage(Order order, User user) | |
{ | |
var mailBodyBuilder = new StringBuilder(); | |
mailBodyBuilder.AppendLine($"Hello, {user.Name}!"); | |
var rate = _rateExchange.GetRate(user.Currency); | |
var dbProducts = order.Products.Select(orderProduct => _productManager.GetProduct(orderProduct.Id)); | |
mailBodyBuilder.AppendLine(GetProductsText(dbProducts, rate)); | |
return new MailMessage( | |
_emailService.ShopEmail, | |
user.Email, | |
"Thank you for your order!", | |
mailBodyBuilder.ToString()); | |
} | |
private string GetProductsText(IEnumerable<Product> products, double rate) | |
{ | |
// Something processing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment