public ActionResult Delete(int id, GamingModel _gamer) { try { //Create instance of DAL, and pass connection string by calling ConnectionString property using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(ConnectionString)) { //Call DAL to delete gamer from database db.DeleteGamer(id); } // end using //Return a 302 redirect action to Index page return(RedirectToAction("Index")); } // end try catch (KeyNotFoundException) // gamer _id was bad { //Return 404 - Not found status code return(new HttpStatusCodeResult( HttpStatusCode.NotFound, string.Format("Gamer with id={0} unknown", id))); } // end catch catch { //Return a HttpStatusCodeResult object, and set status code as 500 (InternalServerError) and pass string to print return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Internal Server Error ")); } // end catch } // end method POST Delete()
} // end method Details() //The Create method - GET //Purpose: To submit a GET request to load the page //Parameters: None //Return: A View in the form of an ActionResult // GET: Gaming/Create public ActionResult Create() { //Create new instance of GamingModel GamingModel gamingModel = new GamingModel(); //Populate GameTypeOptions gamingModel.GameTypeOptions = GetGameTypeOptions(); //Populate ItemTypeOptions gamingModel.ItemTypeOptions = GetItemTypeOptions(); return(View(gamingModel)); } // end method GET Create()
public ActionResult Edit(int id, GamingModel _gamer) { //If model is BAD - user validation failed if (AuditModel(_gamer) == -1) { //Return the model and List options _gamer.GameTypeOptions = _gameTypeOptions; _gamer.ItemTypeOptions = _itemTypeOptions; return(View(_gamer)); } // end if else // Model is GOOD { try { //Create instance of DAL, and pass connection string by calling ConnectionString property using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(ConnectionString)) { //Create new GamingCenter object GamingCenter dbGamer = new GamingCenter(); //Get each data member of the GamingModel object and save as the corresponding data member of the GamingCenter object dbGamer.Id = id; dbGamer.FirstName = _gamer.FirstName; dbGamer.LastName = _gamer.LastName; dbGamer.UserID = _gamer.UserID; dbGamer.GameType = (GameType)Enum.Parse(typeof(GameType), _gamer.GameType); dbGamer.ItemType = (ItemType)Enum.Parse(typeof(ItemType), _gamer.ItemType); dbGamer.IsUVUStudent = _gamer.IsUVUStudent; dbGamer.Date = _gamer.Date; // dbGamer.Date = DateTime.Parse(_gamer.Date) //Update gamer in database db.UpdateGamer(dbGamer); } // end using //Return a 302 redirect action to Index page return(RedirectToAction("Index")); } // end try catch (KeyNotFoundException) // gamer _id was bad { //Return 404 - Not found status code return(new HttpStatusCodeResult( HttpStatusCode.NotFound, string.Format("Gamer with id={0} unknown", id))); } // end catch catch { //Return a HttpStatusCodeResult object, and set status code as 500 (InternalServerError) and pass string to print return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Internal Server Error ")); } // end catch } // end else } // end method POST Edit()
} // end method POST Edit() //The Delete method - GET //Purpose: To connect to the DAL and get gamer with specified id from database //Parameters: An int represented as _id - Value pulled from URI //Return: The gamingModel View in the form of an ActionResult, or status code "NOT FOUND" for no gamer w/ id, or 500 error for all other // GET: Gaming/Delete/5 public ActionResult Delete(int id) { //Create GamingModel instance and initialize to null GamingModel gamingModel = null; try { //Create instance of DAL, and pass connection string by calling ConnectionString property using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(ConnectionString)) { //Get GamingCenter object with specified _id, and save GamingCenter dbGamer = db.ReadGamer(id); //If gamer cannot be found if (dbGamer == null) { //Return 404 - Not found status code return(new HttpStatusCodeResult( HttpStatusCode.NotFound, string.Format("Gamer with id={0} unknown", id))); } // end if //Save dbGamer data members as GamingModel object data members gamingModel = new GamingModel() { Id = dbGamer.Id.ToString(), FirstName = dbGamer.FirstName, LastName = dbGamer.LastName, UserID = dbGamer.UserID.ToString(), GameType = dbGamer.GameType.ToString(), ItemType = dbGamer.ItemType.ToString(), IsUVUStudent = dbGamer.IsUVUStudent, Date = dbGamer.Date, // ToString() GameTypeOptions = GetGameTypeOptions(), ItemTypeOptions = GetItemTypeOptions() }; // end initialize GamingModel } // end using } // end try catch (Exception) { //Return a HttpStatusCodeResult object, and set status code as 500 (InternalServerError) and pass string to print return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Internal Server Error ")); } // end catch return(View(gamingModel)); } // end method GET Delete()
public ActionResult Create(GamingModel _gamer) { //If model is BAD - user validation failed if (AuditModel(_gamer) == -1) { //Return the model and List options _gamer.GameTypeOptions = _gameTypeOptions; _gamer.ItemTypeOptions = _itemTypeOptions; return(View(_gamer)); } // end if else // Model is GOOD { try { //Create instance of DAL, and pass connection string by calling ConnectionString property using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(ConnectionString)) { //Create new GamingCenter object GamingCenter dbGamer = new GamingCenter(); //Get each data member of the GamingModel object and save as the corresponding data member of the GamingCenter object dbGamer.FirstName = _gamer.FirstName; dbGamer.LastName = _gamer.LastName; dbGamer.UserID = _gamer.UserID; dbGamer.GameType = (GameType)Enum.Parse(typeof(GameType), _gamer.GameType); dbGamer.ItemType = (ItemType)Enum.Parse(typeof(ItemType), _gamer.ItemType); dbGamer.IsUVUStudent = _gamer.IsUVUStudent; dbGamer.Date = _gamer.Date; //Add Gamer to the db db.CreateGamer(dbGamer); } // end using //Return a 302 redirect action to Index page return(RedirectToAction("Index")); } // end try catch { //Return a HttpStatusCodeResult object, and set status code as 500 (InternalServerError) and pass string to print return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Internal Server Error ")); } // end catch } // end else } // end method POST Create()
} // end method GetGameTypeOptions() //The Index method //Purpose: To connect to the DAL, pull all records from the database and convert them to GamingModel objects //Parameters: None //Return: The gaming list in the form of a View, or a 500 error if connection or data doesn't exist // GET: Gaming public ActionResult Index() { //Create a list of GamingModel objects List <GamingModel> gamingList = new List <GamingModel>(); try { //Create instance of DAL, and pass connection string by calling ConnectionString property using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(ConnectionString)) { //Foreach GamingCenter object in the database foreach (GamingCenter dbGaming in db.ReadAllGamingCenter()) { //Get the db data members, and save as GamingModel data members GamingModel gamingModel = new GamingModel() { Id = dbGaming.Id.ToString(), FirstName = dbGaming.FirstName, LastName = dbGaming.LastName, UserID = dbGaming.UserID, GameType = dbGaming.GameType.ToString(), ItemType = dbGaming.ItemType.ToString(), IsUVUStudent = dbGaming.IsUVUStudent, Date = dbGaming.Date, GameTypeOptions = GetGameTypeOptions(), ItemTypeOptions = GetItemTypeOptions() }; // end initialize GamingModel //Add this model to the list gamingList.Add(gamingModel); } // end foreach } // end using } // end try catch (Exception) { //Return a HttpStatusCodeResult object, and set status code as 500 (InternalServerError) and pass string to print return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Internal Server Error ")); } // end catch return(View(gamingList)); } // end method Index()
} // end method POST Delete() //The AuditModel method //Purpose: To validate the firstName, lastName, and User ID data members of the given GamingModel object //Parameters: A GamingModel object represented as _gamer //Return: An int: 0 = Model Valid, -1 = Model NOT Valid private int AuditModel(GamingModel _gamer) { //Declare helper vars: DateTime date; bool boolean; //Create string of special characters string specChars = @"^!#$%^&*()[]{}<>"; //Split string into array char[] specCharsArray = specChars.ToCharArray(); //Ensure first name is NOT empty or white space if (string.IsNullOrWhiteSpace(_gamer.FirstName)) { ModelState.AddModelError("FirstName", "First name is required. "); } //Ensure first name is NOT null if (_gamer.FirstName != null) { //Ensure first name does NOT exceed max length if (_gamer.FirstName.Length > 30) { ModelState.AddModelError("FirstName", "First name cannot exceed 30 characters. "); } //Ensure first name does NOT include prohibited special chars if (_gamer.FirstName.IndexOfAny(specCharsArray) != -1) { ModelState.AddModelError("FirstName", "First name can contain only 1 to 30 letters, hyphens, or spaces. "); } } // end if //Ensure last name is NOT empty or white space if (string.IsNullOrWhiteSpace(_gamer.LastName)) { ModelState.AddModelError("LastName", "Last name is required. "); } //Ensure last name is NOT null if (_gamer.LastName != null) { //Ensure last name does NOT exceed max length if (_gamer.LastName.Length > 30) { ModelState.AddModelError("LastName", "Last name cannot exceed 30 characters. "); } //Ensure last name does NOT include prohibited special chars if (_gamer.LastName.IndexOfAny(specCharsArray) != -1) { ModelState.AddModelError("LastName", "Last name can contain only 1 to 30 letters, hyphens, or spaces. "); } } // end if //Ensure user ID is NOT empty or white space if (string.IsNullOrWhiteSpace(_gamer.UserID)) { ModelState.AddModelError("UserID", "User ID is required. "); } //Ensure user ID is NOT null if (_gamer.UserID != null) { //Ensure User ID does NOT include prohibited special chars if (_gamer.UserID.IndexOfAny(specCharsArray) != -1) { ModelState.AddModelError("UserID", "User ID can contain only 1 to 30 letters, numbers, hyphens, or spaces. "); } } // end if //Ensure game type is NOT null if (_gamer.GameType == null) { ModelState.AddModelError("GameType", "Game type is required. "); } // end if //Ensure item type is NOT null if (_gamer.ItemType == null) { ModelState.AddModelError("ItemType", "Item type is required. "); } // end if //Ensure isUVUStudent is a boolean if (!bool.TryParse(_gamer.IsUVUStudent.ToString(), out boolean)) { ModelState.AddModelError("IsUVUStudent", "UVU student indication is required, check box for 'yes'. "); } // end if //Ensure date is within SQL DateTime bounds if (_gamer.Date.Year < 1753 || _gamer.Date.Year > 2999) { ModelState.AddModelError("Date", "Date year must be between 1753 and 2999. "); } // end if //Ensure date can be parsed if (!DateTime.TryParse(_gamer.Date.ToString(), out date)) { ModelState.AddModelError("Date", "Invalid date format. "); } // end if //If the model is BAD if (!ModelState.IsValid) { return(-1); } // end if return(0); } // end method AuditModel()