This commit is contained in:
elias
2023-08-06 23:12:45 +02:00
parent c8242217a5
commit 3c22d881ae
7 changed files with 222 additions and 8 deletions

View File

@@ -30,11 +30,12 @@ namespace Nevyn.Classes
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
public bool OpenConnection()
{
try
{
connection.Open();
if (connection.State != System.Data.ConnectionState.Open)
connection.Open();
return true;
}
catch (MySqlException ex)
@@ -59,7 +60,7 @@ namespace Nevyn.Classes
}
//Close connection
private bool CloseConnection()
public bool CloseConnection()
{
try
{
@@ -112,5 +113,98 @@ namespace Nevyn.Classes
return wallet;
}
}
public List<Models.BudgetItem> getBudgetItems(int ID)
{
string query = "SELECT * FROM tblBudgetItems WHERE category='" + ID + "'";
//Create a list to store the result
List<Models.BudgetItem> budgetItems = new List<Models.BudgetItem>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
Models.BudgetItem item = new Models.BudgetItem();
item.ID = (int)dataReader["idtblBudgetItems"];
item.Name = (string)dataReader["Name"];
item.Amount = (int)dataReader["Amount"];
item.Description = (string)dataReader["Description"];
item.Month = (int)dataReader["Month"];
item.Year = (int)dataReader["Year"];
//item.User = (int)dataReader["UserID"];
budgetItems.Add(item);
}
//close Data Reader
dataReader.Close();
//close Connection
//this.CloseConnection();
//return list to be displayed
return budgetItems;
}
else
{
return budgetItems;
}
}
public List<Models.Category> getBudgetCategories()
{
string query = "SELECT * FROM tblCategories";
//Create a list to store the result
List<Models.Category> categoryList = new List<Models.Category>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
Models.Category category = new Models.Category();
category.ID = (int)dataReader["idtblCategories"];
category.Name = (string)dataReader["Name"];
category.parent = (int)dataReader["parent"];
categoryList.Add(category);
}
//close Data Reader
dataReader.Close();
for (var i = 0; i < categoryList.Count(); i++)
{
categoryList[i].Items = this.getBudgetItems(categoryList[i].ID);
}
//close Connection
this.CloseConnection();
//return list to be displayed
return categoryList;
}
else
{
return categoryList;
}
}
}
}