Example #1
0
        [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()