public async Task <IHttpActionResult> Order(int menuItem) { // this is painful, // but we can swap out concrete implementation // unfortunately, this is not DRY var loggingService = new WebApiLoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); try { var orderId = await service.OrderMenuItemAsync(menuItem); return(Created("http://mypizza.com/orders/" + orderId, orderId)); } catch (Exception ex) { loggingService.LogError(ex.Message); return(BadRequest("Oops, no pizza for you.")); } } }
public async Task PizzaOrderServiceOrderTest() { // this is still an integration test // dependent on credit card service // dependent on database // dependent on SMTP server // dependent on RabbitMQ var loggingService = new LoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); } }
public async Task PizzaOrderServiceOrderFakeTest() { // mock new Mock<ComplianceDbContext>(); var loggingService = new FakeLoggingService(); //db context requires a bit more effort to fake so let's abandon for now Assert.Inconclusive(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); } }
public PizzaOrderService( MyPizzaDbContext context, IPaymentService paymentService, IStoreQueueService storeQueueService, IOrderNotificationService orderNotificationService, ILoggingService loggingService) { this.context = context; this.paymentService = paymentService; this.storeQueueService = storeQueueService; this.orderNotificationService = orderNotificationService; this.loggingService = loggingService; }
static void Main(string[] args) { AutoMapperConfig.Initialize(); while (true) { Console.WriteLine("Press 1 for cheese, 2 for pepperoni. x to exit"); var result = Console.ReadLine(); if (result == "x") { return; } try { // this is painful, // but we can swap out concrete implementation var loggingService = new ConsoleLoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var task = service.OrderMenuItemAsync(int.Parse(result)); task.Wait(); var orderId = task.Result; Console.WriteLine("Your order number is " + orderId); } } catch (Exception ex) { Console.WriteLine(ex); } } }
public async Task PizzaOrderServiceOrderTest() { var loggingService = new LoggingService(); using (var context = new MyPizzaDbContext()) { var queueService = new StoreQueueService(); var paymentService = new PaymentService(); var notificationService = new OrderNotificationService(); var service = new PizzaOrderService( context, paymentService, queueService, notificationService, loggingService); var orderId = await service.OrderMenuItemAsync(1); Assert.IsTrue(orderId != 0); } }
public async Task <int> OrderAsync(PizzaOrderModel pizzaOrderDto) { try { // validate if (!pizzaOrderDto.Pizzas.Any()) { throw new InvalidOperationException("An order must contain at least 1 pizza."); } if (pizzaOrderDto.Pizzas.Any(pizza => pizza.Toppings.Sum(x => x.Percentage) != 100)) { throw new InvalidOperationException("The entire pizza must be covered by toppings."); } // calculate total var total = 10 * pizzaOrderDto.Pizzas.Count; //todo: add sales tax //todo: enable coupons and specials // charge credit card var apiKey = "MyPizza API Key"; var api = new StripeClient(apiKey); var card = Mapper.Map <CreditCard>(pizzaOrderDto.CreditCard); //dynamic response = api.CreateCharge( // total, // "usd", //todo: expand to other currencies // card); //todo: credit card processing isn't working yet dynamic response = new { IsError = false, Paid = true }; // check we got paid if (response.IsError || !response.Paid) { throw new Exception("Payment failed. :("); } // save order in database var pizzaOrder = Mapper.Map <PizzaOrder>(pizzaOrderDto); pizzaOrder.Total = total; using (var context = new MyPizzaDbContext()) { context.PizzaOrders.Add(pizzaOrder); await context.SaveChangesAsync(); } // queue message for the store to make the pizza var factory = new ConnectionFactory { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { var message = Encoding.UTF8.GetBytes(pizzaOrder.Id.ToString()); channel.BasicPublish("", "MyPizza", null, message); } } // send email to user var client = new SmtpClient("localhost", 25); var from = new MailAddress("*****@*****.**", "My Pizza"); var to = new MailAddress(pizzaOrderDto.EmailAddress); var mailMessage = new MailMessage(from, to) { Subject = "MyPizza.com Order " + pizzaOrder.Id, Body = "We are working on your cheesy awesome!" }; //todo: get an smtp server client.Send(mailMessage); return(pizzaOrder.Id); } catch (Exception ex) { // log exception var log = new EventLog { Source = "Application" }; log.WriteEntry(ex.Message, EventLogEntryType.Error); throw; } }