public void Test_Find_FindsCharacterInDatabase() { Character testCharacter = new Character("Artorias", 0,1, 1,1,1); testCharacter.Save(); Character foundCharacter = Character.Find(testCharacter.GetId()); Assert.Equal(testCharacter, foundCharacter); }
public void Test_Save_SavesToDatabase() { Character testCharacter = new Character("Artorias", 0, 1, 1,1,1); testCharacter.Save(); List<Character> result = Character.GetAll(); List<Character> testList = new List<Character>{testCharacter}; Assert.Equal(testList, result); }
public void Test_Update_UpdatesCharacterWithNewValues() { Character testCharacter = new Character("Artorias", 0,1, 1,1,1); testCharacter.Save(); testCharacter.SetName("Guts"); testCharacter.Update(); Character resultCharacter = Character.Find(testCharacter.GetId()); Character test = new Character("Guts", 0,1, 1,1,1, testCharacter.GetId()); Assert.Equal(test, resultCharacter); }
public void Test_Delete_DeletesCharacterFromDatabase() { Character testCharacter1 = new Character("Artorias", 0,1, 1,1,1); testCharacter1.Save(); Character testCharacter2 = new Character("Guts", 0,1, 1,1,1); testCharacter2.Save(); Character.Delete(testCharacter1.GetId()); List<Character> testList = new List<Character>{testCharacter2}; List<Character> resultList = Character.GetAll(); Assert.Equal(testList, resultList); }
public static Character Find(int Id) { SqlConnection conn = DB.Connection(); conn.Open(); SqlDataReader rdr; SqlCommand cmd = new SqlCommand("SELECT * FROM characters WHERE id = @CharacterId;", conn); SqlParameter characterIdParameter = new SqlParameter(); characterIdParameter.ParameterName = "@CharacterId"; characterIdParameter.Value = Id.ToString(); cmd.Parameters.Add(characterIdParameter); rdr = cmd.ExecuteReader(); Character foundCharacter = null; while(rdr.Read()) { int foundId = rdr.GetInt32(0); string foundName = rdr.GetString(1); int foundClassId = rdr.GetInt32(2); int foundCharacterClassId = rdr.GetInt32(2); int foundBodyTypeId = rdr.GetInt32(3); int foundWeaponId = rdr.GetInt32(4); int foundArmorId = rdr.GetInt32(5); int foundSpecialId = rdr.GetInt32(6); foundCharacter = new Character(foundName, foundClassId, foundBodyTypeId, foundWeaponId, foundArmorId, foundSpecialId, foundId); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return foundCharacter; }
public static List<Character> GetAll() { List<Character> AllCharacters = new List<Character>{}; SqlConnection conn = DB.Connection(); conn.Open(); SqlDataReader rdr; SqlCommand cmd = new SqlCommand("SELECT * FROM characters;", conn); rdr = cmd.ExecuteReader(); while(rdr.Read()) { int characterId = rdr.GetInt32(0); string characterName = rdr.GetString(1); int characterClassId = rdr.GetInt32(2); int bodyTypeId = rdr.GetInt32(3); int weaponId = rdr.GetInt32(4); int armorId = rdr.GetInt32(5); int specialId = rdr.GetInt32(6); Character newCharacter = new Character(characterName, characterClassId, bodyTypeId, weaponId, armorId, specialId, characterId); AllCharacters.Add(newCharacter); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return AllCharacters; }
public HomeModule() { Get["/"]= _ =>{ return View["index.cshtml"]; }; Get["/character/new"]= _ =>{ Dictionary<string, object> model = new Dictionary<string, object>(); model.Add("class", Class.GetAll()); model.Add("item", Item.GetAll()); model.Add("itemTypes", ItemType.GetAll()); return View["main.cshtml", model]; }; Post["/character/create"]= _ =>{ Character newCharacter = new Character(Request.Form["name"],Request.Form["klass"],Request.Form["bodyType"],Request.Form["weapon"],Request.Form["armor"],Request.Form["special"]); newCharacter.Save(); Dictionary<string, object> model = new Dictionary<string, object>(); Class characterClassName = Class.Find(newCharacter.GetClassId()); Item characterBodyType = Item.Find(newCharacter.GetBodyTypeId()); Item characterWeapon = Item.Find(newCharacter.GetWeaponId()); Item characterArmor = Item.Find(newCharacter.GetArmorId()); Item characterSpecial = Item.Find(newCharacter.GetSpecialId()); model.Add("className", characterClassName); model.Add("bodyType", characterBodyType); model.Add("weapon", characterWeapon); model.Add("armor", characterArmor); model.Add("special", characterSpecial); model.Add("newCharacter", newCharacter); return View["new_character.cshtml", model]; }; Get["/character/load"] =_=> { List<Character> AllCharacters = Character.GetAll(); return View["load_character.cshtml", AllCharacters]; }; Get["/character/update/{id}"] =parameters=> { Dictionary<string, object> model = new Dictionary<string, object>(); model.Add("klass", Class.GetAll()); model.Add("item", Item.GetAll()); model.Add("itemTypes", ItemType.GetAll()); Character currentCharacter = Character.Find(parameters.id); model.Add("current", currentCharacter); return View["update_character.cshtml", model]; }; Post["/character/confirm-update/{id}"] =parameters=> { Character currentCharacter = Character.Find(parameters.id); currentCharacter.SetBodyType(Request.Form["bodyType"]); currentCharacter.SetClass(Request.Form["klass"]); currentCharacter.SetName(Request.Form["name"]); currentCharacter.SetWeapon(Request.Form["weapon"]); currentCharacter.SetArmor(Request.Form["armor"]); currentCharacter.SetSpecial(Request.Form["special"]); currentCharacter.Update(); List<Character> AllCharacters = Character.GetAll(); return View["load_character.cshtml", AllCharacters]; }; Get["/character/delete/{id}"] =parameters=> { Character currentCharacter = Character.Find(parameters.id); Dictionary<string, object> model = new Dictionary<string, object>(); Class characterClassName = Class.Find(currentCharacter.GetClassId()); Item characterBodyType = Item.Find(currentCharacter.GetBodyTypeId()); Item characterWeapon = Item.Find(currentCharacter.GetWeaponId()); Item characterArmor = Item.Find(currentCharacter.GetArmorId()); Item characterSpecial = Item.Find(currentCharacter.GetSpecialId()); model.Add("current", currentCharacter); model.Add("className", characterClassName); model.Add("bodyType", characterBodyType); model.Add("weapon", characterWeapon); model.Add("armor", characterArmor); model.Add("special", characterSpecial); return View["delete_character.cshtml", model]; }; Get["/character/delete-confirm/{id}"] =parameters=> { Character.Delete(parameters.id); List<Character> AllCharacters = Character.GetAll(); return View["load_character.cshtml", AllCharacters]; }; }