Add Currency and Country tables

This commit is contained in:
CodeLiturgy 2022-08-13 04:35:36 +01:00
parent 6f60f74c90
commit 53fb4acc67
13 changed files with 266 additions and 51 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -15,13 +15,16 @@ namespace BlueWest.WebApi.Controllers
public class UserController : ControllerBase public class UserController : ControllerBase
{ {
private readonly MysqlDbContext _dbContext; private readonly UserDbContext _dbContext;
public UserController(MysqlDbContext dbContext) public UserController(UserDbContext dbContext)
{ {
_dbContext = dbContext; _dbContext = dbContext;
} }
#region Users
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[HttpGet] [HttpGet]
public ActionResult Get() public ActionResult Get()
@ -31,7 +34,12 @@ namespace BlueWest.WebApi.Controllers
return Ok(users); return Ok(users);
} }
/// <summary>
/// Get User by Id
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
@ -48,25 +56,7 @@ namespace BlueWest.WebApi.Controllers
return new NotFoundResult(); 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)] [ProducesResponseType(StatusCodes.Status201Created)]
[HttpPost] [HttpPost]
public ActionResult AddUser(UserUpdateDto userUpdateDto) public ActionResult AddUser(UserUpdateDto userUpdateDto)
@ -100,7 +90,19 @@ namespace BlueWest.WebApi.Controllers
_dbContext.SaveChanges(); _dbContext.SaveChanges();
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.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpPost("{userId}/transactions")] [HttpPost("{userId}/transactions")]
@ -108,7 +110,25 @@ namespace BlueWest.WebApi.Controllers
{ {
return new BadRequestResult(); 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,37 @@
using BlueWest.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.MySQL;
public class CountriesDbContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<Currency> Currencies { get; set; }
public IConfiguration Configuration;
public CountriesDbContext(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)
{
}
}

View File

@ -5,14 +5,17 @@ using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.MySQL; namespace BlueWest.WebApi.MySQL;
public sealed class MysqlDbContext : 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<FinanceTransaction> Transactions { get; set; }
public DbSet<FinanceTransactionType> TransactionTypes { get; set; }
public IConfiguration Configuration; public IConfiguration Configuration;
public MysqlDbContext(DbContextOptions<MysqlDbContext> options) : base(options) public UserDbContext(DbContextOptions<UserDbContext> options) : base(options)
{ {
Database.EnsureCreated(); Database.EnsureCreated();
@ -41,5 +44,7 @@ public sealed class MysqlDbContext : DbContext
.HasForeignKey(x => x.UserId); .HasForeignKey(x => x.UserId);
}); });
} }
} }

View File

@ -59,10 +59,18 @@ namespace BlueWest.WebApi
Version = "v1" Version = "v1"
}); });
}); });
services.AddDbContextPool<MysqlDbContext>(options => services.AddDbContextPool<UserDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("LocalMySQL"), new MySqlServerVersion(new Version(8, 0, 11)))); options.GetSqlSettings(Configuration));
services.AddDbContextPool<CountriesDbContext>(options =>
options.GetSqlSettings(Configuration));
services.AddGrpc(); services.AddGrpc();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

View File

@ -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)));
}
}

View File

@ -1,8 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace BlueWest.Data; namespace BlueWest.Data;
public class Country public class Country
{ {
public string StateName; [Key] public int Code { get; }
public int Code; public string StateName { get; }
public string TLD; public string TLD { get; }
} }

View File

@ -2,5 +2,7 @@ namespace BlueWest.Data;
public class Currency public class Currency
{ {
public Country Country { get; set; } public string Code { get; }
public int Num { get; }
public Country Country { get; }
} }

View File

@ -4,28 +4,19 @@ using MapTo;
namespace BlueWest.Data namespace BlueWest.Data
{ {
public class FinanceTransactionType
{
[Key] public TimeSpan CreationDate { get; set; }
public string Name;
private string Description;
}
[MapFrom(typeof(FinanceTransactionInsertDto))] [MapFrom(typeof(FinanceTransactionInsertDto))]
public partial class FinanceTransaction public partial class FinanceTransaction
{ {
[Key] public TimeSpan CreationDate { get; set; }
public TimeSpan UserId { 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;} public FinanceTransactionType Type {get;}
private string FinanceTransactionDescription; private string Description {get;}
public FinanceTransaction() public FinanceTransaction()
@ -34,11 +25,12 @@ namespace BlueWest.Data
} }
public FinanceTransaction(TimeSpan creationDate, TimeSpan userId, public FinanceTransaction(TimeSpan creationDate, TimeSpan userId,
string currency, FinanceTransactionType financeTransactionType) Currency currency, FinanceTransactionType financeTransactionType)
{ {
CreationDate = creationDate; CreationDate = creationDate;
UserId = userId; UserId = userId;
Currency = currency; Currency = currency;
Type = financeTransactionType;
} }
} }
} }

View File

@ -11,16 +11,17 @@ namespace BlueWest.Data
{ {
public TimeSpan UserId { get; set; } public TimeSpan UserId { get; set; }
public string Currency { get; } public Currency Currency { get; }
public FinanceTransactionType FinanceTransactionType {get;} public FinanceTransactionType FinanceTransactionType {get;}
public TimeSpan CreationDate { get; set; }
public FinanceTransactionInsertDto( public FinanceTransactionInsertDto(
TimeSpan userId, TimeSpan userId,
string currency , Currency currency ,
FinanceTransactionType financeTransactionType FinanceTransactionType financeTransactionType
) )
{ {

View File

@ -11,9 +11,10 @@ namespace BlueWest.Data
{ {
public TimeSpan UserId { get; set; } public TimeSpan UserId { get; set; }
public string Currency { get; } public Currency Currency { get; }
public FinanceTransactionType FinanceTransactionType {get;} public FinanceTransactionType FinanceTransactionType {get;}
public TimeSpan CreationDate { get; set; }
} }
} }

View File

@ -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;
}