using Microsoft.AspNetCore.Mvc;

using MVCWebApp.Models;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Reflection;

using Utilities;

 

namespace MVCWebApp.Controllers

{

    public class CarsController : Controller

    {

       

        public IActionResult GetCars()

        {

            List<Car> cars = new List<Car>();

            Car car;

 

            DBConnect objDB = new DBConnect();

            DataSet ds = objDB.GetDataSet("SELECT * FROM Cars");

 

            foreach (DataRow record in ds.Tables[0].Rows)

            {

                car = new Car(record["Make"].ToString(),

                                record["Model"].ToString(),

                                int.Parse(record["Year"].ToString()),

                                double.Parse(record["Value"].ToString()),

                                record["VIN"].ToString());

                cars.Add(car);

            }

 

            // Store the collection of cars in the ViewBag to allow the View

            // to access and use the collection in generating the HTML content for the page.

            ViewBag.CarsList = cars;

 

            // Return the Razor View "DisplayCars.cshtml"

            return View("DisplayCars");       

 }

 

       

        [HttpPost]

        public IActionResult SearchCarsByMake(string searchKey)

        {

            List<Car> cars = new List<Car>();

            Car car;

 

            DBConnect objDB = new DBConnect();

            SqlCommand command = new SqlCommand();

            command.CommandType = CommandType.StoredProcedure;

            command.CommandText = "GetCarsByMaker";

 

            command.Parameters.AddWithValue("@theMake", searchKey);

            DataSet ds = objDB.GetDataSet(command);

 

            foreach (DataRow record in ds.Tables[0].Rows)

            {

                car = new Car(record["Make"].ToString(),

                                record["Model"].ToString(),

                                int.Parse(record["Year"].ToString()),

                                double.Parse(record["Value"].ToString()),

                                record["VIN"].ToString());

                cars.Add(car);

            }

 

            // Store the collection of cars in the ViewBag to allow the View

            // to access and use the collection in generating the HTML content for the page.

            ViewBag.CarsList = cars;

 

            // Return the Razor View "DisplayCars.cshtml"

            return View("DisplayCars");       

}

    }

}