Add consumption entity and use strongly typed ids
This commit is contained in:
52
src/WebApi/Consumptions/Consumption.cs
Normal file
52
src/WebApi/Consumptions/Consumption.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vegasco.WebApi.Cars;
|
||||
|
||||
namespace Vegasco.WebApi.Consumptions;
|
||||
|
||||
public class Consumption
|
||||
{
|
||||
public ConsumptionId Id { get; set; } = ConsumptionId.New();
|
||||
|
||||
public DateTimeOffset DateTime { get; set; }
|
||||
|
||||
public double Distance { get; set; }
|
||||
|
||||
public double Amount { get; set; }
|
||||
|
||||
public bool IgnoreInCalculation { get; set; }
|
||||
|
||||
public CarId CarId { get; set; }
|
||||
|
||||
public virtual Car Car { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class ConsumptionTableConfiguration : IEntityTypeConfiguration<Consumption>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Consumption> builder)
|
||||
{
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion<ConsumptionId.EfCoreValueConverter>();
|
||||
|
||||
builder.Property(x => x.DateTime)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Distance)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Amount)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.IgnoreInCalculation)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.CarId)
|
||||
.IsRequired()
|
||||
.HasConversion<CarId.EfCoreValueConverter>();
|
||||
|
||||
builder.HasOne(x => x.Car)
|
||||
.WithMany(x => x.Consumptions);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user