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

        {

            // Return the Razor View "SaveCar.cshtml"

            return View();

        }

 

        [HttpPost]

        public IActionResult SaveCar(string vin, string make, string model, int year, string color, double value, string imageUrl)

        {

            if (vin != null && make != null && model != null)

            {

                DBConnect objDB = new DBConnect();

                SqlCommand command = new SqlCommand();

                command.CommandType = CommandType.StoredProcedure;

                command.CommandText = "StoreCar";

 

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

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

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

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

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

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

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

 

                int result = objDB.DoUpdate(command);

 

                if (result > 0)

                {

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

                                      " VIN " + vin + " | " + make + " | " + model + " | " +

                                      year + " | " + color + " | $" + value;

                }

                else

                {

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

                }

            }

            else

            {

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

            }

 

            // Return the Razor View "SaveCar.cshtml"

            return View();

        }

    }

}