public static ItemType Find(int Id) { SqlConnection conn = DB.Connection(); conn.Open(); SqlDataReader rdr; SqlCommand cmd = new SqlCommand("SELECT * FROM item_types WHERE id = @ItemTypeId;", conn); SqlParameter itemTypeIdParameter = new SqlParameter(); itemTypeIdParameter.ParameterName = "@ItemTypeId"; itemTypeIdParameter.Value = Id.ToString(); cmd.Parameters.Add(itemTypeIdParameter); rdr = cmd.ExecuteReader(); ItemType foundItemType = null; while(rdr.Read()) { int foundId = rdr.GetInt32(0); string foundName = rdr.GetString(1); foundItemType = new ItemType(foundName, foundId); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return foundItemType; }
public void Test_Find_FindsItemTypeInDatabase() { ItemType testItemType = new ItemType("Artorias"); testItemType.Save(); ItemType foundItemType = ItemType.Find(testItemType.GetId()); Assert.Equal(testItemType, foundItemType); }
public void Test_Save_SavesToDatabase() { ItemType testItemType = new ItemType("Artorias"); testItemType.Save(); List<ItemType> result = ItemType.GetAll(); List<ItemType> testList = new List<ItemType>{testItemType}; Assert.Equal(testList, result); }
public void Test_Update_UpdatesItemTypeWithNewValues() { ItemType testItemType = new ItemType("Artorias"); testItemType.Save(); testItemType.SetName("Guts"); testItemType.Update(); ItemType resultItemType = ItemType.Find(testItemType.GetId()); ItemType test = new ItemType("Guts", testItemType.GetId()); Assert.Equal(test, resultItemType); }
public void Test_Delete_DeletesItemTypeFromDatabase() { ItemType testItemType1 = new ItemType("Artorias"); testItemType1.Save(); ItemType testItemType2 = new ItemType("Guts"); testItemType2.Save(); ItemType.Delete(testItemType1.GetId()); List<ItemType> testList = new List<ItemType>{testItemType2}; List<ItemType> resultList = ItemType.GetAll(); Assert.Equal(testList, resultList); }
public static List<ItemType> GetAll() { List<ItemType> AllItemTypes = new List<ItemType>{}; SqlConnection conn = DB.Connection(); conn.Open(); SqlDataReader rdr; SqlCommand cmd = new SqlCommand("SELECT * FROM item_types;", conn); rdr = cmd.ExecuteReader(); while(rdr.Read()) { int itemTypeId = rdr.GetInt32(0); string itemTypeName = rdr.GetString(1); ItemType newItemType = new ItemType(itemTypeName, itemTypeId); AllItemTypes.Add(newItemType); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return AllItemTypes; }