// GET: Home/Create
 public ActionResult Create()
 {
     CupcakeModel Cupcake = new CupcakeModel();
     Cupcake = null;
     //return View();
     return PartialView("Partials/Create", Cupcake);
 }
        /// <summary>
        /// Method created for returing a Cupcake by Id
        /// </summary>
        /// <param name="CupcakeId"></param>
        /// <returns></returns>
        public static CupcakeModel GetCupcakeById(int CupcakeId)
        {
            CupcakeModel result = new CupcakeModel();

            SqlConnection connection = new SqlConnection(DatabaseHelper.ConnectionString);
            connection.Open();

            SqlCommand command = new SqlCommand("Get_Cupcake_ById", connection);
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter IdParam = command.Parameters.Add("@Id", SqlDbType.Int);
            IdParam.Direction = ParameterDirection.Input;
            IdParam.Value = CupcakeId;

            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    result.Id = Convert.ToInt32(dataReader["Id"]);
                    result.Name = dataReader["Name"].ToString();
                    result.Price = Convert.ToDecimal(dataReader["Price"]);
                    result.CreatedDate = Convert.ToDateTime(dataReader["CreatedDate"]);
                    result.ModifiedDate = Convert.ToDateTime(dataReader["ModifiedDate"]);
                    result.Description = dataReader["Description"].ToString();
                }
            }

            return result;
        }
 public int InsertCupcake(CupcakeModel Cupcake)
 {
     int Id = 0;
     try
     {
         Id = Repository.InsertCupcake(Cupcake);
     }
     catch (Exception ex)
     {
         throw;
     }
     return Id;
 }
 // GET: Home/Details/5
 public ActionResult Details(int id)
 {
     CupcakeModel Cupcake = new CupcakeModel();
     try
     {
         Cupcake = singleton.GetCupcakeById(id);
         return PartialView("Partials/Details", Cupcake);
     }
     catch
     {
         return View();
     }
 }
 public int SaveCupcake(CupcakeModel Cupcake)
 {
     int Id;
     try
     {
         Id = Repository.SaveCupcake(Cupcake);
     }
     catch (Exception ex)
     {
         throw;
     }
     return Id;
 }
        /// <summary>
        /// Method created for returning the Cupcakes
        /// </summary>
        /// <returns></returns>
        public static List<CupcakeModel> GetCupcakes()
        {
            List<CupcakeModel> result = new List<CupcakeModel>();

            SqlConnection connection = new SqlConnection(DatabaseHelper.ConnectionString);
            connection.Open();

            SqlCommand command = new SqlCommand("Get_Cupcake", connection);
            command.CommandType = CommandType.StoredProcedure;

            SqlDataReader dataReader = command.ExecuteReader();

            CupcakeModel Cupcake = null;

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    Cupcake = new CupcakeModel();

                    Cupcake.Id = Convert.ToInt32(dataReader["Id"]);
                    Cupcake.Name = dataReader["Name"].ToString();
                    Cupcake.Price = Convert.ToDecimal(dataReader["Price"]);
                    Cupcake.CreatedDate = Convert.ToDateTime(dataReader["CreatedDate"]);
                    Cupcake.ModifiedDate = Convert.ToDateTime(dataReader["ModifiedDate"]);
                    Cupcake.Description = dataReader["Description"].ToString();

                    result.Add(Cupcake);
                }
            }

            return result;
        }
        /// <summary>
        /// Method created for updating a Cupcake
        /// </summary>
        /// <param name="cupcake"></param>
        /// <returns></returns>
        public static int SaveCupcake(CupcakeModel cupcake)
        {
            SqlConnection connection = new SqlConnection(DatabaseHelper.ConnectionString);
            connection.Open();

            SqlCommand command = new SqlCommand("Update_Cupcake", connection);
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter IdParam = command.Parameters.Add("@Id", SqlDbType.Int);
            IdParam.Direction = ParameterDirection.Input;
            IdParam.Value = cupcake.Id;

            SqlParameter NameParam = command.Parameters.Add("@Name", SqlDbType.NVarChar);
            NameParam.Direction = ParameterDirection.Input;
            NameParam.Value = cupcake.Name;

            SqlParameter PriceParam = command.Parameters.Add("@Price", SqlDbType.Decimal);
            PriceParam.Direction = ParameterDirection.Input;
            PriceParam.Value = cupcake.Price;

            SqlParameter ModifiedDateParam = command.Parameters.Add("@Modified_On", SqlDbType.DateTime);
            ModifiedDateParam.Direction = ParameterDirection.Input;
            ModifiedDateParam.Value = cupcake.ModifiedDate;

            SqlParameter DescriptionParam = command.Parameters.Add("@Description", SqlDbType.NVarChar);
            DescriptionParam.Direction = ParameterDirection.Input;
            DescriptionParam.Value = cupcake.Description;

            int savedCupcakeID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    savedCupcakeID = Convert.ToInt32(commandResult);
                }
                else
                {
                    savedCupcakeID = cupcake.Id;
                }
            }
            catch (Exception ex)
            {
            }

            command.Dispose();
            connection.Close();
            connection.Dispose();

            return savedCupcakeID;
        }
        /// <summary>
        /// Method created for inserting a Cupcake
        /// </summary>
        /// <param name="cupcake"></param>
        /// <returns></returns>
        public static int InsertCupcake(CupcakeModel cupcake)
        {
            if (!String.IsNullOrEmpty(cupcake.Name) && !String.IsNullOrEmpty(cupcake.Description))
            {
                SqlConnection connection = new SqlConnection(DatabaseHelper.ConnectionString);
                connection.Open();

                SqlCommand command = new SqlCommand("Insert_Cupcake", connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter NameParam = command.Parameters.Add("@Name", SqlDbType.NVarChar);
                NameParam.Direction = ParameterDirection.Input;
                NameParam.Value = cupcake.Name;

                SqlParameter PriceParam = command.Parameters.Add("@Price", SqlDbType.Decimal);
                PriceParam.Direction = ParameterDirection.Input;
                PriceParam.Value = cupcake.Price;

                SqlParameter CreatedDateParam = command.Parameters.Add("@Created_On", SqlDbType.DateTime);
                CreatedDateParam.Direction = ParameterDirection.Input;
                CreatedDateParam.Value = cupcake.CreatedDate;

                SqlParameter ModifiedDateParam = command.Parameters.Add("@Modified_On", SqlDbType.DateTime);
                ModifiedDateParam.Direction = ParameterDirection.Input;
                ModifiedDateParam.Value = cupcake.ModifiedDate;

                SqlParameter DescriptionParam = command.Parameters.Add("@Description", SqlDbType.NVarChar);
                DescriptionParam.Direction = ParameterDirection.Input;
                DescriptionParam.Value = cupcake.Description;

                int newCupcakeID = Convert.ToInt32(command.ExecuteScalar());

                command.Dispose();
                connection.Close();
                connection.Dispose();

                return newCupcakeID;
            }
            else
            {
                return 0;
            }
        }
 // GET: Home/Edit/5
 public ActionResult Edit(int id)
 {
     CupcakeModel Cupcake = new CupcakeModel();
     Cupcake = singleton.GetCupcakeById(id);
     return PartialView("Partials/Edit", Cupcake);
 }
        public ActionResult Edit(CupcakeModel Cupcake)
        {
            try
            {
                Cupcake.ModifiedDate = DateTime.Now;
                singleton.SaveCupcake(Cupcake);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }