From 628f25a8be05b9eed9df081cc0f204d4fda54c97 Mon Sep 17 00:00:00 2001 From: Elias Jansson Date: Mon, 2 Jun 2025 23:52:17 +0200 Subject: [PATCH] More --- Aberwyn/Aberwyn.csproj | 5 -- Aberwyn/Data/BudgetService.cs | 148 ---------------------------------- 2 files changed, 153 deletions(-) delete mode 100644 Aberwyn/Data/BudgetService.cs diff --git a/Aberwyn/Aberwyn.csproj b/Aberwyn/Aberwyn.csproj index 099c10b..92a479f 100644 --- a/Aberwyn/Aberwyn.csproj +++ b/Aberwyn/Aberwyn.csproj @@ -9,11 +9,6 @@ Linux - - - - - diff --git a/Aberwyn/Data/BudgetService.cs b/Aberwyn/Data/BudgetService.cs deleted file mode 100644 index 17421f5..0000000 --- a/Aberwyn/Data/BudgetService.cs +++ /dev/null @@ -1,148 +0,0 @@ -using MySql.Data.MySqlClient; -using System.Collections.Generic; -using Aberwyn.Models; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; // Add this namespace - -namespace Aberwyn.Data -{ - public class BudgetService - { - private readonly IConfiguration _configuration; - private readonly IHostEnvironment _env; // Add this field - - public BudgetService(IConfiguration configuration, IHostEnvironment env) // Update constructor - { - _configuration = configuration; - _env = env; // Initialize the environment field - } - - public MySqlConnection GetConnection() - { - var connectionString = _env.IsDevelopment() // Use the injected environment variable - ? _configuration.GetConnectionString("DefaultConnection") - : _configuration.GetConnectionString("ProductionConnection"); - - return new MySqlConnection(connectionString); - } - - public bool UpdateBudgetItem(BudgetItem item) - { - using (var connection = GetConnection()) - { - connection.Open(); - - string query = @" - UPDATE tblBudgetItems - SET Name = @name, Amount = @amount - WHERE idtblBudgetItems = @id"; - - using (var cmd = new MySqlCommand(query, connection)) - { - cmd.Parameters.AddWithValue("@name", item.Name); - cmd.Parameters.AddWithValue("@amount", item.Amount); - cmd.Parameters.AddWithValue("@id", item.ID); - return cmd.ExecuteNonQuery() > 0; // Returns true if one or more rows are updated - } - } - } - - public List GetBudgetItems(int month, int year) - { - var budgetItems = new List(); - - using (var connection = GetConnection()) - { - connection.Open(); - - string query = @" - SELECT - b.idtblBudgetItems AS id, - b.Name AS item_name, - b.Amount AS amount, - c1.Name AS category, - b.Month, - b.Year, - b.Description AS description - FROM tblBudgetItems b - LEFT JOIN tblCategories c1 ON b.Category = c1.idtblCategories - WHERE b.Month = @month AND b.Year = @year"; - - using (var cmd = new MySqlCommand(query, connection)) - { - cmd.Parameters.AddWithValue("@month", month); - cmd.Parameters.AddWithValue("@year", year); - - using (var reader = cmd.ExecuteReader()) - { - while (reader.Read()) - { - budgetItems.Add(new BudgetItem - { - ID = reader.GetInt32("id"), - Name = reader.GetString("item_name"), // Updated alias - Amount = reader.GetDecimal("amount"), - Category = reader.GetString("category"), - Month = reader.GetInt32("Month"), - Year = reader.GetInt32("Year"), - Description = reader.IsDBNull(reader.GetOrdinal("description")) - ? null - : reader.GetString("description") - }); - } - } - } - } - - return budgetItems; - } - - public bool AddBudgetItem(BudgetItem item) - { - using (var connection = GetConnection()) - { - connection.Open(); - - string query = @" - INSERT INTO tblBudgetItems (Name, Amount, Category, Month, Year) - VALUES (@name, @amount, @category, @month, @year)"; - - using (var cmd = new MySqlCommand(query, connection)) - { - cmd.Parameters.AddWithValue("@name", item.Name); - cmd.Parameters.AddWithValue("@amount", item.Amount); - cmd.Parameters.AddWithValue("@category", item.Category); - cmd.Parameters.AddWithValue("@month", item.Month); - cmd.Parameters.AddWithValue("@year", item.Year); - return cmd.ExecuteNonQuery() > 0; // Returns true if a row was inserted - } - } - }*/ - - // New method to fetch all categories - public List GetCategories() - { - var categories = new List(); - - using (var connection = GetConnection()) - { - connection.Open(); - - string query = "SELECT Name FROM tblCategories"; // Adjust based on your table structure - - using (var cmd = new MySqlCommand(query, connection)) - { - using (var reader = cmd.ExecuteReader()) - { - while (reader.Read()) - { - categories.Add(reader.GetString("Name")); - } - } - } - } - - return categories; - } - } -}