예제 #1
0
        /// <summary>
        /// Creates an order.
        /// </summary>
        /// <param name="order"></param>
        /// <returns>string</returns>
        public string Create(string username, List <int> products)
        {
            Order        new_order;
            List <Order> orders;
            OrderDetail  order_detail;
            string       result;
            double       price = 0.0;

            //calculates the total price
            foreach (int id in products)
            {
                price += ctr_product.FindById(id).Price;
            }

            //creates a new order
            new_order = new Order
            {
                Price     = price,
                IsShipped = false,
                Username  = username
            };

            //saves new order in database
            result = db_order.CreateOrder(new_order);

            if (result.Equals("Success"))
            {
                //finds orders by username
                orders = FindByUsername(username);

                //gets the id for the newest order.
                foreach (Order order in orders)
                {
                    if (!order.IsShipped)
                    {
                        if (order.Id > new_order.Id)
                        {
                            new_order.Id = order.Id;
                        }
                    }
                }

                //creates orderdetails and saves them in the database.
                foreach (int id in products)
                {
                    order_detail = new OrderDetail
                    {
                        OrderId   = new_order.Id,
                        ProductId = id
                    };

                    result = ctr_orderdetail.Create(order_detail);
                }
            }

            return(result);
        }
예제 #2
0
        public void Success_CreateOrder()
        {
            //Arrange
            string result;
            Order  order;

            //Act
            order = new Order
            {
                Price     = 100.0,
                IsShipped = true,
                Username  = user.Username
            };

            result = db_order.CreateOrder(order);

            //Assert
            Assert.AreEqual("Success", result);
        }
예제 #3
0
        public void Initialize()
        {
            db_product     = DB_Product.GetInstance();
            db_order       = DB_Order.GetInstance();
            db_orderdetail = DB_OrderDetail.GetInstance();
            db_user        = DBCustomUser.GetInstance();
            int index = 0;

            //Creates test user
            user = new CustomUser
            {
                Username = "******",
                Password = "******",
                Email    = "ClassLibrary1 - Test OrderDetail Database - User",
                Wallet   = 5000000.0,
            };

            db_user.CreateUser(user);

            //loop for test products and test orders.
            while (index < 100)
            {
                //Creates test product
                Product product = new Product
                {
                    Name  = "ClassLibrary1 - Test OrderDetail Database - Product",
                    Price = 1500.0
                };

                //creates test order.
                Order order = new Order
                {
                    Price     = 100.0,
                    IsShipped = true,
                    Username  = user.Username
                };

                db_order.CreateOrder(order);
                db_product.CreateProduct(product);
                index++;
            }

            products     = db_product.FindProductByName("ClassLibrary1 - Test OrderDetail Database - Product");
            orders       = db_order.FindOrderByUsername(user.Username);
            orderdetails = new List <OrderDetail>();
        }