public async Task <IActionResult> Post([FromBody] CustomerProduct customerProduct)
        {
            Order order = OrderCartExists(customerProduct.CustomerId);

            if (order.Id == 0)
            {
                //Post METHOD GO CREATE AN EMPTY ORDER
                PostOrder(order, customerProduct.CustomerId);
                //reset order to be the order that gets created
            }
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())

                {
                    cmd.CommandText = @"INSERT INTO OrderProduct (OrderId, ProductId)
                                        OUTPUT INSERTED.Id
                                        VALUES (@OrderId, @productId)";

                    cmd.Parameters.Add(new SqlParameter("@OrderId", order.Id));
                    cmd.Parameters.Add(new SqlParameter("@ProductId", customerProduct.ProductId));
                    //(object) is a cast it's like a built in interface to treat these as an object, allowing you use "??" because order.Notes and DBNull are different types
                    //the "??" coaslesce operator specifies to use DBNull.Value as the backup if order.Notes is empty
                    //cmd.Parameters.Add(new SqlParameter("@notes", (object)order.Notes ?? DBNull.Value));

                    cmd.ExecuteScalar();
                    return(CreatedAtRoute("GetOrder", new { id = order.Id }, order));
                }
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Post([FromBody] CustomerProduct cusProduct)
        {
            //Check to see if shopping cart exists
            Order order = ShoppingCartExists(cusProduct.customerId);

            if (order.id == 0)
            {
                await PostingOrder(order, cusProduct.customerId);
            }

            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"INSERT INTO OrderProduct (OrderId, ProductId)
                                        OUTPUT INSERTED.Id
                                        VALUES (@OrderId, @ProductId)";

                    cmd.Parameters.Add(new SqlParameter("@OrderId", order.id));
                    cmd.Parameters.Add(new SqlParameter("@ProductId", cusProduct.productId));
                    await cmd.ExecuteScalarAsync();

                    return(CreatedAtRoute("GetOrder", new { order.id }, order));
                }
            }
        }
        public async Task <IActionResult> Post([FromBody] CustomerProduct customerProduct)
        {
            var cart = GetAllOrdersByCustomerIdCartTrue(customerProduct.CustomerId);

            // if cart is null then make cart which is an order

            // take cart and insert in orderproduct

            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    if (cart == null)
                    {
                        cmd.CommandText = @"INSERT INTO [Order] (CustomerId, UserPaymentTypeId)
                                            OUTPUT INSERTED.Id
                                            VALUES ( @CustomerId, @UserPaymentTypeId)";

                        cmd.Parameters.Add(new SqlParameter("@CustomerId", customerProduct.CustomerId));
                        cmd.Parameters.Add(new SqlParameter("@UserPaymentTypeId", DBNull.Value));

                        int newId = (int)cmd.ExecuteScalar();
                        cart = new Order()
                        {
                            Id         = newId,
                            CustomerId = customerProduct.CustomerId
                        };
                    }

                    //insert into orderproducts; we need the Id made on line 274
                    //orderId associated with the customerId from the customerProduct objec and UserPaymentNotNull



                    cmd.CommandText = @"INSERT INTO OrderProduct ( OrderId, ProductId)
                                            OUTPUT INSERTED.Id
                                            VALUES ( @OrderId, @ProductId)";


                    cmd.Parameters.Add(new SqlParameter("@OrderId", cart.Id));
                    cmd.Parameters.Add(new SqlParameter("@ProductId", customerProduct.ProductId));



                    int newerId = (int)cmd.ExecuteScalar();
                    customerProduct.Id = newerId;
                    return(CreatedAtRoute(new { id = cart.Id }, new Order {
                        CustomerId = customerProduct.CustomerId, Id = cart.Id
                    }));
                }
            }
        }
        public async Task <IActionResult> AddProductToOrder([FromBody] CustomerProduct customerProduct)
        {
            int orderId = GetCustomerId(customerProduct.CustomerId);

            if (orderId > 0)
            {
                await PostOrderProduct(orderId, customerProduct.ProductId);

                return(Ok());
            }
            else
            {
                await PostOrder(customerProduct.CustomerId);

                int newOrderId = GetCustomerId(customerProduct.CustomerId);
                await PostOrderProduct(newOrderId, customerProduct.ProductId);

                return(Ok());
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> AddProductToOrder([FromBody] CustomerProduct customerProduct)
        {
            // call method that determines whether the customer has an open order - return orderId
            int orderId = GetCurrentCustomerOrderId(customerProduct.CustomerId);

            if (orderId > 0)
            {
                // if orderId exists, post to OrderProduct table
                await PostOrderProduct(orderId, customerProduct.ProductId);

                return(Ok());
            }
            else
            {
                // if orderId is null, then create an order and post to OrderProduct
                await PostOrder(customerProduct.CustomerId);

                int newOrderId = GetCurrentCustomerOrderId(customerProduct.CustomerId);
                await PostOrderProduct(newOrderId, customerProduct.ProductId);

                return(Ok());
            }
        }
        public async Task <IActionResult> Post([FromBody] CustomerProduct custProd)
        {
            int orderId = 0;

            // 1. Find the customer's shopping cart if there is one.
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"SELECT Id, CustomerId, UserPaymentTypeId
                                        FROM [Order]
                                        WHERE CustomerId = @customerId AND UserPaymentTypeId IS NULL";
                    cmd.Parameters.Add(new SqlParameter("@customerId", custProd.CustomerId));

                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())
                    {
                        orderId = reader.GetInt32(reader.GetOrdinal("Id"));
                    }
                    reader.Close();
                }
            }

            // 2. If they don't have a shopping cart, create one.
            if (orderId == 0)
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"INSERT INTO [Order] (CustomerId)
                                            OUTPUT INSERTED.Id
                                            VALUES (@customerId)";
                        cmd.Parameters.Add(new SqlParameter("@customerId", custProd.CustomerId));

                        orderId = (int)cmd.ExecuteScalar();
                    }
                }
            }

            // 3. Add a record to OrderProduct
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @"INSERT INTO OrderProduct (OrderId, ProductId)
                                        VALUES (@orderId, @productId)";
                    cmd.Parameters.Add(new SqlParameter("@orderId", orderId));
                    cmd.Parameters.Add(new SqlParameter("@productId", custProd.ProductId));

                    cmd.ExecuteNonQuery();
                    return(Ok(new Order()
                    {
                        Id = orderId,
                        CustomerId = custProd.CustomerId
                    }));
                }
            }
        }