From 3c22d881ae2849e48fa2b93ba5e443b271850326 Mon Sep 17 00:00:00 2001 From: elias Date: Sun, 6 Aug 2023 23:12:45 +0200 Subject: [PATCH] Test --- Nevyn/Classes/Mysql.cs | 100 +++++++++++++++++++++++++- Nevyn/Controllers/BudgetController.cs | 50 +++++++++++++ Nevyn/Controllers/MoneyController.cs | 1 - Nevyn/Models/Budget.cs | 55 ++++++++++++++ Nevyn/Models/BudgetContext.cs | 16 +++++ Nevyn/Models/Money.cs | 6 +- Nevyn/Program.cs | 2 + 7 files changed, 222 insertions(+), 8 deletions(-) create mode 100644 Nevyn/Controllers/BudgetController.cs create mode 100644 Nevyn/Models/Budget.cs create mode 100644 Nevyn/Models/BudgetContext.cs diff --git a/Nevyn/Classes/Mysql.cs b/Nevyn/Classes/Mysql.cs index c1c46a8..1e419ea 100644 --- a/Nevyn/Classes/Mysql.cs +++ b/Nevyn/Classes/Mysql.cs @@ -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 getBudgetItems(int ID) + { + string query = "SELECT * FROM tblBudgetItems WHERE category='" + ID + "'"; + + //Create a list to store the result + List budgetItems = new List(); + + //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 getBudgetCategories() + { + string query = "SELECT * FROM tblCategories"; + + //Create a list to store the result + List categoryList = new List(); + + //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; + } + } } } diff --git a/Nevyn/Controllers/BudgetController.cs b/Nevyn/Controllers/BudgetController.cs new file mode 100644 index 0000000..35d1dfa --- /dev/null +++ b/Nevyn/Controllers/BudgetController.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Nevyn.Models; +using Nevyn.Classes; + +namespace Nevyn.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class BudgetController : ControllerBase + { + private readonly BudgetContext _context; + public BudgetController(BudgetContext context) + { + _context = context; + } + + // GET: api/ShoppingLists + [HttpGet] + public async Task> GetBudget() + { + //Budget budget = await _context.UpdateBudget.FindAsync(1); + + Budget budget = new Budget(); + budget.updateFromDatabase(); + //_context.UpdateBudget.Add(budget); + //await _context.SaveChangesAsync(); + + + return ItemToDTO(budget); + } + + + private static BudgetDTO ItemToDTO(Budget budget) => + new BudgetDTO + { + + Categories = budget.Categories, + Debug = "There is days left", + }; + } + + +} + diff --git a/Nevyn/Controllers/MoneyController.cs b/Nevyn/Controllers/MoneyController.cs index 0b2e863..14bb504 100644 --- a/Nevyn/Controllers/MoneyController.cs +++ b/Nevyn/Controllers/MoneyController.cs @@ -15,7 +15,6 @@ namespace Nevyn.Controllers public class MoneyController : ControllerBase { private readonly MoneyContext _context; - public MoneyController(MoneyContext context) { _context = context; diff --git a/Nevyn/Models/Budget.cs b/Nevyn/Models/Budget.cs new file mode 100644 index 0000000..320ccad --- /dev/null +++ b/Nevyn/Models/Budget.cs @@ -0,0 +1,55 @@ +using System; +using MySql.Data.MySqlClient; + +namespace Nevyn.Models +{ + public class Budget + { + public int Id { get; set; } + public List Categories { get; set; } + public Budget() + { + } + + //public void getBudgetFromSQL() + //this = Classes.Mysql.getBudget(); + + //} + + public void updateFromDatabase() + { + Classes.Mysql mysql = new Classes.Mysql(); + + this.Categories = mysql.getBudgetCategories(); + } + } + + public class Category + { + public int ID { get; set; } + public string? Name { get; set; } + public int parent { get; set; } + public List Items { get; set; } + } + + public class BudgetItem + { + public int ID { get; set; } + public string? Name { get; set; } + //public user User { get; set; } + public int Amount { get; set; } + public string? Description { get; set; } + public bool Recurring { get; set; } + public int Month { get; set; } + public int Year { get; set; } + + } + + + public class BudgetDTO + { + public List Categories { get; set; } + public string Debug { get; set; } + } +} + diff --git a/Nevyn/Models/BudgetContext.cs b/Nevyn/Models/BudgetContext.cs new file mode 100644 index 0000000..f203ecf --- /dev/null +++ b/Nevyn/Models/BudgetContext.cs @@ -0,0 +1,16 @@ +using System; +using Microsoft.EntityFrameworkCore; + +namespace Nevyn.Models +{ + public class BudgetContext : DbContext + { + public BudgetContext(DbContextOptions options) : base(options) + { + + } + + public DbSet UpdateBudget { get; set; } = null!; + } +} + diff --git a/Nevyn/Models/Money.cs b/Nevyn/Models/Money.cs index da02884..c5617bd 100644 --- a/Nevyn/Models/Money.cs +++ b/Nevyn/Models/Money.cs @@ -54,10 +54,8 @@ namespace Nevyn.Models Wallet dbWallet = mysql.SelectWallet(); this.Kort = dbWallet.Kort; this.Spara = dbWallet.Spara; - this.Buffert = dbWallet.Buffert; - - - } + this.Buffert = dbWallet.Buffert; + } } diff --git a/Nevyn/Program.cs b/Nevyn/Program.cs index 615bfcd..dc3deb8 100644 --- a/Nevyn/Program.cs +++ b/Nevyn/Program.cs @@ -15,6 +15,8 @@ builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("ShoppingList")); builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("Wallet")); +builder.Services.AddDbContext(opt => + opt.UseInMemoryDatabase("Budget")); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();