scaffold db, add enum

This commit is contained in:
DavidGudEnough 2025-01-08 17:16:27 -05:00
parent caf6f3275c
commit aa61d945ed
Signed by: david.nguyen
GPG Key ID: 0B95DC36355BEB37
14 changed files with 198 additions and 10 deletions

View File

@ -2,7 +2,7 @@
using Microsoft.Extensions.Configuration;
using System.Net.Http.Json;
using System.Text.Json;
using CH.Energy.Abstractions.Enum;
using CH.Enum;
namespace CH.CryptoStats.CoinGecko;

View File

@ -2,7 +2,7 @@
using Microsoft.Extensions.Configuration;
using System.Net.Http.Json;
using System.Text.Json;
using CH.Energy.Abstractions.Enum;
using CH.Enum;
namespace CH.CryptoStats.CoinMarketCap;

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CH.Energy.Abstractions.Enum;
using CH.Enum;
namespace CH.CryptoStats.Abstractions;

View File

@ -17,5 +17,8 @@
<PackageReference Include="PoweredSoft.Data.Core" Version="3.0.0" />
<PackageReference Include="PoweredSoft.Module.Abstractions" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CH.Enum\CH.Enum.csproj" />
</ItemGroup>
</Project>

View File

@ -1,18 +1,33 @@
using Microsoft.EntityFrameworkCore;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CH.Dal.DbEntity;
using CH.Enum;
namespace CH.Dal;
public class CHDbContext(DbContextOptions options) : CHDbScaffoldedContext(options)
{
static CHDbContext() => NpgsqlConnection.GlobalTypeMapper
.MapEnum<Currency>("currency")
.MapEnum<EnergyRateExceptionTresholdResetType>("energy_rate_exception_treshold_reset_type");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasPostgresEnum<Currency>("currency");
modelBuilder.HasPostgresEnum<EnergyRateExceptionTresholdResetType>("energy_rate_exception_treshold_reset_type");
modelBuilder.Entity<EnergyRate>(entity =>
{
entity.Property(e => e.Currency)
.HasColumnName("currency")
.HasColumnType("currency");
});
modelBuilder.Entity<EnergyRateException>(entity =>
{
entity.Property(e => e.ResetType)
.HasColumnName("reset_type")
.HasColumnType("energy_rate_exception_treshold_reset_type");
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

View File

@ -16,6 +16,12 @@ public partial class CHDbScaffoldedContext : DbContext
{
}
public virtual DbSet<EnergyProvider> EnergyProviders { get; set; }
public virtual DbSet<EnergyRate> EnergyRates { get; set; }
public virtual DbSet<EnergyRateException> EnergyRateExceptions { get; set; }
public virtual DbSet<Location> Locations { get; set; }
public virtual DbSet<Machine> Machines { get; set; }
@ -27,6 +33,72 @@ public partial class CHDbScaffoldedContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.HasPostgresEnum("currency", new[] { "USD", "CAD" })
.HasPostgresEnum("energy_rate_exception_treshold_reset_type", new[] { "daily" });
modelBuilder.Entity<EnergyProvider>(entity =>
{
entity.HasKey(e => e.Id).HasName("energy_provider_pkey");
entity.ToTable("energy_provider");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.CreatedAt)
.HasDefaultValueSql("(CURRENT_TIMESTAMP AT TIME ZONE 'UTC'::text)")
.HasColumnName("created_at");
entity.Property(e => e.Name)
.HasMaxLength(255)
.HasColumnName("name");
entity.Property(e => e.UpdatedAt).HasColumnName("updated_at");
});
modelBuilder.Entity<EnergyRate>(entity =>
{
entity.HasKey(e => e.Id).HasName("energy_rate_pkey");
entity.ToTable("energy_rate");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.CreatedAt)
.HasDefaultValueSql("(CURRENT_TIMESTAMP AT TIME ZONE 'UTC'::text)")
.HasColumnName("created_at");
entity.Property(e => e.Name)
.HasMaxLength(255)
.HasColumnName("name");
entity.Property(e => e.Price).HasColumnName("price");
entity.Property(e => e.ProviderId).HasColumnName("provider_id");
entity.Property(e => e.UpdatedAt).HasColumnName("updated_at");
entity.HasOne(d => d.Provider).WithMany(p => p.EnergyRates)
.HasForeignKey(d => d.ProviderId)
.HasConstraintName("energy_rate_provider_id_fkey");
});
modelBuilder.Entity<EnergyRateException>(entity =>
{
entity.HasKey(e => e.Id).HasName("energy_rate_exception_pkey");
entity.ToTable("energy_rate_exception");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.CreatedAt)
.HasDefaultValueSql("(CURRENT_TIMESTAMP AT TIME ZONE 'UTC'::text)")
.HasColumnName("created_at");
entity.Property(e => e.EndedAt).HasColumnName("ended_at");
entity.Property(e => e.EnergyTreshold).HasColumnName("energy_treshold");
entity.Property(e => e.Name)
.HasMaxLength(255)
.HasColumnName("name");
entity.Property(e => e.RateId).HasColumnName("rate_id");
entity.Property(e => e.StartedAt).HasColumnName("started_at");
entity.Property(e => e.UpdatedAt).HasColumnName("updated_at");
entity.HasOne(d => d.Rate).WithMany(p => p.EnergyRateExceptions)
.HasForeignKey(d => d.RateId)
.HasConstraintName("energy_rate_exception_rate_id_fkey");
});
modelBuilder.Entity<Location>(entity =>
{
entity.HasKey(e => e.Id).HasName("location_pkey");

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
namespace CH.Dal.DbEntity;
public partial class EnergyProvider
{
public long Id { get; set; }
public string Name { get; set; } = null!;
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public virtual ICollection<EnergyRate> EnergyRates { get; set; } = new List<EnergyRate>();
}

View File

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations.Schema;
using CH.Enum;
namespace CH.Dal.DbEntity;
public partial class EnergyRate
{
[Column(TypeName = "currency")]
public Currency Currency { get; set; }
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace CH.Dal.DbEntity;
public partial class EnergyRate
{
public long Id { get; set; }
public long? ProviderId { get; set; }
public string Name { get; set; } = null!;
public decimal? Price { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public virtual ICollection<EnergyRateException> EnergyRateExceptions { get; set; } = new List<EnergyRateException>();
public virtual EnergyProvider? Provider { get; set; }
}

View File

@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations.Schema;
using CH.Enum;
namespace CH.Dal.DbEntity;
public partial class EnergyRateException
{
[Column(TypeName = "energy_rate_exception_treshold_reset_type")]
public EnergyRateExceptionTresholdResetType ResetType { get; set; }
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
namespace CH.Dal.DbEntity;
public partial class EnergyRateException
{
public long Id { get; set; }
public long? RateId { get; set; }
public string? Name { get; set; }
public decimal? EnergyTreshold { get; set; }
public DateTime? StartedAt { get; set; }
public DateTime? EndedAt { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public virtual EnergyRate? Rate { get; set; }
}

View File

@ -5,5 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,10 @@
namespace CH.Energy.Abstractions.Enum;
using NpgsqlTypes;
namespace CH.Enum;
public enum Currency
{
[PgName("USD")]
USD,
[PgName("CAD")]
CAD,
}

View File

@ -0,0 +1,9 @@
using NpgsqlTypes;
namespace CH.Enum;
public enum EnergyRateExceptionTresholdResetType
{
[PgName("daily")]
Daily,
}