CodeLiturgy.Dashboard/BlueWest.Api/Context/CountryDbContext.cs

90 lines
2.8 KiB
C#

using BlueWest.Data;
using BlueWest.WebApi.EF.Model;
using BlueWest.EfMethods;
using BlueWest.WebApi.Context.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BlueWest.WebApi.EF
{
/// <summary>
/// Countries and Currencies
/// </summary>
[EfGenerator]
public sealed class CountryDbContext : DbContext
{
#region Countries
/// <summary>
/// Countries
/// </summary>
[EfGetOneBy(nameof(Country.Id), typeof(CountryUnique))]
[EfGetOne(typeof(CountryUnique))]
[EfAddMethods(createType: typeof(CountryCreate), returnType: typeof(CountryUnique))]
[EfUpdateMethods( updateType: typeof(CountryUpdate), returnType: typeof(CountryUnique), keyPropertyMemberName: nameof(Country.Id))]
[EfGetMany(typeof(CountryUnique))]
[EfGetList(nameof(Country.Currencies), typeof(CurrencyUnique), nameof(Country.Id))]
[EfAddToList(nameof(Country.Currencies), typeof(CurrencyCreate), typeof(CurrencyUnique), nameof(Country.Id))]
[EfGetOneFromList(nameof(Country.Id), nameof(Country.Currencies), nameof(Currency.Id), typeof(CurrencyUnique))]
public DbSet<Country> Countries { get; set; }
#endregion
#region Currencies
/// <summary>
/// Currencies Database Table
/// </summary>
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
[EfUpdateMethods(typeof(CurrencyUpdate), typeof(CurrencyUnique), nameof(Currency.Id))]
[EfGetOneBy(nameof(Currency.Id), typeof(CurrencyUnique))]
[EfGetOne(typeof(CurrencyUnique))]
[EfGetMany(typeof(CurrencyUnique))]
[EfGetList(nameof(Currency.Countries), typeof(CountryUnique), nameof(Currency.Id))]
[EfGetOneFromList(nameof(Currency.Id), nameof(Currency.Countries), nameof(Country.Id), typeof(CountryUnique))]
public DbSet<Currency> Currencies { get; set; }
#endregion
#region Initialization
/// <summary>
/// App Configuration
/// </summary>
public IConfiguration Configuration;
/// <summary>
/// CountryDbContext Constructor.
/// </summary>
/// <param name="options"></param>
public CountryDbContext(DbContextOptions<CountryDbContext> options) : base(options)
{
Database.EnsureCreated();
}
/// <summary>
/// On model creating.
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ConfigureCurrentDbModel();
modelBuilder.AddCurrencyAndCountryData();
}
#endregion
}
}