Пример #1
0
        public void Order_PersistanceTest()
        {
            var compare = new CompareObjects();
            compare.IgnoreObjectTypes = true;

            InAutoRollbackTransaction(uof =>
            {
                Order o = new Order
                {
                    Address = "A",
                    CustomerPhone = 123,
                    Date = DateTime.Now,
                    OrderDetails = new List<OrderDetail>(),
                    State = new State { StateValue = State.DONE },
                    User = new User { Address = "A", Email = "A", Name = "A", Password = "******", Phone = 1, Rights = 1 }
                };
                int x = ((List<Order>)uof.Db.Orders.FindAll()).Count;

                uof.Db.Orders.Insert(o);
                uof.Db.Commit();
                uof.Db.ObjectContext().DetachAll();

                int y = ((List<Order>)uof.Db.Orders.FindAll()).Count;

                Assert.IsTrue(y == x + 1);
            });
        }
Пример #2
0
        public OrderDTO ToSimpleDto(Order o)
        {
            OrderDTO oDto = new OrderDTO
            {
                OrderID = o.OrderID,
                Address = o.Address,
                CustomerPhone = o.CustomerPhone,
                Date = o.Date,
                State = (new StateAssembler()).ToSimpleDto(o.State)
            };

            oDto.OrderDetailsDTO = new List<OrderDetailDTO>();

            foreach (var od in o.OrderDetails)
            {
                List<OrderIngredientDTO> oIngDtos = new List<OrderIngredientDTO>();
                foreach (var ing in od.Ingredients)
                {
                    oIngDtos.Add(new OrderIngredientDTO
                    {
                        IngredientID = ing.IngredientID,
                        Name = ing.Ingredient.Name,
                        Price = ing.Ingredient.PricePerUnit,
                        Quantity = ing.Quantity,
                        ExtraWeight = ing.Ingredient.ExtraWeight,
                        NormalWeight = ing.Ingredient.NormalWeight
                    });
                }
                oDto.OrderDetailsDTO.Add(new OrderDetailDTO { OrderDetailID=od.OrderDetailID, Ingredients = oIngDtos, Size = new SizeDTO { SizeValue = od.Size.SizeValue } });
            }

            return oDto;
        }
Пример #3
0
        public void MakeOrderFromWeb(UpdateOrRemoveRequest<List<OrderInfoDTO>> req)
        {
            if (req.Data == null)
                throw PizzaServiceFault.Create(Messages.NO_DATA);

            db.inTransaction(uow =>
                {
                    UserDTO userDto = GetUser(req).Data;
                    if (!HasRights(userDto, 1))
                        throw PizzaServiceFault.Create(Messages.NO_PERMISSIONS);

                    User user = uow.Db.Users.Get(userDto.UserID);

                    List<OrderInfoDTO> data = req.Data;
                    List<OrderDetail> od = new List<OrderDetail>();

                    foreach (var item in data)
                    {
                        var sizeVal = Size.SMALL;
                        switch (item.Size)
                        {
                            case "normal":
                                sizeVal = Size.MEDIUM;
                                break;
                            case "big":
                                sizeVal = Size.GREAT;
                                break;
                            default:
                                break;
                        }
                        var size = uow.Db.Sizes.Find(sizeVal);
                        var ings = (List<Ingredient>)uow.Db.Ingredients.Find(item.Ingredients);
                        List<OrderIngredient> oings = new List<OrderIngredient>();

                        for (int i = 0; i < item.Quantities.Length; i++)
                        {
                            int quant = (item.Quantities[i] == "normal") ? ings[i].NormalWeight : ings[i].ExtraWeight;
                            oings.Add(new OrderIngredient
                            {
                                Ingredient = ings[i],
                                Quantity = quant
                            });
                        }

                        od.Add(new OrderDetail
                        {
                            Ingredients = oings,
                            Size = size
                        });
                    }

                    Order o = new Order
                    {
                        Address = userDto.Address,
                        CustomerPhone = userDto.Phone,
                        Date = DateTime.Now,
                        OrderDetails = od,
                        State = uow.Db.States.Find(State.NEW),
                        User = user
                    };

                    uow.Db.Orders.Insert(o);
                    uow.Db.Commit();

                    /*State st = uow.Db.States.Find(State.NEW);

                    List<OrderDetail> od = new List<OrderDetail>();
                    foreach (var ordDto in o.OrderDetailsDTO)
                    {
                        List<OrderIngredient> oIngs = new List<OrderIngredient>();
                        foreach (var oIng in ordDto.Ingredients)
                        {
                            oIngs.Add(new OrderIngredient
                            {
                                Ingredient = uow.Db.Ingredients.Get(oIng.IngredientID),
                                Quantity = oIng.Quantity
                            });
                        }
                        od.Add(new OrderDetail
                        {
                            Size = uow.Db.Sizes.Get(ordDto.Size.SizeID),
                            Ingredients = oIngs
                        });
                    }
                    Order order = new Order
                    {
                        Address = o.Address,
                        CustomerPhone = o.CustomerPhone,
                        Date = o.Date,
                        User = user,
                        State = st,
                        UserID = user.UserID,
                        OrderDetails = od
                    };

                    uow.Db.Orders.Insert(order);
                    uow.Db.Commit();*/
                });
        }
Пример #4
0
        public void MakeOrder(UpdateRequest<OrderDTO> req)
        {
            using (var db = new PizzaUnitOfWork())
            {
                db.inTransaction(uow =>
                    {
                        UserDTO userDto = GetUser(req).Data;
                        if (!HasRights(userDto, 1))
                            throw PizzaServiceFault.Create(Messages.NO_PERMISSIONS);

                        User user = uow.Db.Users.Get(userDto.UserID);
                        OrderDTO o = req.Data;
                        State st = uow.Db.States.Find(State.NEW);

                        List<OrderDetail> od = new List<OrderDetail>();
                        foreach (var ordDto in o.OrderDetailsDTO)
                        {
                            List<OrderIngredient> oIngs = new List<OrderIngredient>();
                            foreach (var oIng in ordDto.Ingredients)
                            {
                                oIngs.Add(new OrderIngredient
                                {
                                    Ingredient = uow.Db.Ingredients.Get(oIng.IngredientID),
                                    Quantity = oIng.Quantity
                                });
                            }
                            od.Add(new OrderDetail
                            {
                                Size = uow.Db.Sizes.Get(ordDto.Size.SizeID),
                                Ingredients = oIngs
                            });
                        }
                        Order order = new Order
                        {
                            Address = o.Address,
                            CustomerPhone = o.CustomerPhone,
                            Date = o.Date,
                            User = user,
                            State = st,
                            UserID = user.UserID,
                            OrderDetails = od
                        };

                        uow.Db.Orders.Insert(order);
                        uow.Db.Commit();
                    });
            }
        }
Пример #5
0
        /// <summary>
        /// Method thested by Order_RealisationTest() test.
        /// It changes order state.
        /// For more details about what is being tested see information about mentioned test method.
        /// </summary>
        /// <param name="o">Order which state is to be changed to In Realisation</param>
        /// <param name="uof">Unit of work associated with the tested database.</param>
        /// <returns></returns>
        private bool SetToInRealisation(Order o, TransactionUnitOfWork uof)
        {
            try
            {
                List<OrderIngredient> orderIngredients = new List<OrderIngredient>();
                foreach (var od in o.OrderDetails)
                {
                    foreach (var odIng in od.Ingredients)
                    {
                        orderIngredients.Add(odIng);
                    }
                }
                foreach (var odIng in orderIngredients)
                {
                    Ingredient i = db.Ingredients.Get(odIng.Ingredient.IngredientID);
                    if (i.StockQuantity - odIng.Quantity < 0)
                    {
                        return false;
                    }
                    i.StockQuantity -= odIng.Quantity;
                }

                db.Commit();
            }
            catch (Exception)
            {
                return false;
            }
            o.State.StateValue = State.IN_REALISATION;
            return true;
        }
Пример #6
0
        public void Order_RealisationTest()
        {
            int originalQuantity = 10;
            int inOrderQuantity = 6;
            Ingredient ing = new Ingredient { StockQuantity = originalQuantity, PricePerUnit = 0.1M, NormalWeight = 1, Name = "I", ExtraWeight = 2, Recipies = new List<Recipe>() };
            List<OrderIngredient> orings = new List<OrderIngredient>
            {
                new OrderIngredient{ Ingredient=ing, Quantity=inOrderQuantity}
            };
            List<OrderDetail> od = new List<OrderDetail>
            {
                new OrderDetail{ Size = new Size{SizeValue= Size.MEDIUM}, Ingredients = orings}
            };
            Order order = new Order
            {
                Address = "A",
                CustomerPhone = 123,
                Date = DateTime.Now,
                State = new State { StateValue = State.NEW },
                OrderDetails = od
                ,
                User = new User { Address = "A", Email = "A", Name = "B", Password = "******", Phone = 2, Rights = 2 }
            };

            InAutoRollbackTransaction(uof =>
                {
                    uof.Db.Orders.Insert(order);
                    uof.Db.Commit();
                    uof.Db.ObjectContext().DetachAll();

                    Order o = uof.Db.Orders.Get(order.OrderID);
                    Assert.IsTrue(SetToInRealisation(o, uof));
                    Assert.IsTrue(o.State.StateValue == State.IN_REALISATION);

                    Ingredient ingredient = null;
                    foreach (var orderDet in o.OrderDetails)
                    {
                        foreach (var odIng in orderDet.Ingredients)
                        {
                            ingredient = odIng.Ingredient;
                        }
                    }

                    Assert.IsTrue(ingredient.StockQuantity == originalQuantity - inOrderQuantity);

                    o.State.StateValue = State.NEW;
                    Assert.IsFalse(SetToInRealisation(o, uof));
                    foreach (var orderDet in o.OrderDetails)
                    {
                        foreach (var odIng in orderDet.Ingredients)
                        {
                            ingredient = odIng.Ingredient;
                        }
                    }
                    Assert.IsTrue(ingredient.StockQuantity == originalQuantity - inOrderQuantity);

                });
        }
Пример #7
0
 public void UpdateEntity(Order order, OrderDTO orderDTO)
 {
     order.StateID = orderDTO.State.StateID;
 }
Пример #8
0
        public void Order_Assembling_test()
        {
            List<OrderIngredient> orderIng = new List<OrderIngredient>
            {
                new OrderIngredient{ Ingredient = new Ingredient{ ExtraWeight=20,
                    IngredientID=1,
                    Name="TestIngredient",
                    NormalWeight = 10,
                    PricePerUnit=0.1M,
                    StockQuantity=200},
                    Quantity= 10 }
            };

            List<OrderDetail> od = new List<OrderDetail>
            {
                new OrderDetail{ Ingredients = orderIng, Size=new Size{ SizeID=1, SizeValue=Size.MEDIUM}}
            };

            Order o =
                new Order
                {
                    Address = "asd",
                    CustomerPhone = 123,
                    Date = DateTime.Now,
                    State = new State { StateValue = State.NEW },
                    OrderDetails = od
                };
            OrderDTO oDto = (new OrderAssembler()).ToSimpleDto(o);

            Assert.IsTrue(oDto.Address == o.Address);
            Assert.IsTrue(oDto.CustomerPhone == o.CustomerPhone);
            Assert.IsTrue(oDto.Date == o.Date);
            Assert.IsTrue(oDto.State.StateValue == o.State.StateValue);
            Assert.IsTrue(oDto.State.StateID == o.State.StateID);
            Assert.IsTrue(oDto.OrderDetailsDTO.Count == o.OrderDetails.Count);

            List<OrderDetail> orderDetails = new List<OrderDetail>();
            foreach (var ordDet in o.OrderDetails)
            {
                orderDetails.Add(ordDet);
            }
            List<OrderIngredient> orderIngs = new List<OrderIngredient>();
            foreach (var ordIng in orderDetails)
            {
                foreach (var item in ordIng.Ingredients)
                {
                    orderIngs.Add(item);
                }
            }

            for (int i = 0; i < oDto.OrderDetailsDTO.Count; i++)
            {
                for (int j = 0; j < oDto.OrderDetailsDTO[i].Ingredients.Count; j++)
                {
                    Assert.IsTrue(oDto.OrderDetailsDTO[i].Ingredients[j].IngredientID == orderIngs[i].IngredientID);
                    Assert.IsTrue(oDto.OrderDetailsDTO[i].Ingredients[j].Name == orderIngs[i].Ingredient.Name);
                    Assert.IsTrue(oDto.OrderDetailsDTO[i].Ingredients[j].Price == orderIngs[i].Ingredient.PricePerUnit);
                    Assert.IsTrue(oDto.OrderDetailsDTO[i].Ingredients[j].Quantity == orderIngs[i].Quantity);
                }
            }
        }