Add SQLite DB option

This commit is contained in:
CodeLiturgy 2022-09-06 05:54:48 +01:00
parent 6f5a92ec74
commit 972838d39b
11 changed files with 117 additions and 23 deletions

View File

@ -19,6 +19,17 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.2-mauipre.1.22102.15" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="6.0.2-mauipre.1.22054.8" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

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

View File

@ -21,8 +21,10 @@ public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFull
query.Skip(skip).Take(take);
if (where != null) query.Where(where);
if(orderBy != null)
{
if (orderDir == 1) query.OrderBy(orderBy);
else query.OrderByDescending(orderBy);
}
return (query.Any(), query.ToArray());
}

View File

@ -141,8 +141,8 @@ namespace BlueWest.WebApi.Controllers
/// <returns></returns>
[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

View File

@ -121,6 +121,11 @@ namespace BlueWest.WebApi.EF.Model
return modelBuilder;
}
#endregion
#region Migrations
#endregion
}
}

View File

@ -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
{
/// <summary>
/// Setup the database model
/// </summary>
/// <param name="modelBuilder"></param>
public static void AddCurrencyAndCountryData(this ModelBuilder modelBuilder)
{
var countriesToAdd = new List<Country>();
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<Country>()
.HasData(countriesToAdd.ToArray());
}
}
}

View File

@ -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
/// </summary>
public static class StartupExtensions
{
private static MySqlServerVersion GetMySqlServerVersion(int major, int minor, int build) => new (new Version(major, minor, build));
/// <summary>
/// Get MYSQL Connection String
/// </summary>
@ -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
/// <param name="configuration"></param>
/// <param name="environment"></param>
/// <returns></returns>
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<FinanceDbContext>(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool<CompanyDbContext>(options => options.GetMySqlSettings(configuration, environment));
}
/// <summary>
/// Setup database Contexts
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration"></param>
/// <param name="environment"></param>
/// <returns></returns>
public static IServiceCollection PrepareSQLLiteDatabasePool(this IServiceCollection serviceCollection,
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool<UserDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool<CountryDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool<FinanceDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"))
.AddDbContextPool<CompanyDbContext>(options => options.UseSqlite("Data Source=BlueWest.Api.db"));
}
}
}

View File

@ -1,9 +0,0 @@
namespace BlueWest.Data;
/// <summary>
/// Auth Manager
/// </summary>
public class AuthManager
{
}

View File

@ -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<EventManager>()
.PrepareDatabasePool(_configuration, _environment)
.AddScoped<ExchangeInterface>();
.AddSingleton<EventManager>();
if (allowedDatabase == "mysql")
{
services
.PrepareMySQLDatabasePool(_configuration, _environment);
}
if (allowedDatabase == "sqlite")
{
services.PrepareSQLLiteDatabasePool(_configuration, _environment);
}
services.AddScoped<ExchangeInterface>();
// services.AddGrpc();
}

View File

@ -8,6 +8,6 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"LocalMySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;"
"DockerMySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;"
}
}

4
BlueWest.Api/config.json Normal file
View File

@ -0,0 +1,4 @@
{
"database": "sqlite", // use sqlite or mysql
"environment": "dev"
}