public DrinkTypesRepository(TwilioBaristaContext db) { _db = db; }
public OrderRepository(TwilioBaristaContext db) { _db = db; }
public ActionResult Create(Customer message) { using (var db = new TwilioBaristaContext()) { // Check whether message came from SMS or Messenger Source source; var address = ""; if (message.From.Contains("Messenger")) { source = (from b in db.Sources where b.Name.Equals("Facebook") select b).First(); address = message.To; } else { source = (from b in db.Sources where b.Address.Equals(message.To) select b).First(); address = message.To; } // Check to see if this customer has already placed an order db.Set <Customer>().AddOrUpdate(f => f.From, new Customer { From = message.From, To = message.To }); db.SaveChanges(); // get a reference to the customer which will definitely exist at this point var customer = db.Customers.First(c => c.From == message.From); //Check if this customer has an open order var currentOrder = customer.Orders?.Where(f => f.Fulfilled == false).Select(n => new { n.Name, n.OrderId }).ToList(); if (currentOrder?.Count > 0) { _twilioClient.SendMessage(address, message.From, $"We're still making you a {currentOrder[0].Name}. Check order #{currentOrder[0].OrderId} with the barista if you think there's something wrong."); return(Content("Order already exists")); } // Check if the order is valid against the mispell table var drink = DrinkTypeRepository.SelectAll() .AsQueryable() .Include(m => m.Drink) .Where(m => m.Name.Trim() == message.Body.Trim()); var drinkMatch = drink.Select(p => p.Drink.Name).FirstOrDefault(); string response; if (drink.Any()) { // Create order var order = new Order { Customer = customer, Name = drinkMatch, Source = source }; db.Orders.Add(order); db.SaveChanges(); // Add to pusher _rthub.Trigger("orders", "order", new { id = order.OrderId, product = drinkMatch, message = message.Body }); // Dashboard var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; _rthub.Trigger("orders", "dashboard", new { source = order.Source.Name, type = drinkMatch, time = unixTimestamp }); response = $"Thanks for ordering a {drinkMatch} from the Twilio powered Coffee Shop. We'll text you back when it's ready. In the mean time check out this repo https://github.com/mplacona/TwilioBarista if you want too see how we built this app."; _twilioClient.SendMessage(address, message.From, response); return(Content("New order added")); } // We don't have that drink response = $"Seems like your order of {message.Body} is not something we can serve. Possible orders are {string.Join(", ", DrinkRepository.SelectAll().Select(m => m.Name).Distinct())}"; _twilioClient.SendMessage(address, message.From, response); return(Content(response)); } }