Commit 221d35da authored by Clément's avatar Clément
Browse files

Merge branch 'develop' of https://gitlab.isima.fr/jeneboit1/ServiceWeb into develop

parents 2b5c62a0 9b47bf91
......@@ -28,10 +28,22 @@ namespace BusinessLayer
{
return DalManager.HousesList(sup);
}
public List<Fight> FightsList()
{
return DalManager.FightsList();
}
public War getWarById(int id)
{
return DalManager.getWarById(id);
}
public House getHouseById(int id)
{
return DalManager.getHouseById(id);
}
public Character getCharacterById(int id)
{
return DalManager.getCharacterById(id);
}
public List<Characteristics> CharactersCharacteristicsList()
{
return DalManager.CharactersCharacteristicsList();
......@@ -51,9 +63,9 @@ namespace BusinessLayer
return DalManager.TerritoriesList();
}
public void addCharacter(Character c)
public int addCharacter(Character c)
{
DalManager.addCharacter(c);
return DalManager.addCharacter(c);
}
public void deleteCharacater(int id)
{
......@@ -63,5 +75,53 @@ namespace BusinessLayer
{
DalManager.updateCharacter(c);
}
public int addHouse(House h)
{
return DalManager.addHouse(h);
}
public void deleteHouse(int id)
{
DalManager.deleteHouse(id);
}
public void updateHouse(House h)
{
DalManager.updateHouse(h);
}
public int addFight(Fight f)
{
return DalManager.addFight(f);
}
public void deleteFight(int id)
{
DalManager.deleteFight(id);
}
public void updateFight(Fight f)
{
DalManager.updateFight(f);
}
public int addTerritory(Territory t)
{
return DalManager.addTerritory(t);
}
public void deleteTerritory(int id)
{
DalManager.deleteTerritory(id);
}
public void updateTerritory(Territory t)
{
DalManager.updateTerritory(t);
}
public int addWar(War w)
{
return DalManager.addWar(w);
}
public void deleteWar(int id)
{
DalManager.deleteWar(id);
}
public void updateWar(War w)
{
DalManager.updateWar(w);
}
}
}
......@@ -47,10 +47,24 @@ namespace DataAccessLayer
return dataBaseBridge.HousesList(sup);
}
public List<Fight> FightsList()
{
return dataBaseBridge.FightsList();
}
public War getWarById(int id)
{
return dataBaseBridge.getWarById(id);
}
public House getHouseById(int id)
{
return dataBaseBridge.getHouseById(id);
}
public Character getCharacterById(int id)
{
return dataBaseBridge.getCharacterById(id);
}
public List<Characteristics> CharactersCharacteristicsList()
{
return dataBaseBridge.CharactersCharacteristicsList();
......@@ -66,14 +80,9 @@ namespace DataAccessLayer
return dataBaseBridge.TerritoriesList();
}
public List<Fight> FightsList()
{
return dataBaseBridge.FightsList();
}
public void addCharacter(Character character)
public int addCharacter(Character character)
{
dataBaseBridge.addCharacter(character);
return dataBaseBridge.addCharacter(character);
}
public void deleteCharacter(int id)
......@@ -85,5 +94,62 @@ namespace DataAccessLayer
{
dataBaseBridge.updateCharacter(character);
}
public int addHouse(House house)
{
return dataBaseBridge.addHouse(house);
}
public void deleteHouse(int id)
{
dataBaseBridge.deleteHouse(id);
}
public void updateHouse(House house)
{
dataBaseBridge.updateHouse(house);
}
public int addFight(Fight fight)
{
return dataBaseBridge.addFight(fight);
}
public void deleteFight(int id)
{
dataBaseBridge.deleteFight(id);
}
public void updateFight(Fight fight)
{
dataBaseBridge.updateFight(fight);
}
public int addTerritory(Territory territory)
{
return dataBaseBridge.addTerritory(territory);
}
public void deleteTerritory(int id)
{
dataBaseBridge.deleteTerritory(id);
}
public void updateTerritory(Territory territory)
{
dataBaseBridge.updateTerritory(territory);
}
public int addWar(War war)
{
return dataBaseBridge.addWar(war);
}
public void deleteWar(int id)
{
dataBaseBridge.deleteWar(id);
}
public void updateWar(War war)
{
dataBaseBridge.updateWar(war);
}
}
}
......@@ -11,14 +11,28 @@ namespace DataAccessLayer
{
List<House> HousesList();
List<House> HousesList(int sup);
War getWarById(int id);
House getHouseById(int id);
Character getCharacterById(int id);
List<Character> CharactersList();
List<Characteristics> CharactersCharacteristicsList();
List<Territory> TerritoriesList();
List<Fight> FightsList();
void addCharacter(Character character);
int addCharacter(Character character);
void deleteCharacter(int id);
void updateCharacter(Character character);
int addHouse(House house);
void deleteHouse(int id);
void updateHouse(House house);
int addFight(Fight house);
void deleteFight(int id);
void updateFight(Fight fight);
int addTerritory(Territory territory);
void deleteTerritory(int id);
void updateTerritory(Territory territory);
int addWar(War war);
void deleteWar(int id);
void updateWar(War war);
}
}
<
......@@ -13,6 +13,11 @@ namespace DataAccessLayer
{
private string _connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=H:\\Documents\\ThronesTournamentConsole\\Database\\db.mdf;Integrated Security=True;Connect Timeout=30";
private void LogException(Exception e)
{
Console.WriteLine("Exception: " + e.Message + ", Source" + e.Source);
}
private DataTable SelectByDataAdapter(string request)
{
DataTable results = new DataTable();
......@@ -27,7 +32,7 @@ namespace DataAccessLayer
}
catch (SqlException e)
{
Console.WriteLine(e.StackTrace);
LogException(e);
}
}
......@@ -40,18 +45,25 @@ namespace DataAccessLayer
using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
{
SqlCommand sqlCommand = new SqlCommand(request, sqlConnection);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
try
{
SqlCommand sqlCommand = new SqlCommand(request, sqlConnection);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
sqlDataAdapter.InsertCommand = sqlCommandBuilder.GetInsertCommand();
sqlDataAdapter.DeleteCommand = sqlCommandBuilder.GetDeleteCommand();
sqlDataAdapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
sqlDataAdapter.InsertCommand = sqlCommandBuilder.GetInsertCommand();
sqlDataAdapter.DeleteCommand = sqlCommandBuilder.GetDeleteCommand();
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
result = sqlDataAdapter.Update(table);
result = sqlDataAdapter.Update(table);
}
catch (Exception e)
{
LogException(e);
}
}
return result;
......@@ -61,17 +73,21 @@ namespace DataAccessLayer
{
List<Character> res = new List<Character>();
DataTable dataTable = SelectByDataAdapter("Select * From Character");
foreach (DataRow row in dataTable.Rows)
try
{
int id_house = Convert.ToInt32(row["id_house"]);
DataTable dataTable = SelectByDataAdapter("Select * From Character");
DataTable houseData = SelectByDataAdapter("Select * From House Where Id=" + id_house);
DataRow houseRow = houseData.Rows[0];
House house = new House(Convert.ToInt32(houseRow["Id"]), houseRow["name"].ToString(), Convert.ToInt32(houseRow["nbUnits"]));
Character character = new Character(Convert.ToInt32(row["Id"]), row["FirstName"].ToString(), row["LastName"].ToString(), Convert.ToUInt32(row["PV"]), (CharacterType)Convert.ToInt32(row["Type"]), Convert.ToInt32(row["Bravoury"]), Convert.ToInt32(row["Crazyness"]), house);
res.Add(character);
foreach (DataRow row in dataTable.Rows)
{
int id_house = Convert.ToInt32(row["id_house"]);
House house = getHouseById(id_house); ;
Character character = new Character(Convert.ToInt32(row["Id"]), row["FirstName"].ToString(), row["LastName"].ToString(), Convert.ToUInt32(row["PV"]), (CharacterType)Convert.ToInt32(row["Type"]), Convert.ToInt32(row["Bravoury"]), Convert.ToInt32(row["Crazyness"]), house);
res.Add(character);
}
}
catch(Exception e)
{
LogException(e);
}
return res;
......@@ -81,12 +97,19 @@ namespace DataAccessLayer
{
List<Characteristics> res = new List<Characteristics>();
DataTable dataTable = SelectByDataAdapter("Select * From Character");
try
{
DataTable dataTable = SelectByDataAdapter("Select * From Character");
foreach (DataRow row in dataTable.Rows)
foreach (DataRow row in dataTable.Rows)
{
Characteristics characteristics = new Characteristics(Convert.ToUInt32(row["PV"]), (CharacterType)Convert.ToInt32(row["Type"]), Convert.ToInt32(row["Bravoury"]), Convert.ToInt32(row["Crazyness"]));
res.Add(characteristics);
}
}
catch (Exception e)
{
Characteristics characteristics = new Characteristics(Convert.ToUInt32(row["PV"]), (CharacterType)Convert.ToInt32(row["Type"]), Convert.ToInt32(row["Bravoury"]), Convert.ToInt32(row["Crazyness"]));
res.Add(characteristics);
LogException(e);
}
return res;
......@@ -96,6 +119,33 @@ namespace DataAccessLayer
public List<Fight> FightsList()
{
List<Fight> res = new List<Fight>();
try
{
DataTable dataTable = SelectByDataAdapter("Select * From Fight");
foreach (DataRow row in dataTable.Rows)
{
int id_house = Convert.ToInt32(row["id_challenger1"]);
House chal1 = getHouseById(id_house);
id_house = Convert.ToInt32(row["id_challenger2"]);
House chal2 = getHouseById(id_house);
int id_war = (row["id_war"] is DBNull) ? -1 : Convert.ToInt32(row["id_war"]);
War war = (id_war == -1) ? null : getWarById(id_war);
int id_winner = (row["id_winner"] is DBNull) ? -1 : Convert.ToInt32(row["id_winner"]);
House winner = (id_winner == -1) ? null : getHouseById(id_winner);
Fight fight = new Fight(Convert.ToInt32(row["Id"]), row["name"].ToString(), chal1, chal2, winner, war);
res.Add(fight);
}
}
catch (Exception e)
{
LogException(e);
}
return res;
}
......@@ -103,12 +153,19 @@ namespace DataAccessLayer
{
List<House> res = new List<House>();
DataTable dataTable = SelectByDataAdapter("Select * From House");
try
{
DataTable dataTable = SelectByDataAdapter("Select * From House");
foreach (DataRow row in dataTable.Rows)
foreach (DataRow row in dataTable.Rows)
{
House house = new House(Convert.ToInt32(row["Id"]), row["name"].ToString(), Convert.ToInt32(row["nbUnits"]));
res.Add(house);
}
}
catch (Exception e)
{
House house = new House(Convert.ToInt32(row["Id"]), row["name"].ToString(), Convert.ToInt32(row["nbUnits"]));
res.Add(house);
LogException(e);
}
return res;
......@@ -118,48 +175,103 @@ namespace DataAccessLayer
{
List<House> res = new List<House>();
DataTable dataTable = SelectByDataAdapter("Select * From House Where nbUnits >" + sup);
try
{
DataTable dataTable = SelectByDataAdapter("Select * From House Where nbUnits >" + sup);
foreach (DataRow row in dataTable.Rows)
foreach (DataRow row in dataTable.Rows)
{
House house = new House(Convert.ToInt32(row["Id"]), row["name"].ToString(), Convert.ToInt32(row["nbUnits"]));
res.Add(house);
}
}
catch (Exception e)
{
House house = new House(Convert.ToInt32(row["Id"]) , row["name"].ToString(), Convert.ToInt32(row["nbUnits"]));
res.Add(house);
LogException(e);
}
return res;
}
public House getHouseById(int id)
{
DataTable houseData = SelectByDataAdapter("Select * From House Where Id=" + id);
DataRow houseRow = houseData.Rows[0];
House res = new House(Convert.ToInt32(houseRow["Id"]), houseRow["name"].ToString(), Convert.ToInt32(houseRow["nbUnits"]));
House res = null;
try
{
DataTable houseData = SelectByDataAdapter("Select * From House Where Id=" + id);
DataRow houseRow = houseData.Rows[0];
res = new House(Convert.ToInt32(houseRow["Id"]), houseRow["name"].ToString(), Convert.ToInt32(houseRow["nbUnits"]));
}
catch (Exception e)
{
LogException(e);
}
return res;
}
public Character getCharacterById(int id)
{
Character res = null;
try
{
DataTable characData = SelectByDataAdapter("Select * From Character Where Id=" + id);
DataRow characRow = characData.Rows[0];
int id_house = Convert.ToInt32(characRow["id_house"]);
House house = getHouseById(id_house);
res = new Character(Convert.ToInt32(characRow["Id"]), characRow["FirstName"].ToString(), characRow["LastName"].ToString(), Convert.ToUInt32(characRow["PV"]), (CharacterType)Convert.ToInt32(characRow["Type"]), Convert.ToInt32(characRow["Bravoury"]), Convert.ToInt32(characRow["Crazyness"]), house);
}
catch (Exception e)
{
LogException(e);
}
return res;
}
public War getWarById(int id)
{
War res = null;
try
{
DataTable warData = SelectByDataAdapter("Select * From War Where Id=" + id);
DataRow warRow = warData.Rows[0];
res = new War(Convert.ToInt32(warRow["Id"]), warRow["name"].ToString());
}
catch (Exception e)
{
LogException(e);
}
return res;
}
public List<Territory> TerritoriesList()
{
List<Territory> res = new List<Territory>();
DataTable dataTable = SelectByDataAdapter("Select * From Territory");
foreach (DataRow row in dataTable.Rows)
try
{
int id_house = Convert.ToInt32(row["id_house"]);
DataTable dataTable = SelectByDataAdapter("Select * From Territory");
DataTable houseData = SelectByDataAdapter("Select * From House Where Id=" + id_house);
DataRow houseRow = houseData.Rows[0];
House owner = new House(Convert.ToInt32(houseRow["Id"]), houseRow["name"].ToString(), Convert.ToInt32(houseRow["nbUnits"]));
Territory territory = new Territory(Convert.ToInt32(row["Id"]), (TerritoryType)Convert.ToInt32(row["type"]), owner);
res.Add(territory);
foreach (DataRow row in dataTable.Rows)
{
int id_house = Convert.ToInt32(row["id_house"]);
House owner = getHouseById(id_house);
Territory territory = new Territory(Convert.ToInt32(row["Id"]), (TerritoryType)Convert.ToInt32(row["type"]), owner);
res.Add(territory);
}
}
catch (Exception e)
{
LogException(e);
}
return res;
}
public void addCharacter(Character character)
public int addCharacter(Character character)
{
SelectByDataAdapter("INSERT INTO Character VALUES('" + character.FirstName + "','" + character.LastName + "'," + character.Characteristics.PV + "," + (int)character.Characteristics.Type + "," + character.Characteristics.Bravoury + "," + character.Characteristics.Crazyness + "," + character.House.ID +")");
DataTable characData = SelectByDataAdapter("INSERT INTO Character VALUES('" + character.FirstName + "','" + character.LastName + "'," + character.Characteristics.PV + "," + (int)character.Characteristics.Type + "," + character.Characteristics.Bravoury + "," + character.Characteristics.Crazyness + "," + character.House.ID + "); SELECT SCOPE_IDENTITY()");
DataRow characRow = characData.Rows[0];
return Convert.ToInt32(characRow[0]);
}
public void deleteCharacter(int id)
......@@ -167,19 +279,116 @@ namespace DataAccessLayer
SelectByDataAdapter("DELETE FROM Character Where Id=" + id);
}
public void updateCharacter(Character character)
{
String request = "Select * From Character Where Id=" + character.ID;
DataTable dataTable = SelectByDataAdapter(request);
dataTable.Rows[0]["FirstName"] = character.FirstName;
dataTable.Rows[0]["LastName"] = character.LastName;
dataTable.Rows[0]["PV"] = character.Characteristics.PV;
dataTable.Rows[0]["Type"] = character.Characteristics.Type;
dataTable.Rows[0]["Bravoury"] = character.Characteristics.Bravoury;
dataTable.Rows[0]["Crazyness"] = character.Characteristics.Crazyness;
dataTable.Rows[0]["id_house"] = character.House.ID;
UpdateByCommandBuilder(request, dataTable);
if (dataTable.Rows.Count == 1)
{
dataTable.Rows[0]["FirstName"] = character.FirstName;
dataTable.Rows[0]["LastName"] = character.LastName;
dataTable.Rows[0]["PV"] = character.Characteristics.PV;
dataTable.Rows[0]["Type"] = (int)character.Characteristics.Type;
dataTable.Rows[0]["Bravoury"] = character.Characteristics.Bravoury;
dataTable.Rows[0]["Crazyness"] = character.Characteristics.Crazyness;
dataTable.Rows[0]["id_house"] = character.House.ID;
UpdateByCommandBuilder(request, dataTable);
}
}
public int addHouse(House house)
{
DataTable houseData = SelectByDataAdapter("INSERT INTO Character VALUES('" + house.Name + "'," + house.NbUnits + "); SELECT SCOPE_IDENTITY()");
DataRow houseRow = houseData.Rows[0];
return Convert.ToInt32(houseRow[0]);
}
public void deleteHouse(int id)
{
SelectByDataAdapter("DELETE FROM House Where Id=" + id);
}
public void updateHouse(House house)
{
String request = "Select * From House Where Id=" + house.ID;
DataTable dataTable = SelectByDataAdapter(request);
if (dataTable.Rows.Count == 1)
{
dataTable.Rows[0]["name"] = house.Name;
dataTable.Rows[0]["nbUnits"] = house.NbUnits;
UpdateByCommandBuilder(request, dataTable);
}
}
public int addFight(Fight fight)
{
DataTable fightData = SelectByDataAdapter("INSERT INTO Fight VALUES('" + fight.Name + "'," + fight.Challenger1.ID + "," + fight.Challenger2.ID + "," + fight.Winner.ID + "," + fight.War.ID + "); SELECT SCOPE_IDENTITY()");
DataRow fightRow = fightData.Rows[0];
return Convert.ToInt32(fightRow[0]);
}
public void deleteFight(int id)
{
SelectByDataAdapter("DELETE FROM Fight Where Id=" + id);
}
public void updateFights(Fight fight)
{
String request = "Select * From Fight Where Id=" + fight.ID;
DataTable dataTable = SelectByDataAdapter(request);
if (dataTable.Rows.Count == 1)
{
dataTable.Rows[0]["name"] = fight.Name;
dataTable.Rows[0]["id_challenger1"] = fight.Challenger1.ID;
dataTable.Rows[0]["id_challenger2"] = fight.Challenger2.ID;
dataTable.Rows[0]["id_winner"] = fight.Winner.ID;
dataTable.Rows[0]["id_war"] = fight.War.ID;
UpdateByCommandBuilder(request, dataTable);
}
}
public int addTerritory(Territory territory)
{
DataTable territoryData = SelectByDataAdapter("INSERT INTO Territory VALUES(" + territory.Type + "," + territory.Owner.ID + "); SELECT SCOPE_IDENTITY()");
DataRow territoryRow = territoryData.Rows[0];
return Convert.ToInt32(territoryRow[0]);
}
public void deleteTerritory(int id)
{
SelectByDataAdapter("DELETE FROM Territory Where Id=" + id);
}