[TestMethod] // TestMethod attribute public void CreateGamer() { //Call ConfigMgr to get connection string, and save it string connectionString = ConfigurationManager.ConnectionStrings["GamingCenterDBConnectionString"].ConnectionString; //Create new GCDB object using connString, and open connection using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(connectionString)) { //Create new GamingCenter object and initialize given values GamingCenter gamer = new GamingCenter() { Id = -1, // database will re-populate id FirstName = "Jane", LastName = "Doe", UserID = "0000000", GameType = GameType.Xbox, ItemType = ItemType.Generic, IsUVUStudent = true, Date = new DateTime(2016, 1, 1) }; // end constructor //Create gamer entry db.CreateGamer(gamer); //Verify Id is NOT negative Assert.IsTrue(gamer.Id > 0); } // end using } // end method createGamer()
[TestMethod] // TestMethod attribute public void UpdateGamer() { //Call ConfigMgr to get connection string, and save it string connectionString = ConfigurationManager.ConnectionStrings["GamingCenterDBConnectionString"].ConnectionString; //Create new GCDB object using connString, and open connection using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(connectionString)) { //Create new GamingCenter object and initialize given values GamingCenter gamer = new GamingCenter() { Id = -1, // database will re-populate id FirstName = "Monkey", LastName = "McTester", UserID = "9999999", GameType = GameType.Foos, ItemType = ItemType.Ball, IsUVUStudent = true, Date = new DateTime(2015, 2, 2) }; // end constructor //Create gamer entry db.CreateGamer(gamer); //Verify Id is NOT negative Assert.IsTrue(gamer.Id > 0); //Change all properties to different values gamer.FirstName = "Champ"; gamer.LastName = "Awesometon"; gamer.UserID = "1010101"; gamer.GameType = GameType.PS4; gamer.ItemType = ItemType.Generic; gamer.IsUVUStudent = false; gamer.Date = new DateTime(2000, 10, 10); //Update the entry db.UpdateGamer(gamer); //Read gamer back in and verify changes were made GamingCenter gamerCopy = db.ReadGamer(gamer.Id); Assert.AreEqual(gamer.FirstName, gamerCopy.FirstName); Assert.AreEqual(gamer.LastName, gamerCopy.LastName); Assert.AreEqual(gamer.UserID, gamerCopy.UserID); Assert.AreEqual(gamer.GameType, gamerCopy.GameType); Assert.AreEqual(gamer.ItemType, gamerCopy.ItemType); Assert.AreEqual(gamer.IsUVUStudent, gamerCopy.IsUVUStudent); Assert.AreEqual(gamer.Date, gamerCopy.Date); } // end using } // end method UpdateGamer()
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()
[TestMethod] // TestMethod attribute public void GamingCenterDBConstructor() { //Call constructor for GamingCenter class & init test values GamingCenter gamingCenter = new GamingCenter() { FirstName = "Jimmy", LastName = "LoForti", UserID = "10675175", GameType = GameType.Xbox, ItemType = ItemType.Generic, IsUVUStudent = true, Date = new DateTime(2016, 1, 23), }; // end constructor() } // end method GamingCenterDBConstructor()
} // 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()
[TestMethod] // TestMethod attribute public void ReadGamer() { //Call ConfigMgr to get connection string, and save it string connectionString = ConfigurationManager.ConnectionStrings["GamingCenterDBConnectionString"].ConnectionString; //Create new GCDB object using connString, and open connection using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(connectionString)) { //Call ReadGamer with id of 1, and save returned gamer object GamingCenter gamer = db.ReadGamer(1); //Verify gamer EXISTS Assert.IsNotNull(gamer); //Verify gamer's FirstName is NOT null Assert.IsFalse(string.IsNullOrEmpty(gamer.FirstName)); //Write First & LastName to console Console.WriteLine("{0} {1}", gamer.FirstName, gamer.LastName); } // end using } // end method ReadGamer()
[TestMethod] // TestMethod attribute public void DeleteGamer() { //Call ConfigMgr to get connection string, and save it string connectionString = ConfigurationManager.ConnectionStrings["GamingCenterDBConnectionString"].ConnectionString; //Create new GCDB object using connString, and open connection using (GamingCenterDirectoryDB db = new GamingCenterDirectoryDB(connectionString)) { //Create new GamingCenter object and initialize given values GamingCenter gamer = new GamingCenter() { Id = -1, // database will re-populate id FirstName = "Ender", LastName = "Gameless", UserID = "1111111", GameType = GameType.Pong, ItemType = ItemType.PongSet, IsUVUStudent = false, Date = new DateTime(2009, 4, 11) }; // end constructor //Create gamer entry db.CreateGamer(gamer); //Verify Id is NOT negative Assert.IsTrue(gamer.Id > 0); //Delete the entry db.DeleteGamer(gamer.Id); //Get the recently deleted gamer GamingCenter gamerOld = db.ReadGamer(gamer.Id); //Verify the entry is null Assert.IsTrue(gamerOld == null); } // end using } // end method DeleteGamer()