using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Web.Script.Serialization;  // needed for JSON serializers

using System.IO;                        // needed for Stream and Stream Reader

using System.Net;                       // needed for the Web Request

using Core2WebAPI;                      // needed for the Customer class

using System.Data;                      // needed for DataSet class

 

namespace WebAPIClient

{

    public partial class CustomerAPIExample : System.Web.UI.Page

    {

        String webApiUrl = "http://cis-iis2.temple.edu/users/pascucci/CIS3342/CoreWebAPI/api/CustomerService/";

 

        protected void Page_Load(object sender, EventArgs e)

        {

            lblDisplay.Text = "";

        }

 

        protected void btnDisplayCustomers_Click(object sender, EventArgs e)

        {

            // Create an HTTP Web Request and get the HTTP Web Response from the server.

            WebRequest request = WebRequest.Create(webApiUrl + "GetCustomers/");

            WebResponse response = request.GetResponse();

 

            // Read the data from the Web Response, which requires working with streams.

            Stream theDataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(theDataStream);

            String data = reader.ReadToEnd();

            reader.Close();

            response.Close();

 

            // Deserialize a JSON string that contains an array of JSON objects into a collection of Customer objects.

            JavaScriptSerializer js = new JavaScriptSerializer();

            List<Customer> customers = js.Deserialize<List<Customer>>(data);

 

            // Bind the list to the GridView to display all customers.

            gvCustomers.DataSource = customers;

            gvCustomers.DataBind();

        }

 

        protected void btnAddCustomer_Click(object sender, EventArgs e)

        {

            // Create an object of the Customer class which is avaialable through the web service reference and WSDL

            Customer customer = new Customer();

 

            customer.Name = txtName.Text;

            customer.Address = txtAddress.Text;

            customer.City = txtCity.Text;

            customer.State = txtState.Text;

            customer.Zip = txtZip.Text;

            customer.Email = txtEmail.Text;

 

            // Serialize a Customer object into a JSON string.

            JavaScriptSerializer js = new JavaScriptSerializer();

            String jsonCustomer = js.Serialize(customer);

 

            try

            {

                // Send the Customer object to the Web API that will be used to store a new customer record in the database.

                // Setup an HTTP POST Web Request and get the HTTP Web Response from the server.

                WebRequest request = WebRequest.Create(webApiUrl + "AddCustomer/");

                request.Method = "POST";

                request.ContentLength = jsonCustomer.Length;

                request.ContentType = "application/json";

 

                // Write the JSON data to the Web Request

                StreamWriter writer = new StreamWriter(request.GetRequestStream());

                writer.Write(jsonCustomer);

                writer.Flush();

                writer.Close();

 

                // Read the data from the Web Response, which requires working with streams.

                WebResponse response = request.GetResponse();

                Stream theDataStream = response.GetResponseStream();

                StreamReader reader = new StreamReader(theDataStream);

                String data = reader.ReadToEnd();

                reader.Close();

                response.Close();

 

                if (data == "true")

                    lblDisplay.Text = "The customer was successfully saved to the database.";

 

                else

                    lblDisplay.Text = "A problem occurred while adding the customer to the database. The data wasn't recorded.";

 

            }

            catch (Exception ex)

            {

                lblDisplay.Text = "Error: " + ex.Message;

            }

        }

 

        protected void btnFindCustomer_Click(object sender, EventArgs e)

        {

            // Find a record in the database using the web service method that returns a Customer object

            // Create an HTTP Web Request and get the HTTP Web Response from the server.

            WebRequest request = WebRequest.Create(webApiUrl + "GetCustomerByName/" + txtName.Text);

            WebResponse response = request.GetResponse();

 

            // Read the data from the Web Response, which requires working with streams.

            Stream theDataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(theDataStream);

            String data = reader.ReadToEnd();

            reader.Close();

            response.Close();

 

            // Deserialize a JSON string that contains an array of JSON objects into a Customer object.

            JavaScriptSerializer js = new JavaScriptSerializer();

            Customer customer = js.Deserialize<Customer>(data);

 

            if (customer != null)

            {

                // Use the object's values to populate the form's controls.

                txtName.Text = customer.Name;

                txtAddress.Text = customer.Address;

                txtCity.Text = customer.City;

                txtState.Text = customer.State;

                txtZip.Text = customer.Zip;

                txtEmail.Text = customer.Email;

            }

            else

            {

                lblDisplay.Text = "No record found by that customer name.";

            }

        }

 

        protected void btnFindCustomers_Click(object sender, EventArgs e)

        {

            // Retrieve the list containing all the customers returned by the web servier method via proxy.

            // Create an HTTP Web Request and get the HTTP Web Response from the server.

            WebRequest request = WebRequest.Create(webApiUrl + "GetCustomersByName/" + txtName.Text);

            WebResponse response = request.GetResponse();

 

            // Read the data from the Web Response, which requires working with streams.

            Stream theDataStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(theDataStream);

            String data = reader.ReadToEnd();

            reader.Close();

            response.Close();

 

            // Deserialize a JSON string that contains an array of JSON objects into a collection of Customer objects.

            JavaScriptSerializer js = new JavaScriptSerializer();

            List<Customer> customers = js.Deserialize<List<Customer>>(data);

          

            // Bind the list to the GridView to display all the customers with a given name.

            gvCustomers.DataSource = customers;

            gvCustomers.DataBind();

 

            txtName.Text = "";

            txtAddress.Text = "";

            txtCity.Text = "";

            txtState.Text = "";

            txtZip.Text = "";

            txtEmail.Text = "";

 

            if (customers.Count == 0)

            {

                lblDisplay.Text = "No records found by that name.";

            }

 

        }

    }

}