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

View File

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

@ -21,8 +21,6 @@ namespace BlueWest.WebApi.Controllers
_dbContext = dbContext; _dbContext = dbContext;
} }
#region Users
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[HttpGet] [HttpGet]
@ -60,7 +58,7 @@ namespace BlueWest.WebApi.Controllers
[HttpPost] [HttpPost]
public ActionResult AddUser(UserUpdateDto userUpdateDto) 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.Users.Add(user);
_dbContext.SaveChanges(); _dbContext.SaveChanges();
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user); return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
@ -90,44 +88,6 @@ namespace BlueWest.WebApi.Controllers
return Ok(); 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,13 +3,12 @@ using BlueWest.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.MySQL; namespace BlueWest.WebApi.MySQL
{
public sealed class UserDbContext : DbContext public sealed class UserDbContext : DbContext
{ {
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
public DbSet<FinanceTransaction> Transactions { get; set; }
public DbSet<FinanceTransactionType> TransactionTypes { get; set; }
public IConfiguration Configuration; public IConfiguration Configuration;
@ -32,12 +31,13 @@ public sealed class UserDbContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>(builder => modelBuilder.Entity<User>(builder =>
{ {
builder.HasKey(x => x.Id); builder.HasKey(x => x.Id);
}); });
modelBuilder.Entity<FinanceTransaction>(builder => modelBuilder.Entity<FinanceOp>(builder =>
{ {
builder.HasOne<User>() builder.HasOne<User>()
.WithMany(x => x.FinanceTransactions) .WithMany(x => x.FinanceTransactions)
@ -46,5 +46,6 @@ public sealed class UserDbContext : DbContext
}
} }
} }

View File

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

View File

@ -1,19 +1,36 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using MapTo; using MapTo;
namespace BlueWest.Data namespace BlueWest.Data
{ {
[MapFrom( typeof(Country))] [MapFrom( typeof(Country))]
public partial class CountryCreate public partial class CountryCreate
{ {
// ISO 3166-1 numeric code // ISO 3166-1 numeric code
public int Code { get; set; } // Primary key public int Code { get; set; } // Primary key
public string StateName { get; set; } public string StateName { get; set; }
public List<CurrencyCreate> CurrenciesToCreate { get; set; }
[MaxLength(2)] public string Alpha2Code { get; set; } [MaxLength(2)] public string Alpha2Code { get; set; }
public string TLD { 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 StateName { get; set; }
public string Alpha2Code { get; set; } public string Alpha2Code { get; set; }
public string TLD { get; set; } public string TLD { get; set; }
public CountryUpdate() { }
} }
} }

View File

@ -10,7 +10,7 @@ namespace BlueWest.Data
public partial class Currency 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; } [MaxLength(3)] public string Code { get; set; }
public List<Country> Countries { get; set; } public List<Country> Countries { get; set; }

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using MapTo; using MapTo;
@ -6,8 +7,25 @@ namespace BlueWest.Data
[MapFrom(typeof(Currency))] [MapFrom(typeof(Currency))]
public partial class CurrencyCreate public partial class CurrencyCreate
{ {
[MaxLength(3)] public int Num { get; set; } // Primary key public int Num { get; set; } // Primary key
[MaxLength(3)] public string Code { get; set; } [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

@ -11,6 +11,9 @@ namespace BlueWest.Data
// ISO 4217 Code // 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 namespace BlueWest.Data
{ {
[MapFrom(typeof(FinanceTransactionInsertDto))] [MapFrom(typeof(FinanceOpCreate))]
public partial class FinanceTransaction public partial class FinanceOp
{ {
public TimeSpan UserId { get; set; } public TimeSpan UserId { get; set; }
@ -14,23 +14,20 @@ namespace BlueWest.Data
public Currency Currency { get; } public Currency Currency { get; }
public FinanceTransactionType Type {get;} public FinanceOpType Type {get;}
private string Description {get;} private string Description {get;}
public FinanceTransaction() public FinanceOp() { }
{
} public FinanceOp(TimeSpan creationDate, TimeSpan userId,
Currency currency, FinanceOpType financeOpType)
public FinanceTransaction(TimeSpan creationDate, TimeSpan userId,
Currency currency, FinanceTransactionType financeTransactionType)
{ {
CreationDate = creationDate; CreationDate = creationDate;
UserId = userId; UserId = userId;
Currency = currency; Currency = currency;
Type = financeTransactionType; Type = financeOpType;
} }
} }
} }

View File

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

View File

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

View File

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

View File

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