diff --git a/BlueWest.Api/Context/CompanyDbContext.cs b/BlueWest.Api/Context/CompanyDbContext.cs index 3fe7e3c..af19183 100644 --- a/BlueWest.Api/Context/CompanyDbContext.cs +++ b/BlueWest.Api/Context/CompanyDbContext.cs @@ -12,24 +12,24 @@ namespace BlueWest.WebApi.EF public sealed class CompanyDbContext : DbContext { /// - /// Db set of Companies + /// Companies. /// public DbSet Companies { get; set; } /// - /// Db set of Industries. + /// Industries. /// public DbSet Industries { get; set; } /// - /// Db set of Products. + /// Products. /// public DbSet Products { get; set; } /// - /// Options to be injected in the app + /// CompanyDbContext constructor. /// /// public CompanyDbContext(DbContextOptions options) : base(options) @@ -38,7 +38,7 @@ namespace BlueWest.WebApi.EF } /// - /// On database model creating + /// On model creating. /// /// Builder model of the database protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index e3960c5..923efe2 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -29,7 +29,7 @@ namespace BlueWest.WebApi.EF /// - /// Options to be injected in the app + /// CountryDbContext Constructor. /// /// public CountryDbContext(DbContextOptions options) : base(options) @@ -38,8 +38,7 @@ namespace BlueWest.WebApi.EF } /// - /// The country number is the primary key - /// The currency code is the primary key + /// On model creating. /// /// protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/BlueWest.Api/Context/ExchangeInterface.cs b/BlueWest.Api/Context/ExchangeInterface.cs index 45db8ee..79bbfbe 100644 --- a/BlueWest.Api/Context/ExchangeInterface.cs +++ b/BlueWest.Api/Context/ExchangeInterface.cs @@ -24,13 +24,12 @@ namespace BlueWest.WebApi.Interfaces #region Initialization /// - /// Exchange Interface Object + /// Database Ef context constructor /// /// Country context /// Finance context /// User context /// Event manager injection - public ExchangeInterface( CountryDbContext countryDbContext, @@ -46,7 +45,7 @@ namespace BlueWest.WebApi.Interfaces } /// - /// Empty constructor + /// Database Ef context constructor /// public ExchangeInterface() { } diff --git a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs index 2800453..4f388a2 100644 --- a/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs +++ b/BlueWest.Api/Context/Extensions/CountryDbExtensions.cs @@ -92,7 +92,13 @@ namespace BlueWest.WebApi.EF int countryId, CurrencyCreate currencyCreate, Expression>[] duplicationValidations) { - var country = dbContext.Countries.FirstOrDefault(d => d.Id == countryId); + + var country = dbContext + .Countries + .Where(data => data.Id == countryId) + .Include(o => o.Currencies) + .FirstOrDefault(); + // Check if currency exists if (country == null) return (false, $"{nameof(country)} Not found.", null); diff --git a/BlueWest.Api/Context/FinanceDbContext.cs b/BlueWest.Api/Context/FinanceDbContext.cs index 37f1a66..b6fbf8d 100644 --- a/BlueWest.Api/Context/FinanceDbContext.cs +++ b/BlueWest.Api/Context/FinanceDbContext.cs @@ -20,7 +20,7 @@ namespace BlueWest.WebApi.EF public DbSet TransactionTypes { get; set; } /// - /// Finance transactions context + /// CompanyDbContext constructor. /// /// public FinanceDbContext(DbContextOptions options) : base(options) @@ -29,7 +29,7 @@ namespace BlueWest.WebApi.EF } /// - /// On database model creating + /// On model creating. /// /// Builder model of the database protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/BlueWest.Api/Context/UserDbContext.cs b/BlueWest.Api/Context/UserDbContext.cs index 48cf906..80a0f6f 100644 --- a/BlueWest.Api/Context/UserDbContext.cs +++ b/BlueWest.Api/Context/UserDbContext.cs @@ -31,7 +31,7 @@ namespace BlueWest.WebApi.EF } /// - /// On database model creating + /// On model creating. /// /// Builder model of the database protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/BlueWest.Api/Controllers/CountryController.cs b/BlueWest.Api/Controllers/CountryController.cs index bdeec19..d125de5 100644 --- a/BlueWest.Api/Controllers/CountryController.cs +++ b/BlueWest.Api/Controllers/CountryController.cs @@ -27,7 +27,77 @@ namespace BlueWest.WebApi.Controllers _dbContext = dbContext; } + + /// + /// Get countries + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet] + public ActionResult GetCountries() + { + var array = _dbContext + .Countries + .Select(x => new CountryUnique(x)) + .ToArray(); + + return Ok(array); + + } + + + /// + /// Get Country by Id + /// + /// ISO 3166-1 countryId numeric code + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("{countryId}", Name = nameof(GetCountryById))] + public ActionResult GetCountryById(int countryId) + { + var country = _dbContext.Countries + .Where(x => x.Id == countryId) + .Select(x => new CountryUnique(x)) + .FirstOrDefault(); + + + if (country != null) + { + return Ok(country); + } + + return new NotFoundResult(); + } + + + /// + /// Get currencies of a countryId + /// + /// + /// + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [HttpGet("{countryId}/currencies")] + public ActionResult GetCountryCurrencies(int countryId) + { + var countryObj = _dbContext.Countries.FirstOrDefault(d => d.Id == countryId); + + if (countryObj == null) return new NotFoundResult(); + + var array = _dbContext + .Countries + .Where(data => data.Id == countryId) + .SelectMany(o => o.Currencies) + .Select(x => new CurrencyUnique(x)) + .ToArray(); + + return Ok(array); + } + + /// /// Add Country /// @@ -54,12 +124,11 @@ namespace BlueWest.WebApi.Controllers [HttpPost] public ActionResult AddCountry(CountryCreate countryToCreate) { - Country newCountry = new Country(countryToCreate); _dbContext.Countries.Add(newCountry); bool success = _dbContext.SaveChanges() >= 0; if (!success) return new BadRequestResult(); - return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Id}); + return CreatedAtRoute(nameof(GetCountryById), new {countryId = newCountry.Id}, new CountryUnique(newCountry)); } /// @@ -73,80 +142,18 @@ namespace BlueWest.WebApi.Controllers [HttpPut("{countryCode}")] public ActionResult UpdateCountry(int countryCode, CountryUpdate countryToUpdate) { - var (success, countryId) = _dbContext.UpdateCountry(countryToUpdate, countryCode); + var (success, country) = _dbContext.UpdateCountry(countryToUpdate, countryCode); if (success) { - return Ok(countryId); + var countryReply = new CountryUnique(country); + return Ok(countryReply); } return new NotFoundResult(); } - /// - /// Get countries - /// - /// - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - [HttpGet] - public ActionResult GetCountries() - { - var array = _dbContext.Countries; - - if (array != null) - { - return Ok(array.ToArray()); - } - - return new NotFoundResult(); - } - - /// - /// Get Country by Id - /// - /// ISO 3166-1 countryId numeric code - /// - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - [HttpGet("{countryId}", Name = nameof(GetCountryById))] - public ActionResult GetCountryById(int countryId) - { - var array = _dbContext.Countries.FirstOrDefault(x => x.Id == countryId); - - if (array != null) - { - return Ok(array); - } - - return new NotFoundResult(); - } - - /// - /// Get currencies of a countryId - /// - /// - /// - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - [HttpGet("{countryId}/currencies")] - public ActionResult GetCountryCurrencies(int countryId) - { - var countryObj = _dbContext.Countries.FirstOrDefault(d => d.Id == countryId); - - if (countryObj == null) return new NotFoundResult(); - - var array = _dbContext - .Countries - .Where(data => data.Id == countryId) - .SelectMany(o => o.Currencies) - .ToArray(); - - return Ok(array); - } - - /// /// Adds a currency to Country /// @@ -170,7 +177,7 @@ namespace BlueWest.WebApi.Controllers return new ConflictObjectResult(message); } - return Ok(country); + return Ok(new CountryUnique(country)); } diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index f6f6f04..4e2659a 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -35,7 +35,10 @@ namespace BlueWest.WebApi.Controllers [HttpGet] public ActionResult GetCurrencies() { - var currencies = _dbContext.Currencies.ToArray(); + var currencies = _dbContext.Currencies + .Select(currency => new CurrencyUnique(currency)) + .ToArray(); + return Ok(currencies); } @@ -56,7 +59,7 @@ namespace BlueWest.WebApi.Controllers return new NotFoundResult(); } - return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, newCurrency); + return CreatedAtRoute(nameof(GetCurrencyById), new {CurrencyId = newCurrency.Code}, new CurrencyUnique(newCurrency)); } /// @@ -74,7 +77,7 @@ namespace BlueWest.WebApi.Controllers if (success) { - return Ok(currency); + return Ok(new CurrencyUnique(currency)); } return new NotFoundResult(); @@ -95,7 +98,7 @@ namespace BlueWest.WebApi.Controllers if (currency != null) { - return Ok(currency); + return Ok(new CurrencyUnique(currency)); } return new NotFoundResult(); diff --git a/BlueWest.Api/Dockerfile b/BlueWest.Api/Dockerfile index faff182..f39653f 100644 --- a/BlueWest.Api/Dockerfile +++ b/BlueWest.Api/Dockerfile @@ -7,8 +7,29 @@ WORKDIR /app FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src +COPY ["BlueWest.Data.Capital/BlueWest.Data.Capital.csproj", "BlueWest.Data.Capital/"] +RUN dotnet restore "BlueWest.Data.Capital/BlueWest.Data.Capital.csproj" + + +COPY ["BlueWest/BlueWest.csproj", "BlueWest/"] +RUN dotnet restore "BlueWest/BlueWest.csproj" + + +COPY ["BlueWest.Data.Geography/BlueWest.Data.Geography.csproj", "BlueWest.Data.Geography/"] +RUN dotnet restore "BlueWest.Data.Geography/BlueWest.Data.Geography.csproj" + + COPY ["BlueWest.Api/BlueWest.Api.csproj", "BlueWest.Api/"] RUN dotnet restore "BlueWest.Api/BlueWest.Api.csproj" + + +COPY ["include/BlueWest.MapTo/src/BlueWest.MapTo/BlueWest.MapTo.csproj", "include/BlueWest.MapTo/src/BlueWest.MapTo/"] +RUN dotnet restore "include/BlueWest.MapTo/src/BlueWest.MapTo/BlueWest.MapTo.csproj" + +COPY ["include/Math-Expression-Evaluator/SimpleExpressionEvaluator/SimpleExpressionEvaluator.csproj", "include/Math-Expression-Evaluator/SimpleExpressionEvaluator/"] +RUN dotnet restore "include/Math-Expression-Evaluator/SimpleExpressionEvaluator/SimpleExpressionEvaluator.csproj" + + COPY [".", "."] RUN dotnet build "BlueWest.Api/BlueWest.Api.csproj" -c Release -o /app/build diff --git a/BlueWest.Api/Extensions/StartupExtensions.cs b/BlueWest.Api/Extensions/StartupExtensions.cs index 32821a7..35a00b0 100644 --- a/BlueWest.Api/Extensions/StartupExtensions.cs +++ b/BlueWest.Api/Extensions/StartupExtensions.cs @@ -25,20 +25,22 @@ namespace BlueWest.WebApi IConfiguration configuration, IWebHostEnvironment environment) { - var optionsBuilderRef = optionsBuilder.UseMySql(configuration.GetConnectionString("LocalMySQL"), - new MySqlServerVersion(new Version(8, 0, 11))); + optionsBuilder.UseMySql(configuration.GetConnectionString("LocalMySQL"), + new MySqlServerVersion(new Version(8, 0, 11))) + .UseMySql(new MySqlServerVersion(new Version(8, 0, 11)), + builder => { builder.EnableRetryOnFailure(6, TimeSpan.FromSeconds(3), null); }); // The following three options help with debugging, but should // be changed or removed for production. if (environment.IsDevelopment()) { - optionsBuilderRef + optionsBuilder .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging() .EnableDetailedErrors(); } - return optionsBuilderRef; + return optionsBuilder; } /// @@ -51,14 +53,11 @@ namespace BlueWest.WebApi public static IServiceCollection PrepareDatabasePool(this IServiceCollection serviceCollection, IConfiguration configuration, IWebHostEnvironment environment) { - return serviceCollection .AddDbContextPool(options => options.GetMySqlSettings(configuration, environment)) .AddDbContextPool(options => options.GetMySqlSettings(configuration, environment)) .AddDbContextPool(options => options.GetMySqlSettings(configuration, environment)) .AddDbContextPool(options => options.GetMySqlSettings(configuration, environment)); - - } } } \ No newline at end of file diff --git a/BlueWest.Data.Capital/Currency/CurrencyUnique.cs b/BlueWest.Data.Capital/Currency/CurrencyUnique.cs index 29225fd..a8a9bfc 100644 --- a/BlueWest.Data.Capital/Currency/CurrencyUnique.cs +++ b/BlueWest.Data.Capital/Currency/CurrencyUnique.cs @@ -7,6 +7,7 @@ namespace BlueWest.Data [MapFrom(typeof(Currency))] public partial class CurrencyUnique { + public int Id { get; set; } public int Num { get; set; } // Primary key [MaxLength(3)] public string Code { get; set; } diff --git a/docker-compose.yml b/docker-compose.yml index 2520efc..ebe09e8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,17 +1,5 @@ version: '3' services: - bapi120: - build: - context: ./ - dockerfile: ./BlueWest.Api/Dockerfile - ports: - - 8080:80 - environment: - VIRTUAL_HOST: localhost - restart: always - links: - - db:db - container_name: BW1_API db: container_name: BW1_DB_MYSQL image: mysql/mysql-server:8.0 @@ -34,7 +22,17 @@ services: MYSQL_ROOT_PASSWORD: 'dXjw127124dJ' # ports: # - "3308:3306" - - + bapi120: + build: + context: ./ + dockerfile: ./BlueWest.Api/Dockerfile + ports: + - 8080:80 + environment: + VIRTUAL_HOST: localhost + restart: always + links: + - db:db + container_name: BW1_API \ No newline at end of file