public void SellToyToUser(int toyId, int userId) { if (!this.Exists(toyId)) { throw new ArgumentException("There is no such toy with given id in the database!"); } if (!userService.Exists(userId)) { throw new ArgumentException("There is no such user with given id in the database!"); } var order = new Order() { PurchaseDate = DateTime.UtcNow, Status = OrderStatus.Done, UserId = userId }; var toyOrder = new ToyOrder() { ToyId = toyId, Order = order }; this.data.Orders.Add(order); this.data.ToyOrders.Add(toyOrder); this.data.SaveChanges(); }
public void SellToyToUser(int toyId, int userId) { if (!this.Exists(toyId) || !userService.Exists(userId)) { throw new InvalidOperationException("Given toy or user dont exist in the database"); } var order = new Order() { PurchaseDate = DateTime.UtcNow, Status = OrderStatus.Done, UserId = userId }; var toyOrder = new ToyOrder() { ToyId = toyId, Order = order }; this.data.Orders.Add(order); this.data.ToyOrders.Add(toyOrder); this.data.SaveChanges(); }
public void SellToysToUser(List <int> toyIds, int orderId) { var order = this.data.Orders.Find(orderId); if (order == null) { throw new ArgumentNullException(String.Format(OutputMessages.OrderNotExists, orderId)); } foreach (var id in toyIds) { var toy = this.data.Toys.Find(id); if (toy == null || toy.Quantity < 1) { throw new InvalidOperationException(String.Format(OutputMessages.ToyNotExists, id)); } var toyOrder = new ToyOrder() { ToyId = toy.Id, OrderId = orderId }; toy.Quantity--; this.data.ToyOrders.Add(toyOrder); order.Toys.Add(toyOrder); } this.data.SaveChanges(); }
public void SellToy(int toyId, int userId) { if (!Exists(toyId)) { throw new ArgumentException("No such a toy exists."); } if (!this.userService.Exists(userId)) { throw new ArgumentException("User not found."); } var order = new Order { UserId = userId, PurchaseDate = DateTime.Now, Status = Data.Models.Enumerations.OrderStatus.Sold, }; var toyOrder = new ToyOrder { Order = order, ToyId = toyId }; this.data.Orders.Add(order); this.data.ToyOrders.Add(toyOrder); this.data.SaveChanges(); }
public void NextAvailable_AvailableToyOrders_ReturnsCorrectTime(string toyOrderString, int year, int month, int day, int hour, int minute) { ToyOrder toy = new ToyOrder(toyOrderString); Elf elf = new Elf(1); DateTime shouldFinish = new DateTime(year, month, day, hour, minute, 0); elf.BuildToy(toy); Assert.AreEqual(shouldFinish, elf.nextAvailable, TestContext.CurrentContext.Test.Name); }
public void Productivity_VariousOrders_UpdatesProductivity(string toyOrderString, double sanctionedHours, double unsanctionedHours) { ToyOrder toy = new ToyOrder(toyOrderString); Elf elf = new Elf(1); elf.BuildToy(toy); double expectedProductivity = 1 * Math.Pow(Elf.FACTOR_SANCTIONED, sanctionedHours) * Math.Pow(Elf.FACTOR_UNSANCTIONED, unsanctionedHours); Assert.AreEqual(expectedProductivity, elf.productivity); }
public void Enque(ToyOrder order) { string json = JsonConvert.SerializeObject(order); byte[] messageBodyBytes = Encoding.UTF8.GetBytes(json); IBasicProperties props = _channel.CreateBasicProperties(); props.DeliveryMode = 2; lock (_channel) { _channel.BasicPublish(ExchangeName, order.ToyCategory.ToLowerInvariant(), props, messageBodyBytes); } }
public void ChooseToy_ToysWithDifferentDurationsAndStartTimes_PickEarliestToy() { List <string> orders = new List <string> (); orders.Add("1, 2014 1 4 8 0, 20"); orders.Add("2, 2014 1 5 7 0, 40"); orders.Add("3, 2014 1 4 9 0, 60"); orders.Add("4, 2014 1 2 9 0, 601"); orders.Add("5, 2014 1 2 12 0, 600"); ToyOrderBook.SetupOrderLists(); ToyOrderBook.AddNewOrdersToOrderBook(orders); ToyOrder shouldChooseToy = new ToyOrder("5, 2014 1 2 12 0, 600"); Elf elf = new Elf(1); ToyOrder chosenToy = elf.ChooseToy(); Assert.AreEqual(shouldChooseToy.iD, chosenToy.iD); }
public void ChooseToy_ToysWithDifferentDurationsAndStartTimes_PickQuickestAvailableToy() { List <string> orders = new List <string> (); orders.Add("1, 2014 1 1 8 0, 20"); orders.Add("2, 2014 1 1 7 0, 40"); orders.Add("3, 2014 1 1 9 0, 60"); orders.Add("4, 2014 1 1 12 0, 10"); orders.Add("5, 2014 1 1 9 0, 599"); ToyOrderBook.SetupOrderLists(); ToyOrderBook.AddNewOrdersToOrderBook(orders); ToyOrder shouldChooseToy; shouldChooseToy = new ToyOrder("1, 2014 1 1 8 0, 20"); Elf elf = new Elf(1); ToyOrder chosenToy = elf.ChooseToy(); Assert.AreEqual(shouldChooseToy.iD, chosenToy.iD); }
public void RemoveOrder_SingleOrder_ShouldHaveNoOrderAfter(int toAdd, int toRemove, int resultingCount) { ToyOrderBook.SetupOrderLists(); string newOrderString = "2,2014 1 1 0 1,5"; List <string> newOrderStrings = new List <string> (); for (int i = 0; i < toAdd; i++) { newOrderStrings.Add(newOrderString); } ToyOrderBook.AddNewOrdersToOrderBook(newOrderStrings); for (int j = 0; j < toRemove; j++) { ToyOrder toyOrder = ToyOrderBook.orderLists [0] [0]; ToyOrderBook.RemoveOrderFromBooks(toyOrder); } int ordersPresent = ToyOrderBook.CountAllOrdersInBook(); Assert.AreEqual(resultingCount, ordersPresent); }