From 53fb4acc677073c9e14592d9f915abf530cab85c Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Sat, 13 Aug 2022 04:35:36 +0100 Subject: [PATCH] Add Currency and Country tables --- BlueWest.Api/Controllers/CountryController.cs | 62 +++++++++++++++++ .../Controllers/CurrencyController.cs | 59 ++++++++++++++++ BlueWest.Api/Controllers/UserController.cs | 68 ++++++++++++------- BlueWest.Api/MySQL/CountriesDbContext.cs | 37 ++++++++++ .../{MysqlDbContext.cs => UserDbContext.cs} | 9 ++- BlueWest.Api/Startup.cs | 12 +++- BlueWest.Api/StartupExtensions.cs | 14 ++++ BlueWest.Data/Finance/Country.cs | 8 ++- BlueWest.Data/Finance/Currency.cs | 4 +- BlueWest.Data/Finance/FinanceTransaction.cs | 24 +++---- .../Finance/FinanceTransactionInsertDto.cs | 5 +- .../Finance/FinanceTransactionReadDto.cs | 3 +- .../Finance/FinanceTransactionType.cs | 12 ++++ 13 files changed, 266 insertions(+), 51 deletions(-) create mode 100644 BlueWest.Api/Controllers/CountryController.cs create mode 100644 BlueWest.Api/Controllers/CurrencyController.cs create mode 100644 BlueWest.Api/MySQL/CountriesDbContext.cs rename BlueWest.Api/MySQL/{MysqlDbContext.cs => UserDbContext.cs} (82%) create mode 100644 BlueWest.Api/StartupExtensions.cs create mode 100644 BlueWest.Data/Finance/FinanceTransactionType.cs diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs new file mode 100644 index 0000000..899506a --- /dev/null +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -0,0 +1,62 @@ +using System.Linq; +using BlueWest.Data; +using BlueWest.WebApi.MySQL; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace BlueWest.WebApi.Controllers; +[ApiController] +[Route("[controller]")] +public class CountryController : ControllerBase +{ + private readonly CountriesDbContext _dbContext; + + public CountryController(CountriesDbContext dbContext) + { + _dbContext = dbContext; + } + + + [ProducesResponseType(StatusCodes.Status201Created)] + [HttpPost] + public ActionResult AddCountry(Country country) + { + _dbContext.Countries.Add(country); + _dbContext.SaveChanges(); + return CreatedAtRoute(nameof(GetCountryById), new {countryId = country.Code}, country); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [HttpPut("countries/{country.Code}")] + public ActionResult UpdateCountry(Country country) + { + + var array = _dbContext.Countries.FirstOrDefault(x => x.Code == country.Code); + + if (array != null) + { + return Ok(array); + } + + return new NotFoundResult(); + } + + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("countries/{countryId}", Name = nameof(GetCountryById))] + public ActionResult GetCountryById(int countryId) + { + var array = _dbContext.Countries.FirstOrDefault(x => x.Code == countryId); + + if (array != null) + { + return Ok(array); + } + + return new NotFoundResult(); + } + + +} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs new file mode 100644 index 0000000..78501b7 --- /dev/null +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -0,0 +1,59 @@ +using System.Linq; +using BlueWest.Data; +using BlueWest.WebApi.MySQL; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace BlueWest.WebApi.Controllers; + +public class CurrencyController : ControllerBase +{ + private readonly CountriesDbContext _dbContext; + + public CurrencyController(CountriesDbContext dbContext) + { + _dbContext = dbContext; + } + + + [ProducesResponseType(StatusCodes.Status201Created)] + [HttpPost] + public ActionResult AddCurrency(Currency currency) + { + _dbContext.Currencies.Add(currency); + _dbContext.SaveChanges(); + return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = currency.Code}, currency); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [HttpPut("countries/{Currency.Code}")] + public ActionResult UpdateCurrency(Currency Currency) + { + + var array = _dbContext.Currencies.FirstOrDefault(x => x.Code == Currency.Code); + + if (array != null) + { + return Ok(array); + } + + return new NotFoundResult(); + } + + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("countries/{CurrencyId}", Name = nameof(GetCurrencyById))] + public ActionResult GetCurrencyById(int CurrencyId) + { + var array = _dbContext.Countries.FirstOrDefault(x => x.Code == CurrencyId); + + if (array != null) + { + return Ok(array); + } + + return new NotFoundResult(); + } +} \ No newline at end of file diff --git a/BlueWest.Api/Controllers/UserController.cs b/BlueWest.Api/Controllers/UserController.cs index 7ad3c8c..93f3eb7 100644 --- a/BlueWest.Api/Controllers/UserController.cs +++ b/BlueWest.Api/Controllers/UserController.cs @@ -15,13 +15,16 @@ namespace BlueWest.WebApi.Controllers public class UserController : ControllerBase { - private readonly MysqlDbContext _dbContext; + private readonly UserDbContext _dbContext; - public UserController(MysqlDbContext dbContext) + public UserController(UserDbContext dbContext) { _dbContext = dbContext; } + + #region Users + [ProducesResponseType(StatusCodes.Status200OK)] [HttpGet] public ActionResult Get() @@ -31,7 +34,12 @@ namespace BlueWest.WebApi.Controllers return Ok(users); } - + + /// + /// Get User by Id + /// + /// + /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -48,25 +56,7 @@ namespace BlueWest.WebApi.Controllers return new NotFoundResult(); } - - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [HttpGet("{userId}/transactions")] - public ActionResult GetTransactions(int userId) - { - return Ok(); - } - - - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status400BadRequest)] - [HttpGet("{userId}/transactions/{transactionId}")] - public ActionResult GetTransactionsById(int userId, TimeSpan transactionId) - { - return new NotFoundResult(); - } - - + [ProducesResponseType(StatusCodes.Status201Created)] [HttpPost] public ActionResult AddUser(UserUpdateDto userUpdateDto) @@ -100,7 +90,19 @@ 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")] @@ -108,7 +110,25 @@ namespace BlueWest.WebApi.Controllers { 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/CountriesDbContext.cs b/BlueWest.Api/MySQL/CountriesDbContext.cs new file mode 100644 index 0000000..256b531 --- /dev/null +++ b/BlueWest.Api/MySQL/CountriesDbContext.cs @@ -0,0 +1,37 @@ +using BlueWest.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +namespace BlueWest.WebApi.MySQL; + +public class CountriesDbContext : DbContext +{ + + public DbSet Countries { get; set; } + + public DbSet Currencies { get; set; } + + + public IConfiguration Configuration; + + + public CountriesDbContext(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) + { + + + } +} \ No newline at end of file diff --git a/BlueWest.Api/MySQL/MysqlDbContext.cs b/BlueWest.Api/MySQL/UserDbContext.cs similarity index 82% rename from BlueWest.Api/MySQL/MysqlDbContext.cs rename to BlueWest.Api/MySQL/UserDbContext.cs index d6a9a34..937c531 100644 --- a/BlueWest.Api/MySQL/MysqlDbContext.cs +++ b/BlueWest.Api/MySQL/UserDbContext.cs @@ -5,14 +5,17 @@ using Microsoft.Extensions.Configuration; namespace BlueWest.WebApi.MySQL; -public sealed class MysqlDbContext : DbContext +public sealed class UserDbContext : DbContext { public DbSet Users { get; set; } public DbSet Transactions { get; set; } + public DbSet TransactionTypes { get; set; } + + public IConfiguration Configuration; - public MysqlDbContext(DbContextOptions options) : base(options) + public UserDbContext(DbContextOptions options) : base(options) { Database.EnsureCreated(); @@ -41,5 +44,7 @@ public sealed class MysqlDbContext : DbContext .HasForeignKey(x => x.UserId); }); + + } } \ No newline at end of file diff --git a/BlueWest.Api/Startup.cs b/BlueWest.Api/Startup.cs index cbcfbb5..e6e1efc 100644 --- a/BlueWest.Api/Startup.cs +++ b/BlueWest.Api/Startup.cs @@ -59,10 +59,18 @@ namespace BlueWest.WebApi Version = "v1" }); }); - services.AddDbContextPool(options => - options.UseMySql(Configuration.GetConnectionString("LocalMySQL"), new MySqlServerVersion(new Version(8, 0, 11)))); + services.AddDbContextPool(options => + options.GetSqlSettings(Configuration)); + + services.AddDbContextPool(options => + options.GetSqlSettings(Configuration)); + + + services.AddGrpc(); } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/BlueWest.Api/StartupExtensions.cs b/BlueWest.Api/StartupExtensions.cs new file mode 100644 index 0000000..eb75c8f --- /dev/null +++ b/BlueWest.Api/StartupExtensions.cs @@ -0,0 +1,14 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +namespace BlueWest.WebApi; + +public static class StartupExtensions +{ + public static void GetSqlSettings(this DbContextOptionsBuilder optionsBuilder, IConfiguration configuration) + { + optionsBuilder.UseMySql(configuration.GetConnectionString("LocalMySQL"), + new MySqlServerVersion(new Version(8, 0, 11))); + } +} \ No newline at end of file diff --git a/BlueWest.Data/Finance/Country.cs b/BlueWest.Data/Finance/Country.cs index 0f4f037..c27b8bb 100644 --- a/BlueWest.Data/Finance/Country.cs +++ b/BlueWest.Data/Finance/Country.cs @@ -1,8 +1,10 @@ +using System.ComponentModel.DataAnnotations; + namespace BlueWest.Data; public class Country { - public string StateName; - public int Code; - public string TLD; + [Key] public int Code { get; } + public string StateName { get; } + public string TLD { get; } } \ No newline at end of file diff --git a/BlueWest.Data/Finance/Currency.cs b/BlueWest.Data/Finance/Currency.cs index e4fb9a4..ee40a28 100644 --- a/BlueWest.Data/Finance/Currency.cs +++ b/BlueWest.Data/Finance/Currency.cs @@ -2,5 +2,7 @@ namespace BlueWest.Data; public class Currency { - public Country Country { get; set; } + public string Code { get; } + public int Num { get; } + public Country Country { get; } } \ No newline at end of file diff --git a/BlueWest.Data/Finance/FinanceTransaction.cs b/BlueWest.Data/Finance/FinanceTransaction.cs index 40d86a0..3b65a87 100644 --- a/BlueWest.Data/Finance/FinanceTransaction.cs +++ b/BlueWest.Data/Finance/FinanceTransaction.cs @@ -4,28 +4,19 @@ using MapTo; namespace BlueWest.Data { - - public class FinanceTransactionType - { - [Key] public TimeSpan CreationDate { get; set; } - - public string Name; - - private string Description; - } - - [MapFrom(typeof(FinanceTransactionInsertDto))] public partial class FinanceTransaction { - [Key] public TimeSpan CreationDate { get; set; } public TimeSpan UserId { get; set; } - - public string Currency { get; } + + [Key] public TimeSpan CreationDate { get; set; } + + public Currency Currency { get; } + public FinanceTransactionType Type {get;} - private string FinanceTransactionDescription; + private string Description {get;} public FinanceTransaction() @@ -34,11 +25,12 @@ namespace BlueWest.Data } public FinanceTransaction(TimeSpan creationDate, TimeSpan userId, - string currency, FinanceTransactionType financeTransactionType) + Currency currency, FinanceTransactionType financeTransactionType) { CreationDate = creationDate; UserId = userId; Currency = currency; + Type = financeTransactionType; } } } \ No newline at end of file diff --git a/BlueWest.Data/Finance/FinanceTransactionInsertDto.cs b/BlueWest.Data/Finance/FinanceTransactionInsertDto.cs index 3a04bb5..df11584 100644 --- a/BlueWest.Data/Finance/FinanceTransactionInsertDto.cs +++ b/BlueWest.Data/Finance/FinanceTransactionInsertDto.cs @@ -11,16 +11,17 @@ namespace BlueWest.Data { public TimeSpan UserId { get; set; } - public string Currency { get; } + public Currency Currency { get; } public FinanceTransactionType FinanceTransactionType {get;} + public TimeSpan CreationDate { get; set; } public FinanceTransactionInsertDto( TimeSpan userId, - string currency , + Currency currency , FinanceTransactionType financeTransactionType ) { diff --git a/BlueWest.Data/Finance/FinanceTransactionReadDto.cs b/BlueWest.Data/Finance/FinanceTransactionReadDto.cs index 606a6dd..7420120 100644 --- a/BlueWest.Data/Finance/FinanceTransactionReadDto.cs +++ b/BlueWest.Data/Finance/FinanceTransactionReadDto.cs @@ -11,9 +11,10 @@ namespace BlueWest.Data { public TimeSpan UserId { get; set; } - public string Currency { get; } + public Currency Currency { get; } public FinanceTransactionType FinanceTransactionType {get;} + public TimeSpan CreationDate { get; set; } } } diff --git a/BlueWest.Data/Finance/FinanceTransactionType.cs b/BlueWest.Data/Finance/FinanceTransactionType.cs new file mode 100644 index 0000000..f99f001 --- /dev/null +++ b/BlueWest.Data/Finance/FinanceTransactionType.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlueWest.Data; + +public class FinanceTransactionType +{ + [Key] public int Id { get; set; } + + public string Name; + + private string Description; +} \ No newline at end of file