From 972838d39bbf0c06978215a834349225943007ca Mon Sep 17 00:00:00 2001 From: CodeLiturgy Date: Tue, 6 Sep 2022 05:54:48 +0100 Subject: [PATCH] Add SQLite DB option --- BlueWest.Api/BlueWest.Api.csproj | 11 ++++++ BlueWest.Api/Context/CountryDbContext.cs | 2 ++ .../Context/Templates/GetManyTemplate.csx | 8 +++-- .../Controllers/CurrencyController.cs | 4 +-- .../Extensions/ModelBuilderExtensions.cs | 5 +++ .../ModelBuilderMigrationExtensions.cs | 34 ++++++++++++++++++ BlueWest.Api/Extensions/StartupExtensions.cs | 35 ++++++++++++++++--- BlueWest.Api/Identity/AuthManager.cs | 9 ----- BlueWest.Api/Startup.cs | 26 ++++++++++++-- BlueWest.Api/appsettings.json | 2 +- BlueWest.Api/config.json | 4 +++ 11 files changed, 117 insertions(+), 23 deletions(-) create mode 100644 BlueWest.Api/Extensions/ModelBuilderMigrationExtensions.cs delete mode 100644 BlueWest.Api/Identity/AuthManager.cs create mode 100644 BlueWest.Api/config.json diff --git a/BlueWest.Api/BlueWest.Api.csproj b/BlueWest.Api/BlueWest.Api.csproj index c0a451b..ca67f72 100644 --- a/BlueWest.Api/BlueWest.Api.csproj +++ b/BlueWest.Api/BlueWest.Api.csproj @@ -19,6 +19,17 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/BlueWest.Api/Context/CountryDbContext.cs b/BlueWest.Api/Context/CountryDbContext.cs index 14cbd0c..9eb4a8b 100644 --- a/BlueWest.Api/Context/CountryDbContext.cs +++ b/BlueWest.Api/Context/CountryDbContext.cs @@ -1,6 +1,7 @@ using BlueWest.Data; using BlueWest.WebApi.EF.Model; using BlueWest.EfMethods; +using BlueWest.WebApi.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; @@ -91,6 +92,7 @@ namespace BlueWest.WebApi.EF { base.OnModelCreating(modelBuilder); modelBuilder.ConfigureCurrentDbModel(); + modelBuilder.AddCurrencyAndCountryData(); } #endregion diff --git a/BlueWest.Api/Context/Templates/GetManyTemplate.csx b/BlueWest.Api/Context/Templates/GetManyTemplate.csx index 9c28bc6..2832db8 100644 --- a/BlueWest.Api/Context/Templates/GetManyTemplate.csx +++ b/BlueWest.Api/Context/Templates/GetManyTemplate.csx @@ -21,8 +21,10 @@ public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFull query.Skip(skip).Take(take); if (where != null) query.Where(where); - if (orderDir == 1) query.OrderBy(orderBy); - else query.OrderByDescending(orderBy); - + if(orderBy != null) + { + if (orderDir == 1) query.OrderBy(orderBy); + else query.OrderByDescending(orderBy); + } return (query.Any(), query.ToArray()); } \ No newline at end of file diff --git a/BlueWest.Api/Controllers/CurrencyController.cs b/BlueWest.Api/Controllers/CurrencyController.cs index 5bcb7dd..bc70823 100644 --- a/BlueWest.Api/Controllers/CurrencyController.cs +++ b/BlueWest.Api/Controllers/CurrencyController.cs @@ -141,8 +141,8 @@ namespace BlueWest.WebApi.Controllers /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] - [HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCurrencyById))] - public ActionResult GetCountry(int currencyId, int countryId) + [HttpGet("{currencyId}/countries/{countryId}", Name = nameof(GetCountryFromCurrency))] + public ActionResult GetCountryFromCurrency(int currencyId, int countryId) { var countryQuery = from aCurrency in _dbContext.Currencies diff --git a/BlueWest.Api/Extensions/ModelBuilderExtensions.cs b/BlueWest.Api/Extensions/ModelBuilderExtensions.cs index 09aa7ac..a462855 100644 --- a/BlueWest.Api/Extensions/ModelBuilderExtensions.cs +++ b/BlueWest.Api/Extensions/ModelBuilderExtensions.cs @@ -121,6 +121,11 @@ namespace BlueWest.WebApi.EF.Model return modelBuilder; } + #endregion + + #region Migrations + + #endregion } } diff --git a/BlueWest.Api/Extensions/ModelBuilderMigrationExtensions.cs b/BlueWest.Api/Extensions/ModelBuilderMigrationExtensions.cs new file mode 100644 index 0000000..bb6a984 --- /dev/null +++ b/BlueWest.Api/Extensions/ModelBuilderMigrationExtensions.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using BlueWest.Data; +using BlueWest.WebApi.EF.Model; +using Microsoft.EntityFrameworkCore; + +namespace BlueWest.WebApi.Extensions +{ + public static class ModelBuilderMigrationExtensions + { + /// + /// Setup the database model + /// + /// + public static void AddCurrencyAndCountryData(this ModelBuilder modelBuilder) + { + + var countriesToAdd = new List(); + var country = new Country(); + + country.Id = 1; + country.Code = 1; + country.Name = "United States"; + country.Alpha2Code = "US"; + country.StateName = "United States of America"; + countriesToAdd.Add(country); + + modelBuilder + .Entity() + .HasData(countriesToAdd.ToArray()); + } + + } +} + diff --git a/BlueWest.Api/Extensions/StartupExtensions.cs b/BlueWest.Api/Extensions/StartupExtensions.cs index 35a00b0..3f71a82 100644 --- a/BlueWest.Api/Extensions/StartupExtensions.cs +++ b/BlueWest.Api/Extensions/StartupExtensions.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Configuration; namespace BlueWest.WebApi { @@ -14,6 +15,8 @@ namespace BlueWest.WebApi /// public static class StartupExtensions { + private static MySqlServerVersion GetMySqlServerVersion(int major, int minor, int build) => new (new Version(major, minor, build)); + /// /// Get MYSQL Connection String /// @@ -25,10 +28,14 @@ namespace BlueWest.WebApi IConfiguration configuration, IWebHostEnvironment environment) { - 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); }); + + optionsBuilder + .UseMySql(configuration.GetConnectionString("DockerMySQL"), GetMySqlServerVersion(8, 0, 11)) + .UseMySql(GetMySqlServerVersion(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. @@ -50,7 +57,7 @@ namespace BlueWest.WebApi /// /// /// - public static IServiceCollection PrepareDatabasePool(this IServiceCollection serviceCollection, + public static IServiceCollection PrepareMySQLDatabasePool(this IServiceCollection serviceCollection, IConfiguration configuration, IWebHostEnvironment environment) { return serviceCollection @@ -59,5 +66,23 @@ namespace BlueWest.WebApi .AddDbContextPool(options => options.GetMySqlSettings(configuration, environment)) .AddDbContextPool(options => options.GetMySqlSettings(configuration, environment)); } + + /// + /// Setup database Contexts + /// + /// + /// + /// + /// + public static IServiceCollection PrepareSQLLiteDatabasePool(this IServiceCollection serviceCollection, + IConfiguration configuration, IWebHostEnvironment environment) + { + return serviceCollection + .AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db")) + .AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db")) + .AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db")) + .AddDbContextPool(options => options.UseSqlite("Data Source=BlueWest.Api.db")); + } + } } \ No newline at end of file diff --git a/BlueWest.Api/Identity/AuthManager.cs b/BlueWest.Api/Identity/AuthManager.cs deleted file mode 100644 index 94a4f5b..0000000 --- a/BlueWest.Api/Identity/AuthManager.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace BlueWest.Data; - -/// -/// Auth Manager -/// -public class AuthManager -{ - -} \ No newline at end of file diff --git a/BlueWest.Api/Startup.cs b/BlueWest.Api/Startup.cs index bde2bf5..0567b55 100644 --- a/BlueWest.Api/Startup.cs +++ b/BlueWest.Api/Startup.cs @@ -81,10 +81,30 @@ namespace BlueWest.WebApi var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); options.IncludeXmlComments(xmlPath); }); + + + IConfigurationRoot configuration = new ConfigurationBuilder() + .AddJsonFile("config.json") + .Build(); + + var allowedDatabase = configuration["database"]; + services - .AddSingleton() - .PrepareDatabasePool(_configuration, _environment) - .AddScoped(); + .AddSingleton(); + + if (allowedDatabase == "mysql") + { + services + .PrepareMySQLDatabasePool(_configuration, _environment); + } + + if (allowedDatabase == "sqlite") + { + services.PrepareSQLLiteDatabasePool(_configuration, _environment); + } + + services.AddScoped(); + // services.AddGrpc(); } diff --git a/BlueWest.Api/appsettings.json b/BlueWest.Api/appsettings.json index 053cd92..2661716 100644 --- a/BlueWest.Api/appsettings.json +++ b/BlueWest.Api/appsettings.json @@ -8,6 +8,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "LocalMySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;" + "DockerMySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;" } } diff --git a/BlueWest.Api/config.json b/BlueWest.Api/config.json new file mode 100644 index 0000000..e55950c --- /dev/null +++ b/BlueWest.Api/config.json @@ -0,0 +1,4 @@ +{ + "database": "sqlite", // use sqlite or mysql + "environment": "dev" +} \ No newline at end of file