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 Index()

        {

            ViewData["Programmer"] = "Christopher M. Pascucci";

 

            // Return the Razor View "Index.cshtml"

            return View();

        }

       

        public IActionResult AddCar()

        {

            return View(new Car());

        }

 

        [HttpPost]

        public IActionResult AddCar(Car theCar)

        {

            if (theCar != null)

            {

                DBConnect objDB = new DBConnect();

                SqlCommand command = new SqlCommand();

                command.CommandType = CommandType.StoredProcedure;

                command.CommandText = "StoreCar";

 

                command.Parameters.AddWithValue("@VIN", theCar.VIN);

                command.Parameters.AddWithValue("@Make", theCar.Make);

                command.Parameters.AddWithValue("@Model", theCar.Model);

                command.Parameters.AddWithValue("@Year", theCar.Year);

                command.Parameters.AddWithValue("@Color", theCar.Color);

                command.Parameters.AddWithValue("@Value", theCar.Value);

                command.Parameters.AddWithValue("@ImageUrl", theCar.ImageUrl);

 

                int result = objDB.DoUpdate(command);

 

                if (result > 0)

                {

                    ViewData["Message"] = "Successfully stored the following car: ";

                }

                else

                {

                    ViewData["Message"] = "The car was not saved. Try again later...";

                }

            }

            else

            {

                ViewData["Message"] = "Error occurred. Try again later...";

            }

 

            // Return the Razor View "AddCar.cshtml"

            return View();

        }       

    }

}