Test
This commit is contained in:
@@ -30,11 +30,12 @@ namespace Nevyn.Classes
|
|||||||
connection = new MySqlConnection(connectionString);
|
connection = new MySqlConnection(connectionString);
|
||||||
}
|
}
|
||||||
//open connection to database
|
//open connection to database
|
||||||
private bool OpenConnection()
|
public bool OpenConnection()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
connection.Open();
|
if (connection.State != System.Data.ConnectionState.Open)
|
||||||
|
connection.Open();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (MySqlException ex)
|
catch (MySqlException ex)
|
||||||
@@ -59,7 +60,7 @@ namespace Nevyn.Classes
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Close connection
|
//Close connection
|
||||||
private bool CloseConnection()
|
public bool CloseConnection()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -112,5 +113,98 @@ namespace Nevyn.Classes
|
|||||||
return wallet;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
50
Nevyn/Controllers/BudgetController.cs
Normal file
50
Nevyn/Controllers/BudgetController.cs
Normal file
@@ -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<ActionResult<BudgetDTO>> 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",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -15,7 +15,6 @@ namespace Nevyn.Controllers
|
|||||||
public class MoneyController : ControllerBase
|
public class MoneyController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly MoneyContext _context;
|
private readonly MoneyContext _context;
|
||||||
|
|
||||||
public MoneyController(MoneyContext context)
|
public MoneyController(MoneyContext context)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
|||||||
55
Nevyn/Models/Budget.cs
Normal file
55
Nevyn/Models/Budget.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
|
||||||
|
namespace Nevyn.Models
|
||||||
|
{
|
||||||
|
public class Budget
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public List<Category> 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<BudgetItem> 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<Category> Categories { get; set; }
|
||||||
|
public string Debug { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
16
Nevyn/Models/BudgetContext.cs
Normal file
16
Nevyn/Models/BudgetContext.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Nevyn.Models
|
||||||
|
{
|
||||||
|
public class BudgetContext : DbContext
|
||||||
|
{
|
||||||
|
public BudgetContext(DbContextOptions<BudgetContext> options) : base(options)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<Budget> UpdateBudget { get; set; } = null!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -55,9 +55,7 @@ namespace Nevyn.Models
|
|||||||
this.Kort = dbWallet.Kort;
|
this.Kort = dbWallet.Kort;
|
||||||
this.Spara = dbWallet.Spara;
|
this.Spara = dbWallet.Spara;
|
||||||
this.Buffert = dbWallet.Buffert;
|
this.Buffert = dbWallet.Buffert;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ builder.Services.AddDbContext<ShoppingContext>(opt =>
|
|||||||
opt.UseInMemoryDatabase("ShoppingList"));
|
opt.UseInMemoryDatabase("ShoppingList"));
|
||||||
builder.Services.AddDbContext<MoneyContext>(opt =>
|
builder.Services.AddDbContext<MoneyContext>(opt =>
|
||||||
opt.UseInMemoryDatabase("Wallet"));
|
opt.UseInMemoryDatabase("Wallet"));
|
||||||
|
builder.Services.AddDbContext<BudgetContext>(opt =>
|
||||||
|
opt.UseInMemoryDatabase("Budget"));
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user