static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("RECEIVER has started. \nPress any key to stop."); var factory = new ConnectionFactory() { UserName = "******", Password = "******", HostName = "localhost" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(QUEUE_NAME, false, false, false, null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var jsonString = Encoding.UTF8.GetString(body); NewOrderMessage newOrderMessage = JsonConvert.DeserializeObject <NewOrderMessage>(jsonString); Console.WriteLine($"Received{jsonString}"); using (var context = new FoodServiceContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.Order.Add(newOrderMessage.NewOrder); context.OrderCustomer.Add(newOrderMessage.NewOrderCustomer); context.OrderEmployee.Add(newOrderMessage.NewOrderEmployee); context.OrderItem.AddRange(newOrderMessage.NewOrderItems); context.SaveChanges(); transaction.Commit(); channel.BasicAck(ea.DeliveryTag, false); } catch (Exception e) { transaction.Rollback(); } } } }; try { channel.BasicConsume(QUEUE_NAME, false, consumer); Console.ReadKey(); } catch (Exception e) { throw e; } } }
public void RemoveProducts(FoodServiceContext context, ProductDto product) { if (!_CheckProduct(context, product)) { return; } _shoppingCartProducts.Remove(product); TotalCost -= product.Price; _CalcDiscount(Promo); }
public void CheckIsNotProductOutOfStock() { using (var context = new FoodServiceContext(_optionsBuilder.Options)) { context.Products.Add(new Product { Name = "sdsd", Quantity = 0 }); context.SaveChanges(); IProductService productService = new ProductService(context); Assert.False(productService.IsProductOutOfStock(context.Products.First().ProductId)); } }
public void GetAll() { using (var context = new FoodServiceContext(_optionsBuilder.Options)) { context.Products.Add(new Product { Name = "sdsd", Quantity = 2 }); context.SaveChanges(); IProductService productService = new ProductService(context); Assert.NotEmpty(productService.GetAll()); } }
public bool TryAppliedVoucher(FoodServiceContext context, string voucher) { var promo = context.Promotions.Where(p => p.Name.Equals(voucher)).FirstOrDefault(); if (promo == null) { return(false); } AppliedVoucher = voucher; Promo = promo; _CalcDiscount(promo); return(true); }
public bool AddProductToBasket(FoodServiceContext context, ProductDto product) { if (product.Quantity <= 0) { return(false); } if (!_CheckProduct(context, product)) { return(false); } _shoppingCartProducts.Add(product); TotalCost += product.Price; _CalcDiscount(Promo); return(true); }
public DashboardModel() { using (var context = new FoodServiceContext()) { Orders = context.Order.ToList(); OrderItems = context.OrderItem.ToList(); Customers = context.Customer.ToList(); Employees = context.Employee.ToList(); FoodItems = context.FoodItem.ToList(); Payments = context.Payment.ToList(); } var modelData = GetCreateModelData(); CreateOrderModel = new CreateOrderModel(modelData["Customers"], modelData["Employees"], modelData["Payments"], modelData["FoodItems"]); }
private IDictionary <string, List <SelectListItem> > GetCreateModelData() { using (var context = new FoodServiceContext()) { var customers = (from customer in context.Customer select customer).ToList(); var employees = (from employee in context.Employee select employee).ToList(); var payments = (from payment in context.Payment select payment).ToList(); var foodItems = (from foodItem in context.FoodItem select foodItem).ToList(); var dictionary = new Dictionary <string, List <SelectListItem> >() { { "Customers", customers.Select(a => new SelectListItem { Value = a.CustomerId.ToString(), Text = a.Email }).ToList() }, { "Employees", employees.Select(a => new SelectListItem { Value = a.EmployeeId.ToString(), Text = a.Email }).ToList() }, { "Payments", payments.Select(a => new SelectListItem { Value = a.PaymentId.ToString(), Text = $"{a.Code}, {a.Name}" }).ToList() }, { "FoodItems", foodItems.Select(a => new SelectListItem { Value = a.FoodItemId.ToString(), Text = $"{a.Name}, {a.UnitPrice}$" }).ToList() } }; return(dictionary); } }
public IActionResult AddNewOrder(CreateOrderModel createOrderModel) { var timestamp = DateTime.UtcNow; var newOrder = new Order { CustomerIdRef = Int32.Parse(createOrderModel?.CustomerSelected), EmployeeRef = Int32.Parse(createOrderModel?.EmployeeSelected), PaymentIdRef = Int32.Parse(createOrderModel?.PaymentSelected), Description = $"Ordered at {timestamp.ToShortDateString()} by {Int32.Parse(createOrderModel?.CustomerSelected)}. Employee: {Int32.Parse(createOrderModel?.EmployeeSelected)}", Status = (new OrderStatus { Status = OrderStatusEnum.Pending }).ToString(), Timestamp = timestamp, }; var newOrderCustomer = new OrderCustomer { CustomerIdRef = newOrder.CustomerIdRef, OrderIdRefNavigation = newOrder }; var newOrderEmployee = new OrderEmployee { EmployeeIdRef = newOrder.EmployeeRef, OrderIdRefNavigation = newOrder }; var newOrderItems = (from foodItem in createOrderModel?.FoodItemsSelected select new OrderItem { FoodItemIdRef = Int32.Parse(foodItem), Quantity = 1, Order = newOrder }).ToList(); var publisher = new Publisher.Publisher(); try { var newOrderMessage = new NewOrderMessage(newOrder, newOrderCustomer, newOrderEmployee, newOrderItems); publisher.Publish(newOrderMessage); string customerFullName = null; string employeeFullName = null; string paymentCode = null; decimal totalPrice = 0; using (var context = new FoodServiceContext()) { var customer = context.Customer.FirstOrDefault(x => x.CustomerId == newOrder.CustomerIdRef); customerFullName = $"{customer.FirstName}, {customer.LastName}"; var employee = context.Employee.FirstOrDefault(x => x.EmployeeId == newOrder.EmployeeRef); employeeFullName = $"{employee.FirstName}, {employee.LastName}"; var payment = context.Payment.FirstOrDefault(x => x.PaymentId == newOrder.PaymentIdRef); paymentCode = payment.Code; foreach (var orderItem in newOrderItems) { var foodItem = context.FoodItem.FirstOrDefault(x => x.FoodItemId == orderItem.FoodItemIdRef); totalPrice += foodItem.UnitPrice * orderItem.Quantity; } } var toAppend = $"<tr>" + $"<td>{customerFullName}</td>" + $"<td>{employeeFullName}</td>" + $"<td>{paymentCode}</td>" + $"<td>{newOrder.Status}</td>" + $"<td>{newOrder.Timestamp.ToString()}</td>" + $"<td>{totalPrice}</tr>"; return(Json(new { status = "OK", toAppend = toAppend })); } catch (Exception e) { return(Json(new { status = "ERROR" })); } }
public ProductService(FoodServiceContext context) { _context = context; }
public ShoppingCartController(FoodServiceContext context, ShoppingCartSingleton shoppingCartSingleton) { _context = context; _shoppingCartSingleton = shoppingCartSingleton; }
private bool _CheckProduct(FoodServiceContext context, ProductDto product) { return(context.Products.Any(p => p.ProductId == product.ProductId && p.Name.Equals(product.Name))); }