/// <summary> /// Retrieves the form/data for the selected artist based off of Artist ID /// </summary> /// <returns></returns> public ActionResult UpdateArtist(int artistID) { ActionResult oResponse = null; // Ensures user is authenticated and is an Admin/SuperUser for Updating Priveleges if (Session["Email"] != null && (int)Session["Role"] >= 4) { var artistVM = new ArtistViewModel(); // Retrieves an artist by its ID IArtistDO artistDO = artistDA.ViewArtistByID(artistID); // Maps artistDO from Data Object to Presentation Objects for Updating artistVM.Artist = ArtistMapper.MapArtistDOtoPO(artistDO); //PopulateDropDownLists(artistVM); oResponse = View(artistVM); } else { // User doesn't have priveleges to this page redirect to home oResponse = RedirectToAction("Index", "Home"); } return(oResponse); }
// Maps Artist BO to Artist BO using the IArtistDO interface public static IArtistBO MapArtistDOtoBO(IArtistDO iArtist) { IArtistBO oArtist = new ArtistBO(); oArtist.ArtistBandID = iArtist.ArtistBandID; oArtist.ArtistBandName = iArtist.ArtistBandName; oArtist.ArtistBandLocation = iArtist.ArtistBandLocation; oArtist.ArtistBandDebutDate = iArtist.ArtistBandDebutDate; oArtist.ArtistBandBiography = iArtist.ArtistBandBiography; return(oArtist); }
// Updates the Artist based off the form data public void UpdateArtistInformation(IArtistDO iArtist) { var selectedArtist = new ArtistDAO(); try { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RockersUnite"].ConnectionString)) { using (SqlCommand updateComm = new SqlCommand("UpdateArtistInformation", conn)) { try { updateComm.CommandType = CommandType.StoredProcedure; updateComm.CommandTimeout = 35; updateComm.Parameters.AddWithValue("@ArtistBandID", SqlDbType.Int).Value = iArtist.ArtistBandID; updateComm.Parameters.AddWithValue("@ArtistBandName", SqlDbType.VarChar).Value = iArtist.ArtistBandName; updateComm.Parameters.AddWithValue("@ArtistBandLocation", SqlDbType.VarChar).Value = iArtist.ArtistBandLocation; updateComm.Parameters.AddWithValue("@ArtistBandDebutDate", SqlDbType.DateTime).Value = iArtist.ArtistBandDebutDate; updateComm.Parameters.AddWithValue("@ArtistBandBiography", SqlDbType.VarChar).Value = iArtist.ArtistBandBiography; var artistBandID = selectedArtist.ArtistBandID; var id = 0; // Ensures the ArtistBandID is a valid number before executing the request // This is known as whitelisting; pretty much says what to expect that is trusted if (!int.TryParse(artistBandID.ToString(), out id)) { throw new ApplicationException("Artist ID was not an integer"); } conn.Open(); updateComm.ExecuteNonQuery(); } catch (Exception ex) { throw; } finally { conn.Close(); conn.Dispose(); updateComm.Dispose(); } } } } catch (Exception ex) { throw; } }
public void AddArtist(IArtistDO iArtist) { try { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RockersUnite"].ConnectionString)) { using (SqlCommand command = new SqlCommand("CreateArtistInformation", conn)) { try { command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = 15; // SqlDbType followed by datatype is whitelisting to tell MVC what to expect for the parameter command.Parameters.AddWithValue("@ArtistBandName", SqlDbType.VarChar).Value = iArtist.ArtistBandName; command.Parameters.AddWithValue("@ArtistBandLocation", SqlDbType.VarChar).Value = iArtist.ArtistBandLocation; command.Parameters.AddWithValue("@ArtistBandDebutDate", SqlDbType.DateTime).Value = iArtist.ArtistBandDebutDate; command.Parameters.AddWithValue("@ArtistBandBiography", SqlDbType.VarChar).Value = iArtist.ArtistBandBiography; conn.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { throw; } finally { conn.Close(); conn.Dispose(); command.Dispose(); } } } } catch (Exception ex) { // Log ex throw; } finally { // Connection/Command already disposed, do nothing } }
/// <summary> /// Creates and stores the artist submitted to the site /// </summary> /// <returns></returns> public ActionResult CreateArtist(ArtistViewModel iViewModel) { ActionResult oResponse = null; // Ensures the user is authenticated and is an Admin/SuperUser for creating an artist if (Session["Email"] != null && (int)Session["Role"] >= 4) { if (ModelState.IsValid) { try { // Maps Artist properties from Presentation Objects to Data Objects for creation IArtistDO lArtistForm = ArtistMapper.MapArtistPOtoDO(iViewModel.Artist); // Passes lArtistForm to the AddArtist method artistDA.AddArtist(lArtistForm); oResponse = RedirectToAction("ViewAllArtists", "Artist"); } catch (Exception ex) { using (StreamWriter fileWriter = new StreamWriter(@"C:\Course Content\ForgetTheMilk\RockersUnite\Logger\Log.txt")) { fileWriter.WriteLine("{0}-{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), ex.Message, true); } iViewModel.ErrorMessage = "We apologize, but are unable to handle request at this time.."; // Map data to populate form drop down //PopulateDropDownLists(iViewModel); oResponse = View(iViewModel); } } else { oResponse = View(iViewModel); } } else { // User doesn't have access redirect to home oResponse = RedirectToAction("Index", "Home"); } return(oResponse); }
/// <summary> /// Will modify the changes made to a given artist based off of their Artist ID /// </summary> /// <returns></returns> public ActionResult UpdateArtist(ArtistViewModel iViewModel) { ActionResult oResponse = null; // Ensures the user is Authenticated and is an Admin/SuperUser for Updating an Artist if (Session["Email"] != null && (int)Session["Role"] >= 4) { if (ModelState.IsValid) { try { // Maps Artist from Presenation Object to Data Object IArtistDO lArtistForm = ArtistMapper.MapArtistPOtoDO(iViewModel.Artist); // Passes lArtistForm properties to map From Presentation Objects to Data Objects for Update submission artistDA.UpdateArtistInformation(lArtistForm); oResponse = RedirectToAction("ViewAllArtists", "Artist"); } catch (Exception ex) { using (StreamWriter fileWriter = new StreamWriter(@"C:\Course Content\ForgetTheMilk\RockersUnite\Logger\Log.txt")) { fileWriter.WriteLine("{0}-{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), ex.Message, true); } iViewModel.ErrorMessage = "Sorry, something went wrong. Please try again."; // Mapp all data to appropriate types to populate drop down //PopulateDropDownLists(iViewModel); oResponse = View(iViewModel); } } else { oResponse = View(iViewModel); } } else { // User doesn't have privileges to Update an Artist, redirect to home oResponse = RedirectToAction("Index", "Home"); } return(oResponse); }
/// <summary> /// Retrieves the details for the selected Artist based off of their Artist ID /// </summary> /// <returns></returns> public ActionResult Details(int id) { var selectedArtist = new ArtistViewModel(); ActionResult oResponse = null; if (ModelState.IsValid) { try { // Stores Artist Details by ID to iArtist IArtistDO iArtist = (IArtistDO)artistDA.ArtistDetails(id); // Maps iArtist from Data Objects to Presentation Objects selectedArtist.Artist = ArtistMapper.MapArtistDOtoPO(iArtist); oResponse = View(selectedArtist); } catch (Exception ex) { // Catch the exception; log it and store in View Model using (StreamWriter fileWriter = new StreamWriter(@"C:\Course Content\ForgetTheMilk\RockersUnite\Logger\Log.txt")) { fileWriter.WriteLine("{0}-{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), ex.Message, true); } selectedArtist.ErrorMessage = "We apologize, but we are unable to handle your request at this time."; oResponse = View(selectedArtist); } } else { oResponse = View(selectedArtist); } return(oResponse); }