using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Data.SqlClient;
using Utilities;
namespace Core2WebAPI.Controllers
{
[Produces("application/json")]
[Route("api/CustomerService")]
public class CustomerServiceController : Controller
{
// This method retrieves all the records from the Customer
table
[HttpGet] // GET api/CustomerService/
[HttpGet("GetCustomers")] // GET api/CustomerService/GetCustomers
public List<Customer> GetCustomers()
{
List<Customer> customerList = new List<Customer>();
DBConnect objDB
= new DBConnect();
String strSQL = "SELECT
* FROM Account";
DataSet myDS
= objDB.GetDataSet(strSQL);
int count = myDS.Tables[0].Rows.Count;
for (int i = 0; i < count; i++)
{
Customer cust = new Customer();
cust.Name
= objDB.GetField("CustomerName", i).ToString();
cust.Address
= objDB.GetField("Address", i).ToString();
cust.City
= objDB.GetField("City", i).ToString();
cust.State
= objDB.GetField("State", i).ToString();
cust.Zip
= objDB.GetField("Zip", i).ToString();
cust.Email
= objDB.GetField("Email", i).ToString();
customerList.Add(cust);
}
return customerList;
}
// This method accepts a Customer object and creates a new
record based on the values
// set for the
object passed into the method.
[HttpPost()] // POST api/CustomerService/
[HttpPost("AddCustomer")] // POST api/CustomerService/AddCustomer/
public Boolean AddCustomer([FromBody]Customer cust)
{
if (cust != null)
{
DBConnect objDB
= new DBConnect();
SqlCommand objCommand
= new SqlCommand();
objCommand.CommandType
= CommandType.StoredProcedure;
objCommand.CommandText
= "CreateNewAccount";
objCommand.Parameters.AddWithValue("@theName", cust.Name);
objCommand.Parameters.AddWithValue("@theAddress", cust.Address);
objCommand.Parameters.AddWithValue("@theCity", cust.City);
objCommand.Parameters.AddWithValue("@theState", cust.State);
objCommand.Parameters.AddWithValue("@theZip", cust.Zip);
objCommand.Parameters.AddWithValue("@theEmail", cust.Email);
objCommand.Parameters.AddWithValue("@theBalance", 0);
int retVal = objDB.DoUpdateUsingCmdObj(objCommand);
if (retVal
> 0)
return true;
else
return false;
}
else
{
return false;
}
}
// This method receives a name for a customer and returns a
Customer object with the field values from the database record.
// This method returns only the first occurrence of a customer
name.
[HttpGet("GetCustomerByName/{name}")] // GET api/CustomerService/GetCustomerByName/
public Customer GetCustomerByName(String name)
{
Customer cust = null;
DBConnect objDB
= new DBConnect();
SqlCommand objCommand
= new SqlCommand();
objCommand.CommandType
= CommandType.StoredProcedure;
objCommand.CommandText
= "GetAccountByName";
objCommand.Parameters.AddWithValue("@theName", name);
DataSet myDS
= objDB.GetDataSetUsingCmdObj(objCommand);
if (myDS.Tables[0].Rows.Count > 0)
{
cust = new Customer();
cust.Name
= objDB.GetField("CustomerName", 0).ToString();
cust.Address = objDB.GetField("Address", 0).ToString();
cust.City
= objDB.GetField("City", 0).ToString();
cust.State
= objDB.GetField("State", 0).ToString();
cust.Zip
= objDB.GetField("Zip", 0).ToString();
cust.Email
= objDB.GetField("Email", 0).ToString();
}
return cust;
}
// This method receives a name for a customer and returns a
Customer object with the field values from the database.
// This method returns an ArrayList
of Customer objects that represents all the customers with a given name.
[HttpGet("GetCustomersByName/{name}")] // GET api/CustomerService/GetCustomersByName/
public List<Customer> GetCustomersByName(String name)
{
List<Customer> customerList = new List<Customer>();
DBConnect objDB
= new DBConnect();
SqlCommand objCommand
= new SqlCommand();
objCommand.CommandType
= CommandType.StoredProcedure;
objCommand.CommandText
= "GetAccountByName";
objCommand.Parameters.AddWithValue("@theName", name);
DataSet myDS
= objDB.GetDataSetUsingCmdObj(objCommand);
int count = myDS.Tables[0].Rows.Count;
for (int i = 0; i < count; i++)
{
Customer cust = new Customer();
cust.Name
= objDB.GetField("CustomerName", i).ToString();
cust.Address
= objDB.GetField("Address", i).ToString();
cust.City
= objDB.GetField("City", i).ToString();
cust.State
= objDB.GetField("State", i).ToString();
cust.Zip
= objDB.GetField("Zip", i).ToString();
cust.Email
= objDB.GetField("Email", i).ToString();
customerList.Add(cust);
}
return customerList;
}
}
}