public void AddOrderItemToOrderTest()
 {
     ClientOrder testOrder = new ClientOrder(1)
     {
         PlacingDate = DateTime.Now,
         Status = Status.New,
         Products = new System.ComponentModel.BindingList<ClientOrderItem>()
     };
     Product testProduct = new Product()
     {
         Id = 1,
         Name = "Test product 1",
         Price = new ProductPrice()
         {
             Price = 100
         }
     };
     int value = 10;
     presenter.CommandList.Clear();
     mockCustomerEditOrderView.Order = testOrder;
     presenter.AddOrderItemToOrder(testProduct, value, testOrder);
     Assert.AreEqual(presenter.CommandList[0].Entity, mockCustomerEditOrderView.Order.Products[0]);
     Assert.AreEqual(presenter.CommandList[0].CommandType, CommandType.Create);
     Assert.AreEqual(presenter.CommandList[1].Entity, mockCustomerEditOrderView.Order.Products[0]);
     Assert.AreEqual(presenter.CommandList[1].CommandType, CommandType.Update);
     Assert.AreEqual(presenter.CommandList[1].FieldName, "Count");
     Assert.AreEqual(presenter.CommandList[1].Value, value);
 }
        public void AddOrderItemToOrder(Product product, int count, ClientOrder order)
        {
            if(product==null)
                throw new ArgumentException("A product with the specified ID not found", "productId");
            if (count <= 0)
                throw new ArgumentException("Count should be greatest then zero", "count");

                ClientOrderItem existingOrderItem = order.Products.FirstOrDefault(p => p.Id == product.Id);
                if (existingOrderItem != null)
                {
                    existingOrderItem.Count += count;
                    Command command = CommandList.FirstOrDefault(c => c.Entity == existingOrderItem && c.FieldName == "Count" && c.CommandType == CommandType.Update);
                    if (command != null)
                    {
                        command.Value = existingOrderItem.Count;
                    }
                    else
                    {
                        CommandList.AddUpdateOrderItemCommand(existingOrderItem, "Count", existingOrderItem.Count);
                    }
                    order.Products.ResetItem(order.Products.IndexOf(existingOrderItem));
                }
                else
                {
                    ClientOrderItem orderItem = new ClientOrderItem(product.Id);
                    orderItem.Count = count;
                    orderItem.Price = product.Price.Price;
                    orderItem.Name = product.Name;
                    order.Products.Add(orderItem);
                    CommandList.AddCreateOrderItemCommand(orderItem);
                    CommandList.AddUpdateOrderItemCommand(orderItem, "Count", orderItem.Count);
                }
        }
 public CommandListParser(IContract contract, CommandList commandList, ClientOrder order, User user)
 {
     this.contract = contract;
     this.commandList = commandList;
     this.order = order;
     this.user = user;
 }
 /// <summary>
 /// Gets the otrder items list of the specified order
 /// </summary>
 /// <param name="orderID">Order ID</param>
 /// <returns>The list of order items</returns>
 public List<ClientOrderItem> GetOrderItems(ClientOrder order)
 {
     if (customerOrderView.Orders.FirstOrDefault(o => o == order) != null)
     {
         if (order.Products == null && order.Id != 0)
             FillOrderItemsList(order.Id);
         return customerOrderView.Orders.First(oi => oi.Id == order.Id).Products.ToList();
     }
     else
     {
         throw new ArgumentException("The specified order is not at the order list");
     }
 }
        public void GetOrderItemsTest()
        {
            ClientOrder testOrder1 = new ClientOrder() { Id = 1 };
            ClientOrder testOrder2 = new ClientOrder() { Id = 2 };
            List<ClientOrder> testOrders = new List<ClientOrder>() { testOrder1, testOrder2 };
            mockCustomerOrdersView.Orders.Returns(testOrders);
            OrderItem testOrderItem1 = new OrderItem()
            {
                Id = 0,
                Count = 2,
                Product = new Product()
                    {
                        Id = 1,
                        Name = "Test Product 1",
                        Price = new ProductPrice()
                            {
                                Price = 200
                            }
                    }
            };
            OrderItem testOrderItem2 = new OrderItem()
            {
                Id = 1,
                Count = 5,
                Product = new Product()
                {
                    Id = 2,
                    Name = "Test Product 2",
                    Price = new ProductPrice()
                    {
                        Price=10
                    }

                }
            };
            List<OrderItem> testOrderItems = new List<OrderItem>() { testOrderItem1, testOrderItem2 };
            mockContract.GetOrderItems(Arg.Any<int>()).Returns(testOrderItems);
            presenter.GetOrderItems(mockCustomerOrdersView.Orders[0]);
            Assert.AreEqual(mockCustomerOrdersView.Orders, testOrders);
            Assert.AreEqual(mockCustomerOrdersView.Orders[1].Products, null);
            Assert.AreEqual(mockCustomerOrdersView.Orders[0].Products.Count, testOrder1.Products.Count);
            Assert.AreEqual(mockCustomerOrdersView.Orders[0].Products[0], testOrder1.Products[0]);
            Assert.AreEqual(mockCustomerOrdersView.Orders[0].Products[1], testOrder1.Products[1]);
        }
 public static ClientOrder TranslateToClientOrder(Order order)
 {
     if (order != null)
     {
         ClientOrder clientOrder = new ClientOrder(order.Id);
         clientOrder.PlacingDate = order.PlacingDate;
         clientOrder.Status = order.Status;
         if (order.Items != null)
         {
             clientOrder.Products = new System.ComponentModel.BindingList<ClientOrderItem>();
             foreach (var item in order.Items)
             {
                 clientOrder.Products.Add(TranslateToClientOrderItem(item));
             }
         }
         return clientOrder;
     }
     return null;
 }
 public static Order TranslateToOrder(ClientOrder clientOrder, User user)
 {
     if (clientOrder != null)
     {
         Order order = new Order();
         order.Id = clientOrder.Id;
         order.PlacingDate = clientOrder.PlacingDate;
         order.Status = clientOrder.Status;
         order.User = user;
         if (clientOrder.Products != null)
         {
             order.Items = new List<OrderItem>();
             for (int i = 0; i < clientOrder.Products.Count; i++)
             {
                 order.Items.Add(TranslateToOrderItem(clientOrder.Products[i], order));
             }
         }
         return order;
     }
     return null;
 }
 void ParseClientOrderCommand(Command command)
 {
     if (command.Entity as ClientOrder == null)
         throw new ArgumentException("Command has incorrect entity type. The entity should be ClientOrder", "command");
     switch (command.CommandType)
     {
         case CommandType.Create:
             {
                 Order tmpOrder = EntitiesTranslator.TranslateToOrder((ClientOrder)command.Entity,user);
                 tmpOrder.Items.Clear();
                 ClientOrder newOrder = EntitiesTranslator.TranslateToClientOrder(contract.SaveNewOrder(tmpOrder));
                 ((ClientOrder)command.Entity).Id = newOrder.Id;
                 ((ClientOrder)command.Entity).PlacingDate = newOrder.PlacingDate;
                 ((ClientOrder)command.Entity).Status = newOrder.Status;
                 if (order == null)
                     order = newOrder;
                 return;
             }
         case CommandType.Delete:
             {
                 Order tmpOrder = EntitiesTranslator.TranslateToOrder((ClientOrder)command.Entity,user);
                 tmpOrder.Items.Clear();
                 contract.RemoveOrder(tmpOrder);
                 return;
             }
         case CommandType.Update:
             {
                 if (command.FieldName == "Status")
                 {
                     contract.ChangeOrderStatus(command.Entity.Id, (Status)command.Value);
                 }
                 else
                 {
                     throw new UnknownFieldNameException("Parser cannot recognize the field " + command.FieldName);
                 }
                 return;
             }
     }
 }
 private void btnCreateNewOrder_Click(object sender, EventArgs e)
 {
     SelectedOrder = new ClientOrder();
     this.Close();
 }
 private void showOrderInfoToolStripMenuItem_Click(object sender, EventArgs e)
 {
     SelectedOrder = ((ClientOrder)gvOrders.GetFocusedRow());
     this.Close();
 }
 public static Order TranslateToOrder(ClientOrder clientOrder)
 {
     return TranslateToOrder(clientOrder, null);
 }
 public void TranslateToOrderTest()
 {
     ClientOrder clientOrder = new ClientOrder()
     {
         Id = 1,
         PlacingDate = DateTime.Now,
         Status = Status.New,
         Products = new System.ComponentModel.BindingList<ClientOrderItem>()
         {
             new ClientOrderItem(1)
             {
                 Count=5,
                 Name="Test product 1",
                 Price=100
             },
             new ClientOrderItem(2)
             {
                 Count=2,
                 Name="Test product 2",
                 Price=200
             }
         }
     };
     User customer = new User()
     {
         Id = 111,
         Login = "******",
         Name = "Name",
         Password = "******",
         Role = Role.Customer
     };
     Order order = EntitiesTranslator.TranslateToOrder(clientOrder, customer);
     Assert.AreEqual(order.Id,clientOrder.Id);
     Assert.AreEqual(order.PlacingDate,clientOrder.PlacingDate);
     Assert.AreEqual(order.User, customer);
     Assert.AreEqual(order.Status, clientOrder.Status);
     Assert.AreEqual(order.Items.Count, clientOrder.Products.Count);
     Assert.AreEqual(order.Items[0].Count, clientOrder.Products[0].Count);
     Assert.AreEqual(order.Items[0].Product.Name, clientOrder.Products[0].Name);
     Assert.AreEqual(order.Items[0].Product.Id, clientOrder.Products[0].Id);
     Assert.AreEqual(order.Items[1].Count, clientOrder.Products[1].Count);
     Assert.AreEqual(order.Items[1].Product.Name, clientOrder.Products[1].Name);
     Assert.AreEqual(order.Items[1].Product.Id, clientOrder.Products[1].Id);
 }
 void ParseClientOrderItemCommand(Command command, ClientOrder order)
 {
     if (command.Entity as ClientOrderItem == null)
         throw new ArgumentException("Command has incorrect entity type. The entity should be ClientOrderItem", "command");
     switch (command.CommandType)
     {
         case CommandType.Create:
             {
                 BindingList<ClientOrderItem> products = order.Products;
                 order.Products.Clear();
                 contract.SaveNewOrderItem(EntitiesTranslator.TranslateToOrderItem((ClientOrderItem)command.Entity, EntitiesTranslator.TranslateToOrder(order,user)));
                 order.Products = products;
                 return;
             }
         case CommandType.Delete:
             {
                 contract.RemoveOrderItem(contract.GetOrderItem(order.Id, command.Entity.Id));
                 return;
             }
         case CommandType.Update:
             {
                 if (command.FieldName == "Count")
                 {
                     contract.ChangeOrderItemCount(contract.GetOrderItem(order.Id, command.Entity.Id).Id, (int)command.Value);
                 }
                 return;
             }
     }
 }