Ejemplo n.º 1
0
        /// <summary>
        /// Creates the service request.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <param name="order">The order.</param>
        /// <param name="product">The product.</param>
        /// <param name="cart">The cart.</param>
        /// <returns>The service request.</returns>
        private static ServerServiceRequest CreateServerServiceRequest(Customer customer, Order order, Product product, Cart cart)
        {
            var data = new ServerServiceRequest();

            data.Customer_CustID      = customer.custID;
            data.Customer_FirstName   = customer.firstName;
            data.Customer_LastName    = customer.lastName;
            data.Customer_PhoneNumber = customer.phoneNumber;

            data.Order_CustID    = order.custID;
            data.Order_OrderID   = order.orderID;
            data.Order_OrderDate = order.orderDate;
            data.Order_PoNumber  = order.poNumber;

            data.Product_ProdID     = product.prodID;
            data.Product_ProdName   = product.prodName;
            data.Product_ProdWeight = product.prodWeight;
            data.Product_Price      = product.price;
            data.Product_InStock    = product.inStock;

            data.Cart_OrderID  = cart.orderID;
            data.Cart_ProdID   = cart.prodID;
            data.Cart_Quantity = cart.quantity;

            return(data);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Issues the service delete.
        /// </summary>
        /// <param name="data">The data.</param>
        private void IssueServiceDelete(ServerServiceRequest data)
        {
            var js = new JavaScriptSerializer();

            var url    = new Uri(ClientConfiguration.ServerUrl.ToString() + data.ToUrl());
            var client = HttpWebRequest.Create(url);

            client.Method = "DELETE";
            client.GetRequestStream().Close();

            using (var responseStream = client.GetResponse().GetResponseStream())
                using (var reader = new StreamReader(responseStream))
                {
                    var content = reader.ReadToEnd();

                    try
                    {
                        var result       = js.Deserialize <JsonSuccess>(content);
                        var responseText = "Server Response: -" + result.Message;
                        Response.Redirect("/Pages/DeletePage.aspx?response=" + responseText);
                    }
                    catch (Exception)
                    {
                        var error        = js.Deserialize <JsonError>(content);
                        var responseText = "Server Response: -" + error.Message;
                        Response.Redirect("/Pages/DeletePage.aspx?response=" + responseText);
                    }
                }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles the Click event of the OrderDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <exception cref="System.Exception">Failure to process Order Drop Down</exception>
        protected void OrderDelete_Click(object sender, EventArgs e)
        {
            try
            {
                var data = new ServerServiceRequest();
                data.Order_OrderID = int.Parse(OrderDropDown.SelectedItem.Value);

                IssueServiceDelete(data);
            }
            catch (Exception ex)
            {
                Logger.GetInstance().Write(ex);
                throw new Exception("Failure to process Order Drop Down");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles the Click event of the CartDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        /// <exception cref="System.Exception">Failure to process Cart Drop Down</exception>
        protected void CartDelete_Click(object sender, EventArgs e)
        {
            try
            {
                var data  = new ServerServiceRequest();
                var split = CartDropDown.SelectedItem.Text.Split(',');
                data.Cart_OrderID = int.Parse(split[0]);
                data.Cart_ProdID  = int.Parse(split[1]);

                IssueServiceDelete(data);
            }
            catch (Exception ex)
            {
                Logger.GetInstance().Write(ex);
                throw new Exception("Failure to process Cart Drop Down");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Handler for a get (search) request.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns>The JSON encoded request results</returns>
        public JsonResult Get(string[] values)
        {
            try
            {
                if (values == null)
                {
                    throw new Exception("No table/column/values given for GET");
                }

                values = values[0].Split('/');

                bool purchaseOrder = false;
                if (values[0].Equals("true", StringComparison.CurrentCultureIgnoreCase))
                {
                    purchaseOrder = true;
                    // Remove the purchase order item
                    values = values.Skip(1).ToArray();
                }
                else if (values[0].Equals("false", StringComparison.CurrentCultureIgnoreCase))
                {
                    purchaseOrder = false;
                    // Remove the purchase order item
                    values = values.Skip(1).ToArray();
                }

                var queries = TableQuery.ListQueriesFromPath(values);
                var data    = ServerServiceRequest.FromTableQueries(queries.Select(q => q.ToTableColumnValue()).ToList());

                var searcher = new DatabaseSearch(data, purchaseOrder);
                var results  = searcher.Search();

                return(Json(results, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Logger.GetInstance().Write(ex);
                return(Json(new JsonError(ex), JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handler for a delete request
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns>The JSON encoded delete request results</returns>
        public JsonResult Delete(string[] values)
        {
            try
            {
                if (values == null)
                {
                    throw new Exception("No table/column/values given for DELETE");
                }

                values = values[0].Split('/');

                var queries = TableQuery.ListQueriesFromPath(values);
                var data    = ServerServiceRequest.FromTableQueries(queries.Select(q => q.ToTableColumnValue()).ToList());

                new DatabaseUpdate(data).Delete();

                return(Json(new JsonSuccess("OK", "Successfully Deleted")));
            }
            catch (Exception ex)
            {
                Logger.GetInstance().Write(ex);
                return(Json(new JsonError(ex), JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Enforces the required insert fields filled.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        private bool EnforceRequiredInsertFieldsFilled(ServerServiceRequest data)
        {
            bool missingMandatoryField = false;

            if (customerRadio.Checked)
            {
                if (data.Customer_LastName == null)
                {
                    AppendClientError("Customer must have a lastName");
                    missingMandatoryField = true;
                }

                if (data.Customer_PhoneNumber == null)
                {
                    AppendClientError("Customer must have a phoneNumber");
                    missingMandatoryField = true;
                }
            }
            else if (productRadio.Checked)
            {
                if (data.Product_ProdName == null)
                {
                    AppendClientError("Product must have a prodName");
                    missingMandatoryField = true;
                }

                if (data.Product_Price == null)
                {
                    AppendClientError("Product must have a price");
                    missingMandatoryField = true;
                }

                if (data.Product_ProdWeight == null)
                {
                    AppendClientError("Product must have a prodWeight");
                    missingMandatoryField = true;
                }
            }
            else if (orderRadio.Checked)
            {
                if (data.Order_CustID == null)
                {
                    AppendClientError("Order must have a custID");
                    missingMandatoryField = true;
                }

                if (data.Order_OrderDate == null)
                {
                    AppendClientError("Order must have a orderDate");
                    missingMandatoryField = true;
                }
            }
            else if (cartRadio.Checked)
            {
                if (data.Cart_OrderID == null)
                {
                    AppendClientError("Cart must have a orderID");
                    missingMandatoryField = true;
                }

                if (data.Cart_ProdID == null)
                {
                    AppendClientError("Cart must have a prodID");
                    missingMandatoryField = true;
                }

                if (data.Cart_Quantity == null)
                {
                    AppendClientError("Cart must have a quantity");
                    missingMandatoryField = true;
                }
            }

            return(!missingMandatoryField);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Populates the fields.
        /// </summary>
        /// <param name="data">The data.</param>
        private void PopulateFields(ServerServiceRequest data)
        {
            // Customers
            try
            {
                data.Customer_CustID = (String.IsNullOrWhiteSpace(CustId.Text)) ? null : (int?)int.Parse(CustId.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Customer custID");
            }

            data.Customer_FirstName   = (String.IsNullOrWhiteSpace(FirstName.Text)) ? null : FirstName.Text;
            data.Customer_LastName    = (String.IsNullOrWhiteSpace(LastName.Text)) ? null : LastName.Text;
            data.Customer_PhoneNumber = (String.IsNullOrWhiteSpace(Phonenumber.Text)) ? null : Phonenumber.Text;

            // Orders
            try
            {
                data.Order_CustID = (String.IsNullOrWhiteSpace(OrderCustId.Text)) ? null : (int?)int.Parse(OrderCustId.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into the Order custID");
            }

            try
            {
                data.Order_OrderID = (String.IsNullOrWhiteSpace(OrderId.Text)) ? null : (int?)int.Parse(OrderId.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Order orderID");
            }

            data.Order_OrderDate = (String.IsNullOrWhiteSpace(OrderDate.Text)) ? null : OrderDate.Text;
            data.Order_PoNumber  = (String.IsNullOrWhiteSpace(PoNumber.Text)) ? null : PoNumber.Text;

            // Products
            try
            {
                data.Product_ProdID = (String.IsNullOrWhiteSpace(ProdId.Text)) ? null : (int?)int.Parse(ProdId.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Product prodID");
            }

            try
            {
                data.Product_Price = (String.IsNullOrWhiteSpace(Price.Text)) ? null : (double?)double.Parse(Price.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Product price");
            }

            try
            {
                data.Product_ProdWeight = (String.IsNullOrWhiteSpace(ProdWeight.Text)) ? null : (int?)int.Parse(ProdWeight.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Product prodWeight");
            }

            if (RequestType != PageType.Search)
            {
                data.Product_InStock = (SoldOut.Checked) ? (byte)0 : (byte)1;
            }
            else
            {
                // Ignore the instock field when performing search
                data.Product_InStock = null;
            }

            data.Product_ProdName = (String.IsNullOrWhiteSpace(ProdName.Text)) ? null : ProdName.Text;

            // Carts
            try
            {
                data.Cart_OrderID = (String.IsNullOrWhiteSpace(CartOrderId.Text)) ? null : (int?)int.Parse(CartOrderId.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Cart orderID");
            }

            try
            {
                data.Cart_ProdID = (String.IsNullOrWhiteSpace(CartProdId.Text)) ? null : (int?)int.Parse(CartProdId.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Cart prodID");
            }

            try
            {
                data.Cart_Quantity = (String.IsNullOrWhiteSpace(Quantity.Text)) ? null : (int?)int.Parse(Quantity.Text);
            }
            catch (Exception)
            {
                AppendClientError("Please enter a number into Cart quantity");
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Handles the Click event of the execute control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void execute_Click(object sender, EventArgs e)
        {
            ClientErrors.Text = "Client Errors<br>...";
            ServerErrors.Text = "Server Errors<br>...";

            // Populate data for service call
            var data = new ServerServiceRequest();

            PopulateFields(data);

            // Bail out, if we have errors
            if (HasClientErrors())
            {
                return;
            }

            // Make sure that the user has not selected both customers and products
            var hasCustomerField = (data.Customer_CustID != null ||
                                    data.Customer_FirstName != null ||
                                    data.Customer_LastName != null ||
                                    data.Customer_PhoneNumber != null);

            var hasProductField = (data.Product_ProdID != null ||
                                   data.Product_ProdName != null ||
                                   data.Product_ProdWeight != null ||
                                   data.Product_Price != null
                                   // Ignore the instock field...
                                   );

            if (hasCustomerField && hasProductField)
            {
                AppendClientError("Cannot have <b>both</b> customer and product fields filled");
                return;
            }

            if (GeneratePurchaseOrder.Checked)
            {
                // Different fields are mandatory when generate P.O. is checked
                if (data.Customer_CustID == null && data.Customer_LastName == null &&
                    data.Customer_FirstName == null && data.Order_OrderID == null &&
                    data.Order_PoNumber == null && data.Order_OrderDate == null)
                {
                    AppendClientError(@"At least one of the following fields must be filled: Customer custID, Customer lastName, <br>
Customer firstName, Order orderID, Order poNumber or Order orderDate when 'Generate P.O.' is checked");
                    return;
                }
            }

            if (!data.HasOneOrMoreFieldsWithAValue())
            {
                AppendClientError("Please fill in at least one of the fields");
                return;
            }

            if (RequestType == PageType.Insert)
            {
                if (!EnforceRequiredInsertFieldsFilled(data))
                {
                    return;
                }
            }

            // Check maximum lengths, because Linq to SQL generates crappy messages on the server end
            DateTime parsed;

            if (data.Customer_PhoneNumber != null && !Regex.Match(data.Customer_PhoneNumber, @"^\d{3}-\d{3}-\d{4}$").Success)
            {
                AppendClientError("Please use the following format on phone numbers (xxx-xxx-xxxx)");
                return;
            }
            else if (data.Order_OrderDate != null && !DateTime.TryParseExact(data.Order_OrderDate, "MM-dd-yy",
                                                                             CultureInfo.CurrentCulture, DateTimeStyles.None, out parsed))
            {
                AppendClientError("Please use the following format for order dates (MM-DD-YY) and proper dates");
                return;
            }

            switch (RequestType)
            {
            case PageType.Search:
            {
                var purchaseOrder = GeneratePurchaseOrder.Checked;

                var url    = new Uri(ClientConfiguration.ServerUrl.ToString() + data.ToUrl(purchaseOrder));
                var client = HttpWebRequest.Create(url);
                client.Method = "GET";

                using (var responseStream = client.GetResponse().GetResponseStream())
                    using (var reader = new StreamReader(responseStream))
                    {
                        SearchContent = reader.ReadToEnd();

                        try
                        {
                            var js     = new JavaScriptSerializer();
                            var result = js.Deserialize <SearchResult>(SearchContent);

                            SearchResult = result;

                            if (purchaseOrder)
                            {
                                Server.Transfer("/Pages/PurchaseOrderPage.aspx");
                            }
                            else
                            {
                                Server.Transfer("/Pages/SearchResultsPage.aspx");
                            }
                        }
                        catch (Exception)
                        {
                            // Check if we have a Json Error
                            var js    = new JavaScriptSerializer();
                            var error = js.Deserialize <JsonError>(SearchContent);

                            AppendServerError(error.Message);
                        }
                    }

                break;
            }

            case PageType.Insert:
            {
                var url    = ClientConfiguration.ServerUrl;
                var client = HttpWebRequest.Create(url);
                client.Method = "POST";

                var js             = new JavaScriptSerializer();
                var json           = js.Serialize(data);
                var requestContent = Encoding.ASCII.GetBytes(json);

                client.ContentLength = requestContent.Length;

                var requestStream = client.GetRequestStream();
                requestStream.Write(requestContent, 0, requestContent.Length);
                requestStream.Close();

                using (var responseStream = client.GetResponse().GetResponseStream())
                    using (var reader = new StreamReader(responseStream))
                    {
                        var responseContent = reader.ReadToEnd();

                        try
                        {
                            var success = js.Deserialize <JsonSuccess>(responseContent);
                            WebFormTools.MessageBoxShow(this, success.Message);
                        }
                        catch (Exception)
                        {
                            var error = js.Deserialize <JsonError>(responseContent);
                            AppendServerError(error.Message);
                        }
                    }

                break;
            }

            case PageType.Update:
            {
                var url    = ClientConfiguration.ServerUrl;
                var client = HttpWebRequest.Create(url);
                client.Method = "PUT";

                var js             = new JavaScriptSerializer();
                var json           = js.Serialize(data);
                var requestContent = Encoding.ASCII.GetBytes(json);

                client.ContentLength = requestContent.Length;

                using (Stream requestStream = client.GetRequestStream())
                {
                    requestStream.Write(requestContent, 0, requestContent.Length);
                }

                using (var responseStream = client.GetResponse().GetResponseStream())
                    using (var reader = new StreamReader(responseStream))
                    {
                        var responseContent = reader.ReadToEnd();

                        try
                        {
                            var success = js.Deserialize <JsonSuccess>(responseContent);
                            WebFormTools.MessageBoxShow(this, success.Message);
                        }
                        catch (Exception)
                        {
                            var error = js.Deserialize <JsonError>(responseContent);
                            AppendServerError(error.Message);
                        }
                    }

                break;
            }
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DatabaseSearch" /> class.
 /// </summary>
 /// <param name="serviceRequest">The service request.</param>
 /// <param name="purchaseOrder">if set to <c>true</c> [purchase order].</param>
 public DatabaseSearch(ServerServiceRequest serviceRequest, bool purchaseOrder = false)
 {
     this.Data          = serviceRequest;
     this.PurchaseOrder = purchaseOrder;
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DatabaseUpdate" /> class.
 /// </summary>
 /// <param name="data">The data.</param>
 public DatabaseUpdate(ServerServiceRequest data)
 {
     this.Data = data;
 }