Refactor controllers and data

This commit is contained in:
CodeLiturgy 2022-08-18 20:59:13 +01:00
parent abe9a7587b
commit d12e0b2f6e
18 changed files with 214 additions and 126 deletions

View File

@ -12,7 +12,7 @@ namespace BlueWest.WebApi.Controllers
/// </summary>
[ApiController]
[Route("[controller]")]
public class CountriesController : ControllerBase
public class CountryController : ControllerBase
{
private readonly CountriesDbContext _dbContext;
@ -20,7 +20,7 @@ public class CountriesController : ControllerBase
/// Controller responsible for handling country data in the Country table
/// </summary>
/// <param name="dbContext"></param>
public CountriesController(CountriesDbContext dbContext)
public CountryController(CountriesDbContext dbContext)
{
_dbContext = dbContext;
}
@ -50,9 +50,10 @@ public class CountriesController : ControllerBase
[HttpPost]
public ActionResult AddCountry(CountryCreate countryToCreate)
{
_dbContext.Countries.Add(new Country(countryToCreate, new List<Currency>()));
var newCountry = new Country(countryToCreate, new List<Currency>());
_dbContext.Countries.Add(newCountry);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetCountryById), new {countryId = countryToCreate.Code}, countryToCreate);
return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Code}, newCountry);
}
/// <summary>

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using BlueWest.Data;
using BlueWest.WebApi.MySQL;
@ -12,29 +13,49 @@ namespace BlueWest.WebApi.Controllers
/// </summary>
[ApiController]
[Route("[controller]")]
public class CurrenciesController : ControllerBase
public class CurrencyController : ControllerBase
{
private readonly CountriesDbContext _dbContext;
public CurrenciesController(CountriesDbContext dbContext)
public CurrencyController(CountriesDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// Gets the currency data from currency table
/// </summary>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet]
public ActionResult GetCurrencies()
{
var dbContext = _dbContext.Currencies;
if (dbContext != null)
{
return Ok(dbContext.ToArray());
}
return new NotFoundResult();
}
/// <summary>
/// Add Currency to the table of currencies
/// </summary>
/// <param name="currencyToCreate">Currency data to create</param>
/// <param name="currencyAssociatedCountries">Countries</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost]
public ActionResult AddCurrency(CurrencyCreate currencyToCreate)
{
var newCurrency = new Currency(currencyToCreate, new List<Country>());
var newCurrency = currencyToCreate.ToCurrency();
_dbContext.Currencies.Add(newCurrency);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currencyToCreate.Code}, currencyToCreate);
return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, newCurrency);
}
/// <summary>

View File

@ -0,0 +1,68 @@
using System;
using BlueWest.Data;
using BlueWest.WebApi.MySQL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BlueWest.WebApi.Controllers;
/// <summary>
/// The controller responsible to fetch currency data
/// </summary>
[ApiController]
[Route("[controller]")]
public class FinanceController : ControllerBase
{
private readonly FinanceDbContext _dbContext;
public FinanceController(FinanceDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// Returns a transaction by the provided id
/// </summary>
/// <param name="userId"></param>
/// <param name="transactionId"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions/{transactionId}")]
public ActionResult GetTransactionsById(int userId, TimeSpan transactionId)
{
return new NotFoundResult();
}
/// <summary>
/// Posts a finance transaction
/// </summary>
/// <param name="userId"></param>
/// <param name="financeTransaction"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{userId}/transactions")]
public ActionResult PostTransaction(int userId, FinanceOpCreate financeTransaction)
{
return new BadRequestResult();
}
/// <summary>
/// Get Transactions
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions")]
public ActionResult GetTransactions(int userId)
{
return Ok();
}
}

View File

@ -20,8 +20,6 @@ namespace BlueWest.WebApi.Controllers
{
_dbContext = dbContext;
}
#region Users
[ProducesResponseType(StatusCodes.Status200OK)]
@ -60,7 +58,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost]
public ActionResult AddUser(UserUpdateDto userUpdateDto)
{
var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List<FinanceTransaction>());
var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List<FinanceOp>());
_dbContext.Users.Add(user);
_dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
@ -89,45 +87,7 @@ namespace BlueWest.WebApi.Controllers
_dbContext.SaveChanges();
return Ok();
}
#endregion
#region Transactions
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions/{transactionId}")]
public ActionResult GetTransactionsById(int userId, TimeSpan transactionId)
{
return new NotFoundResult();
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{userId}/transactions")]
public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction)
{
return new BadRequestResult();
}
/// <summary>
/// Get Transactions
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{userId}/transactions")]
public ActionResult GetTransactions(int userId)
{
return Ok();
}
#endregion
}
}

View File

@ -0,0 +1,12 @@
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
namespace BlueWest.WebApi.MySQL
{
public class FinanceDbContext
{
public DbSet<FinanceOp> Transactions { get; set; }
public DbSet<FinanceOpType> TransactionTypes { get; set; }
}
}

View File

@ -3,48 +3,49 @@ using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.MySQL;
public sealed class UserDbContext : DbContext
namespace BlueWest.WebApi.MySQL
{
public DbSet<User> Users { get; set; }
public DbSet<FinanceTransaction> Transactions { get; set; }
public DbSet<FinanceTransactionType> TransactionTypes { get; set; }
public IConfiguration Configuration;
public sealed class UserDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public IConfiguration Configuration;
public UserDbContext(DbContextOptions<UserDbContext> options) : base(options)
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
/*optionsBuilder.UseMySql(
Configuration.GetConnectionString("LocalMySQL"),
new MySqlServerVersion(new Version(8, 0, 11))
);*/
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>(builder =>
public UserDbContext(DbContextOptions<UserDbContext> options) : base(options)
{
builder.HasKey(x => x.Id);
});
Database.EnsureCreated();
modelBuilder.Entity<FinanceTransaction>(builder =>
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
builder.HasOne<User>()
.WithMany(x => x.FinanceTransactions)
.HasForeignKey(x => x.UserId);
});
/*optionsBuilder.UseMySql(
Configuration.GetConnectionString("LocalMySQL"),
new MySqlServerVersion(new Version(8, 0, 11))
);*/
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>(builder =>
{
builder.HasKey(x => x.Id);
});
modelBuilder.Entity<FinanceOp>(builder =>
{
builder.HasOne<User>()
.WithMany(x => x.FinanceTransactions)
.HasForeignKey(x => x.UserId);
});
}
}
}
}

View File

@ -1,10 +1,10 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using MapTo;
namespace BlueWest.Data
{
[MapFrom(new [] {typeof(CountryUpdate), typeof(CountryCreate)})]
public partial class Country
{
@ -17,6 +17,7 @@ namespace BlueWest.Data
public List<Currency> Currencies { get; set; }
[JsonConstructor]
public Country(int code, string stateName, string tld, List<Currency> currencies)
{
Code = code;
@ -25,10 +26,7 @@ namespace BlueWest.Data
Currencies = currencies;
}
public Country()
{
}
public Country() { }
}
}

View File

@ -1,19 +1,36 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using MapTo;
namespace BlueWest.Data
{
[MapFrom( typeof(Country))]
[MapFrom( typeof(Country))]
public partial class CountryCreate
{
// ISO 3166-1 numeric code
public int Code { get; set; } // Primary key
public string StateName { get; set; }
public List<CurrencyCreate> CurrenciesToCreate { get; set; }
[MaxLength(2)] public string Alpha2Code { get; set; }
public string TLD { get; set; }
public CountryCreate() { }
public Country ToCountry()
{
var currencies = new List<Currency>();
foreach (var currencyCreate in CurrenciesToCreate)
{
currencies.Add(new Currency(currencyCreate, null));
}
return new Country(this, currencies);
}
}
}

View File

@ -11,6 +11,8 @@ namespace BlueWest.Data
public string StateName { get; set; }
public string Alpha2Code { get; set; }
public string TLD { get; set; }
public CountryUpdate() { }
}
}

View File

@ -10,7 +10,7 @@ namespace BlueWest.Data
public partial class Currency
{
[MaxLength(3)] public int Num { get; set; } // Primary key
public int Num { get; set; } // Primary key
[MaxLength(3)] public string Code { get; set; }
public List<Country> Countries { get; set; }

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using MapTo;
@ -6,8 +7,25 @@ namespace BlueWest.Data
[MapFrom(typeof(Currency))]
public partial class CurrencyCreate
{
[MaxLength(3)] public int Num { get; set; } // Primary key
[MaxLength(3)] public string Code { get; set; }
public int Num { get; set; } // Primary key
[MaxLength(3)] public string Code { get; set; }
public List<CountryCreate> CountriesToCreate { get; set; }
public CurrencyCreate() { }
public Currency ToCurrency()
{
List<Country> countries = new List<Country>();
foreach (var countryCreate in CountriesToCreate)
{
var newCountry = new Country(countryCreate, null);
countries.Add(newCountry);
}
return new Currency(this, countries);
}
}
}

View File

@ -9,8 +9,11 @@ namespace BlueWest.Data
public partial class CurrencyUpdate
{
// ISO 4217 Code
[MaxLength(3)] public string Code { get; set; }
[MaxLength(3)] public string Code { get; set; }
public CurrencyUpdate()
{
}
}
}

View File

@ -1,7 +0,0 @@
namespace BlueWest.Data;
public class TestData
{
public int Id { get; set; }
public string Name { get; set; }
}

View File

@ -4,8 +4,8 @@ using MapTo;
namespace BlueWest.Data
{
[MapFrom(typeof(FinanceTransactionInsertDto))]
public partial class FinanceTransaction
[MapFrom(typeof(FinanceOpCreate))]
public partial class FinanceOp
{
public TimeSpan UserId { get; set; }
@ -14,23 +14,20 @@ namespace BlueWest.Data
public Currency Currency { get; }
public FinanceTransactionType Type {get;}
public FinanceOpType Type {get;}
private string Description {get;}
public FinanceTransaction()
{
}
public FinanceOp() { }
public FinanceTransaction(TimeSpan creationDate, TimeSpan userId,
Currency currency, FinanceTransactionType financeTransactionType)
public FinanceOp(TimeSpan creationDate, TimeSpan userId,
Currency currency, FinanceOpType financeOpType)
{
CreationDate = creationDate;
UserId = userId;
Currency = currency;
Type = financeTransactionType;
Type = financeOpType;
}
}
}

View File

@ -5,30 +5,27 @@ using MapTo;
namespace BlueWest.Data
{
[MapFrom( typeof(FinanceTransaction))]
[MapFrom( typeof(FinanceOp))]
public partial class FinanceTransactionInsertDto
public partial class FinanceOpCreate
{
public TimeSpan UserId { get; set; }
public Currency Currency { get; }
public FinanceTransactionType FinanceTransactionType {get;}
public FinanceOpType FinanceOpType {get;}
public TimeSpan CreationDate { get; set; }
public FinanceTransactionInsertDto(
public FinanceOpCreate(
TimeSpan userId,
Currency currency ,
FinanceTransactionType financeTransactionType
FinanceOpType financeOpType
)
{
Currency = currency;
UserId = userId;
Currency = currency;
FinanceTransactionType = financeTransactionType;
FinanceOpType = financeOpType;
}
}
}

View File

@ -5,7 +5,7 @@ using MapTo;
namespace BlueWest.Data
{
[MapFrom(typeof(FinanceTransaction))]
[MapFrom(typeof(FinanceOp))]
public partial class FinanceTransactionReadDto
{
@ -13,7 +13,7 @@ namespace BlueWest.Data
public Currency Currency { get; }
public FinanceTransactionType FinanceTransactionType {get;}
public FinanceOpType FinanceOpType {get;}
public TimeSpan CreationDate { get; set; }
}

View File

@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations;
namespace BlueWest.Data;
public class FinanceTransactionType
public class FinanceOpType
{
[Key] public int Id { get; set; }

View File

@ -13,7 +13,7 @@ namespace BlueWest.Data
public TimeSpan Id { get; } = TimeSpan.Zero;
public string Name { get; set; }
public List<FinanceTransaction> FinanceTransactions { get; }
public List<FinanceOp> FinanceTransactions { get; }
public User(TimeSpan id, string name)