From d12e0b2f6e0d7f82aa7d0851eede9e603545ef4c Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Thu, 18 Aug 2022 20:59:13 +0100 Subject: [PATCH] Refactor controllers and data --- ...riesController.cs => CountryController.cs} | 9 +-- ...iesController.cs => CurrencyController.cs} | 29 ++++++-- BlueWest.Api/Controllers/FinanceController.cs | 68 ++++++++++++++++++ BlueWest.Api/Controllers/UserController.cs | 42 +---------- BlueWest.Api/MySQL/FinanceDbContext.cs | 12 ++++ BlueWest.Api/MySQL/UserDbContext.cs | 69 ++++++++++--------- BlueWest.Data/Country/Country.cs | 8 +-- BlueWest.Data/Country/CountryCreate.cs | 21 +++++- BlueWest.Data/Country/CountryUpdate.cs | 2 + BlueWest.Data/Currency/Currency.cs | 2 +- BlueWest.Data/Currency/CurrencyCreate.cs | 22 +++++- BlueWest.Data/Currency/CurrencyUpdate.cs | 9 ++- BlueWest.Data/Currency/TestData.cs | 7 -- .../{FinanceTransaction.cs => FinanceOp.cs} | 17 ++--- ...sactionInsertDto.cs => FinanceOpCreate.cs} | 15 ++-- ...TransactionReadDto.cs => FinanceOpRead.cs} | 4 +- ...nceTransactionType.cs => FinanceOpType.cs} | 2 +- BlueWest.Data/User/User.cs | 2 +- 18 files changed, 214 insertions(+), 126 deletions(-) rename BlueWest.Api/Controllers/{CountriesController.cs => CountryController.cs} (92%) rename BlueWest.Api/Controllers/{CurrenciesController.cs => CurrencyController.cs} (75%) create mode 100644 BlueWest.Api/Controllers/FinanceController.cs create mode 100644 BlueWest.Api/MySQL/FinanceDbContext.cs delete mode 100644 BlueWest.Data/Currency/TestData.cs rename BlueWest.Data/Transaction/{FinanceTransaction.cs => FinanceOp.cs} (53%) rename BlueWest.Data/Transaction/{FinanceTransactionInsertDto.cs => FinanceOpCreate.cs} (52%) rename BlueWest.Data/Transaction/{FinanceTransactionReadDto.cs => FinanceOpRead.cs} (75%) rename BlueWest.Data/Transaction/{FinanceTransactionType.cs => FinanceOpType.cs} (82%) diff --git a/BlueWest.Api/Controllers/CountriesController.cs b/BlueWest.Api/Controllers/CountryController.cs similarity index 92% rename from BlueWest.Api/Controllers/CountriesController.cs rename to BlueWest.Api/Controllers/CountryController.cs index 4f4aa2e..38a60d8 100644 --- a/BlueWest.Api/Controllers/CountriesController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -12,7 +12,7 @@ namespace BlueWest.WebApi.Controllers /// [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 /// /// - 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())); + var newCountry = new Country(countryToCreate, new List()); + _dbContext.Countries.Add(newCountry); _dbContext.SaveChanges(); - return CreatedAtRoute(nameof(GetCountryById), new {countryId = countryToCreate.Code}, countryToCreate); + return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Code}, newCountry); } /// diff --git a/BlueWest.Api/Controllers/CurrenciesController.cs b/BlueWest.Api/Controllers/CurrencyController.cs similarity index 75% rename from BlueWest.Api/Controllers/CurrenciesController.cs rename to BlueWest.Api/Controllers/CurrencyController.cs index d22fad3..39cb66e 100644 --- a/BlueWest.Api/Controllers/CurrenciesController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -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 /// [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; } + /// + /// Gets the currency data from currency table + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet] + public ActionResult GetCurrencies() + { + var dbContext = _dbContext.Currencies; + + if (dbContext != null) + { + return Ok(dbContext.ToArray()); + } + + return new NotFoundResult(); + } /// /// Add Currency to the table of currencies /// /// Currency data to create + /// Countries /// [ProducesResponseType(StatusCodes.Status201Created)] [HttpPost] public ActionResult AddCurrency(CurrencyCreate currencyToCreate) { - var newCurrency = new Currency(currencyToCreate, new List()); + + 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); } /// diff --git a/BlueWest.Api/Controllers/FinanceController.cs b/BlueWest.Api/Controllers/FinanceController.cs new file mode 100644 index 0000000..0471a35 --- /dev/null +++ b/BlueWest.Api/Controllers/FinanceController.cs @@ -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; + +/// +/// The controller responsible to fetch currency data +/// +[ApiController] +[Route("[controller]")] +public class FinanceController : ControllerBase +{ + private readonly FinanceDbContext _dbContext; + + public FinanceController(FinanceDbContext dbContext) + { + _dbContext = dbContext; + } + + + /// + /// Returns a transaction by the provided id + /// + /// + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [HttpGet("{userId}/transactions/{transactionId}")] + public ActionResult GetTransactionsById(int userId, TimeSpan transactionId) + { + return new NotFoundResult(); + } + + /// + /// Posts a finance transaction + /// + /// + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [HttpPost("{userId}/transactions")] + public ActionResult PostTransaction(int userId, FinanceOpCreate financeTransaction) + { + return new BadRequestResult(); + } + + + /// + /// Get Transactions + /// + /// + /// + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [HttpGet("{userId}/transactions")] + public ActionResult GetTransactions(int userId) + { + return Ok(); + } + + +} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/UserController.cs b/BlueWest.Api/Controllers/UserController.cs index c6b320a..8fd01af 100644 --- a/BlueWest.Api/Controllers/UserController.cs +++ b/BlueWest.Api/Controllers/UserController.cs @@ -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()); + var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List()); _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(); - } - - - /// - /// Get Transactions - /// - /// - /// - - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [HttpGet("{userId}/transactions")] - public ActionResult GetTransactions(int userId) - { - return Ok(); - } - - - #endregion } } \ No newline at end of file diff --git a/BlueWest.Api/MySQL/FinanceDbContext.cs b/BlueWest.Api/MySQL/FinanceDbContext.cs new file mode 100644 index 0000000..ab462d7 --- /dev/null +++ b/BlueWest.Api/MySQL/FinanceDbContext.cs @@ -0,0 +1,12 @@ +using BlueWest.Data; +using Microsoft.EntityFrameworkCore; + +namespace BlueWest.WebApi.MySQL +{ + public class FinanceDbContext + { + public DbSet Transactions { get; set; } + public DbSet TransactionTypes { get; set; } + } +} + diff --git a/BlueWest.Api/MySQL/UserDbContext.cs b/BlueWest.Api/MySQL/UserDbContext.cs index 937c531..14e7f8a 100644 --- a/BlueWest.Api/MySQL/UserDbContext.cs +++ b/BlueWest.Api/MySQL/UserDbContext.cs @@ -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 Users { get; set; } - public DbSet Transactions { get; set; } - public DbSet TransactionTypes { get; set; } - - public IConfiguration Configuration; + public sealed class UserDbContext : DbContext + { + public DbSet Users { get; set; } + + + public IConfiguration Configuration; - public UserDbContext(DbContextOptions 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(builder => + public UserDbContext(DbContextOptions options) : base(options) { - builder.HasKey(x => x.Id); - }); + Database.EnsureCreated(); - modelBuilder.Entity(builder => + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - builder.HasOne() - .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(builder => + { + builder.HasKey(x => x.Id); + }); + + modelBuilder.Entity(builder => + { + builder.HasOne() + .WithMany(x => x.FinanceTransactions) + .HasForeignKey(x => x.UserId); + }); + } } -} \ No newline at end of file +} diff --git a/BlueWest.Data/Country/Country.cs b/BlueWest.Data/Country/Country.cs index 1530c54..5f05747 100644 --- a/BlueWest.Data/Country/Country.cs +++ b/BlueWest.Data/Country/Country.cs @@ -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 Currencies { get; set; } + [JsonConstructor] public Country(int code, string stateName, string tld, List currencies) { Code = code; @@ -25,10 +26,7 @@ namespace BlueWest.Data Currencies = currencies; } - public Country() - { - - } + public Country() { } } } diff --git a/BlueWest.Data/Country/CountryCreate.cs b/BlueWest.Data/Country/CountryCreate.cs index 9bcb72b..e8c8491 100644 --- a/BlueWest.Data/Country/CountryCreate.cs +++ b/BlueWest.Data/Country/CountryCreate.cs @@ -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 CurrenciesToCreate { get; set; } + [MaxLength(2)] public string Alpha2Code { get; set; } + public string TLD { get; set; } + public CountryCreate() { } + + public Country ToCountry() + { + var currencies = new List(); + + foreach (var currencyCreate in CurrenciesToCreate) + { + currencies.Add(new Currency(currencyCreate, null)); + } + + return new Country(this, currencies); + } + } } diff --git a/BlueWest.Data/Country/CountryUpdate.cs b/BlueWest.Data/Country/CountryUpdate.cs index 5b676d3..40323dd 100644 --- a/BlueWest.Data/Country/CountryUpdate.cs +++ b/BlueWest.Data/Country/CountryUpdate.cs @@ -11,6 +11,8 @@ namespace BlueWest.Data public string StateName { get; set; } public string Alpha2Code { get; set; } public string TLD { get; set; } + + public CountryUpdate() { } } } diff --git a/BlueWest.Data/Currency/Currency.cs b/BlueWest.Data/Currency/Currency.cs index 5d95c36..6ca39a2 100644 --- a/BlueWest.Data/Currency/Currency.cs +++ b/BlueWest.Data/Currency/Currency.cs @@ -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 Countries { get; set; } diff --git a/BlueWest.Data/Currency/CurrencyCreate.cs b/BlueWest.Data/Currency/CurrencyCreate.cs index 4474d9d..388f999 100644 --- a/BlueWest.Data/Currency/CurrencyCreate.cs +++ b/BlueWest.Data/Currency/CurrencyCreate.cs @@ -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 CountriesToCreate { get; set; } + public CurrencyCreate() { } + + public Currency ToCurrency() + { + List countries = new List(); + + foreach (var countryCreate in CountriesToCreate) + { + var newCountry = new Country(countryCreate, null); + countries.Add(newCountry); + } + + return new Currency(this, countries); + + } } } diff --git a/BlueWest.Data/Currency/CurrencyUpdate.cs b/BlueWest.Data/Currency/CurrencyUpdate.cs index d9f9828..c662f9c 100644 --- a/BlueWest.Data/Currency/CurrencyUpdate.cs +++ b/BlueWest.Data/Currency/CurrencyUpdate.cs @@ -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() + { + + } } } diff --git a/BlueWest.Data/Currency/TestData.cs b/BlueWest.Data/Currency/TestData.cs deleted file mode 100644 index 532f345..0000000 --- a/BlueWest.Data/Currency/TestData.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace BlueWest.Data; - -public class TestData -{ - public int Id { get; set; } - public string Name { get; set; } -} \ No newline at end of file diff --git a/BlueWest.Data/Transaction/FinanceTransaction.cs b/BlueWest.Data/Transaction/FinanceOp.cs similarity index 53% rename from BlueWest.Data/Transaction/FinanceTransaction.cs rename to BlueWest.Data/Transaction/FinanceOp.cs index 3b65a87..c0c7a50 100644 --- a/BlueWest.Data/Transaction/FinanceTransaction.cs +++ b/BlueWest.Data/Transaction/FinanceOp.cs @@ -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; } } } \ No newline at end of file diff --git a/BlueWest.Data/Transaction/FinanceTransactionInsertDto.cs b/BlueWest.Data/Transaction/FinanceOpCreate.cs similarity index 52% rename from BlueWest.Data/Transaction/FinanceTransactionInsertDto.cs rename to BlueWest.Data/Transaction/FinanceOpCreate.cs index 8675f4b..cb2c6a7 100644 --- a/BlueWest.Data/Transaction/FinanceTransactionInsertDto.cs +++ b/BlueWest.Data/Transaction/FinanceOpCreate.cs @@ -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; } } } diff --git a/BlueWest.Data/Transaction/FinanceTransactionReadDto.cs b/BlueWest.Data/Transaction/FinanceOpRead.cs similarity index 75% rename from BlueWest.Data/Transaction/FinanceTransactionReadDto.cs rename to BlueWest.Data/Transaction/FinanceOpRead.cs index 7420120..a96af67 100644 --- a/BlueWest.Data/Transaction/FinanceTransactionReadDto.cs +++ b/BlueWest.Data/Transaction/FinanceOpRead.cs @@ -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; } } diff --git a/BlueWest.Data/Transaction/FinanceTransactionType.cs b/BlueWest.Data/Transaction/FinanceOpType.cs similarity index 82% rename from BlueWest.Data/Transaction/FinanceTransactionType.cs rename to BlueWest.Data/Transaction/FinanceOpType.cs index f99f001..78dc6bf 100644 --- a/BlueWest.Data/Transaction/FinanceTransactionType.cs +++ b/BlueWest.Data/Transaction/FinanceOpType.cs @@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations; namespace BlueWest.Data; -public class FinanceTransactionType +public class FinanceOpType { [Key] public int Id { get; set; } diff --git a/BlueWest.Data/User/User.cs b/BlueWest.Data/User/User.cs index 23d5a04..6905dce 100644 --- a/BlueWest.Data/User/User.cs +++ b/BlueWest.Data/User/User.cs @@ -13,7 +13,7 @@ namespace BlueWest.Data public TimeSpan Id { get; } = TimeSpan.Zero; public string Name { get; set; } - public List FinanceTransactions { get; } + public List FinanceTransactions { get; } public User(TimeSpan id, string name)