Add Currency and Country tables
This commit is contained in:
parent
6f60f74c90
commit
53fb4acc67
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get User by Id
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
[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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <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
|
||||
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -5,14 +5,17 @@ using Microsoft.Extensions.Configuration;
|
|||
|
||||
namespace BlueWest.WebApi.MySQL;
|
||||
|
||||
public sealed class MysqlDbContext : DbContext
|
||||
public sealed class UserDbContext : DbContext
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<FinanceTransaction> Transactions { get; set; }
|
||||
public DbSet<FinanceTransactionType> TransactionTypes { get; set; }
|
||||
|
||||
|
||||
public IConfiguration Configuration;
|
||||
|
||||
|
||||
public MysqlDbContext(DbContextOptions<MysqlDbContext> options) : base(options)
|
||||
public UserDbContext(DbContextOptions<UserDbContext> options) : base(options)
|
||||
{
|
||||
Database.EnsureCreated();
|
||||
|
||||
|
@ -41,5 +44,7 @@ public sealed class MysqlDbContext : DbContext
|
|||
.HasForeignKey(x => x.UserId);
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -59,10 +59,18 @@ namespace BlueWest.WebApi
|
|||
Version = "v1"
|
||||
});
|
||||
});
|
||||
services.AddDbContextPool<MysqlDbContext>(options =>
|
||||
options.UseMySql(Configuration.GetConnectionString("LocalMySQL"), new MySqlServerVersion(new Version(8, 0, 11))));
|
||||
services.AddDbContextPool<UserDbContext>(options =>
|
||||
options.GetSqlSettings(Configuration));
|
||||
|
||||
services.AddDbContextPool<CountriesDbContext>(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)
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
|
@ -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; }
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
)
|
||||
{
|
||||
|
|
|
@ -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; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue