add database model fluent configuration

This commit is contained in:
cuqmbr 2023-11-02 07:57:39 +02:00
parent 58140b6f70
commit 8e264c4f11
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
33 changed files with 4399 additions and 7 deletions

View File

@ -9,6 +9,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="HotChocolate.AspNetCore" Version="13.5.1" /> <PackageReference Include="HotChocolate.AspNetCore" Version="13.5.1" />
<PackageReference Include="HotChocolate.Types" Version="13.5.1" /> <PackageReference Include="HotChocolate.Types" Version="13.5.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,6 +1,16 @@
using AutoubsApi.Persistence.Contexts;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<PostgresContext>(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("DefaultConnection"),
npgsqOptions => npgsqOptions.UseNetTopologySuite()
));
var app = builder.Build(); var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run(); app.Run();

View File

@ -1,8 +1,5 @@
{ {
"Logging": { "ConnectionStrings": {
"LogLevel": { "DefaultConnection": "Host=10.0.0.20:5432;Database=autobus;Username=postgres;Password=12345678"
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
} }
} }

View File

@ -7,7 +7,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.13">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="7.0.11" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="7.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="7.0.11" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="7.0.11" />

View File

@ -0,0 +1,63 @@
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NetTopologySuite.Geometries;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class AddressConfiguration : EntityBaseConfiguration<Address>
{
public override void Configure(EntityTypeBuilder<Address> builder)
{
base.Configure(builder);
builder
.ToTable("addresses")
.HasKey(e => e.Id);
builder
.Property(a => a.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(a => a.VehicleType)
.HasColumnName("vehicle_type")
.HasColumnType("varchar(16)")
.HasConversion(
t => t.ToString(),
s => (VehicleType) Enum.Parse(typeof(VehicleType), s)
)
.IsRequired();
builder
.Property(a => a.Location)
.HasColumnName("location")
.HasColumnType("geography(point)")
.HasConversion(
l => new Point(l.Latitude, l.Longitude),
p => new Entities.Coordinates(p.X, p.Y)
)
.IsRequired();
builder
.Property(a => a.CityId)
.HasColumnName("city_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(a => a.City)
.WithMany(c => c.Addresses)
.HasForeignKey(a => a.CityId)
.HasConstraintName("fk_addresses_city_id")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(a => a.RouteAddresses)
.WithOne(ra => ra.Address)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,57 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class AircraftConfiguration : EntityBaseConfiguration<Aircraft>
{
public override void Configure(EntityTypeBuilder<Aircraft> builder)
{
builder
.ToTable("aircrafts");
builder
.Property(b => b.Number)
.HasColumnName("number")
.HasColumnType("varchar(8)")
.IsRequired();
builder
.Property(b => b.Model)
.HasColumnName("model")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(b => b.Capacity)
.HasColumnName("capacity")
.HasColumnType("int")
.IsRequired();
builder
.Property(b => b.HasWiFi)
.HasColumnName("has_wifi")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.HasMultimedia)
.HasColumnName("has_multimedia")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.Id)
.HasColumnName("id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(b => b.Vehicle)
.WithOne()
.HasForeignKey<Aircraft>(b => b.Id)
.HasConstraintName("fk_aircrafts_vehicles_id")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,75 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class BusConfiguration : EntityBaseConfiguration<Bus>
{
public override void Configure(EntityTypeBuilder<Bus> builder)
{
builder
.ToTable("buses");
builder
.Property(b => b.Number)
.HasColumnName("number")
.HasColumnType("varchar(8)")
.IsRequired();
builder
.Property(b => b.Model)
.HasColumnName("model")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(b => b.Capacity)
.HasColumnName("capacity")
.HasColumnType("int")
.IsRequired();
builder
.Property(b => b.HasClimateControl)
.HasColumnName("has_climate_control")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.HasWC)
.HasColumnName("has_wc")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.HasWiFi)
.HasColumnName("has_wifi")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.HasMultimedia)
.HasColumnName("has_multimedia")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.HasOutlets)
.HasColumnName("has_outlets")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.Id)
.HasColumnName("id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(b => b.Vehicle)
.WithOne()
.HasForeignKey<Bus>(b => b.Id)
.HasConstraintName("fk_buses_vehicles_id")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,57 @@
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class CarriageConfiguration : EntityBaseConfiguration<Carriage>
{
public override void Configure(EntityTypeBuilder<Carriage> builder)
{
base.Configure(builder);
builder
.ToTable("carriages")
.HasKey(e => e.Id);
builder
.Property(b => b.Type)
.HasColumnName("type")
.HasColumnType("varchar(16)")
.HasConversion(
e => e.ToString(),
s => (CarriageType)Enum.Parse(typeof(CarriageType), s)
)
.IsRequired();
builder
.Property(b => b.Capacity)
.HasColumnName("capacity")
.HasColumnType("int")
.IsRequired();
builder
.Property(b => b.Number)
.HasColumnName("number")
.HasColumnType("int")
.IsRequired();
builder
.Property(b => b.HasWiFi)
.HasColumnName("has_wifi")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(b => b.HasOutlets)
.HasColumnName("has_outlets")
.HasColumnType("boolean")
.IsRequired();
builder
.HasMany(c => c.TrainCarriage)
.WithOne(tc => tc.Carriage)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,41 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class CityConfiguration : EntityBaseConfiguration<City>
{
public override void Configure(EntityTypeBuilder<City> builder)
{
base.Configure(builder);
builder
.ToTable("cities")
.HasKey(e => e.Id);
builder
.Property(c => c.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(r => r.RegionId)
.HasColumnName("region_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(c => c.Region)
.WithMany(r => r.Cities)
.HasForeignKey(c => c.RegionId)
.HasConstraintName("fk_cities_regions_regionId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(c => c.Addresses)
.WithOne(a => a.City)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,51 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class CompanyConfiguration : EntityBaseConfiguration<Company>
{
public override void Configure(EntityTypeBuilder<Company> builder)
{
base.Configure(builder);
builder
.ToTable("companies")
.HasKey(e => e.Id);
builder
.Property(c => c.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(c => c.LegalAddress)
.HasColumnName("legal_address")
.HasColumnType("varchar(256)")
.IsRequired();
builder
.Property(c => c.ContactEmail)
.HasColumnName("contact_email")
.HasColumnType("varchar(256)")
.IsRequired();
builder
.Property(c => c.ContactPhoneNumber)
.HasColumnName("contact_phone_number")
.HasColumnType("varchar(16)")
.IsRequired();
builder
.HasMany(c => c.Employees)
.WithOne(e => e.EmployerCompany)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(c => c.Vehicles)
.WithOne(v => v.Company)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,28 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class CountryConfiguration : EntityBaseConfiguration<Country>
{
public override void Configure(EntityTypeBuilder<Country> builder)
{
base.Configure(builder);
builder
.ToTable("countries")
.HasKey(e => e.Id);
builder
.Property(c => c.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.HasMany(c => c.Regions)
.WithOne(r => r.Country)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,75 @@
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class EmployeeConfiguration : EntityBaseConfiguration<Employee>
{
public override void Configure(EntityTypeBuilder<Employee> builder)
{
base.Configure(builder);
builder
.ToTable("employees")
.HasKey(e => e.Id);
builder
.Property(e => e.FisrtName)
.HasColumnName("first_name")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(e => e.LastName)
.HasColumnName("last_name")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(e => e.Patronymic)
.HasColumnName("patronymic")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(e => e.Sex)
.HasColumnName("sex")
.HasColumnType("varchar(16)")
.HasConversion(
e => e.ToString(),
s => (Sex)Enum.Parse(typeof(Sex), s)
)
.IsRequired();
builder
.Property(e => e.BirthDate)
.HasColumnName("birth_date")
.HasColumnType("date")
.IsRequired();
builder
.Property(e => e.EmployerCompanyId)
.HasColumnName("employer_company_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(e => e.EmployerCompany)
.WithMany(c => c.Employees)
.HasForeignKey(e => e.EmployerCompanyId)
.HasConstraintName("fk_employees_companies_employerCompanyId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(e => e.Documents)
.WithOne(d => d.Employee)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(e => e.Shifts)
.WithOne(s => s.Employee)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,47 @@
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class EmployeeDocumentConfiguration : EntityBaseConfiguration<EmployeeDocument>
{
public override void Configure(EntityTypeBuilder<EmployeeDocument> builder)
{
base.Configure(builder);
builder
.ToTable("employee_documents")
.HasKey(e => e.Id);
builder
.Property(ed => ed.Type)
.HasColumnName("type")
.HasColumnType("varchar(32)")
.HasConversion(
e => e.ToString(),
s => (EmployeeDocumentType)Enum.Parse(typeof(EmployeeDocumentType), s)
)
.IsRequired();
builder
.Property(ed => ed.Information)
.HasColumnName("information")
.HasColumnType("varchar(256)")
.IsRequired();
builder
.Property(ed => ed.EmployeeId)
.HasColumnName("employee_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(ed => ed.Employee)
.WithMany(e => e.Documents)
.HasForeignKey(ed => ed.EmployeeId)
.HasConstraintName("fk_employeeDocuments_employees_employeeId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,27 @@
using AutobusApi.Domain.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class EntityBaseConfiguration<TEntity> : IEntityTypeConfiguration<TEntity>
where TEntity : EntityBase
{
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
builder
.HasKey(e => e.Id);
builder
.Property(e => e.Id)
.HasColumnName("id")
.HasColumnType("int")
.IsRequired();
builder
.Property(e => e.IsDeleted)
.HasColumnName("is_deleted")
.HasColumnType("boolean")
.IsRequired();
}
}

View File

@ -0,0 +1,41 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class RegionConfiguration : EntityBaseConfiguration<Region>
{
public override void Configure(EntityTypeBuilder<Region> builder)
{
base.Configure(builder);
builder
.ToTable("regions")
.HasKey(e => e.Id);
builder
.Property(r => r.Name)
.HasColumnName("name")
.HasColumnType("varchar(64)")
.IsRequired();
builder
.Property(r => r.CountryId)
.HasColumnName("country_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(r => r.Country)
.WithMany(c => c.Regions)
.HasForeignKey(r => r.CountryId)
.HasConstraintName("fk_regions_coutries_countryId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(r => r.Cities)
.WithOne(c => c.Region)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,61 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class ReviewConfiguration : EntityBaseConfiguration<Review>
{
public override void Configure(EntityTypeBuilder<Review> builder)
{
base.Configure(builder);
builder
.ToTable("reviews")
.HasKey(e => e.Id);
builder
.Property(r => r.Rating)
.HasColumnName("rating")
.HasColumnType("numeric(1,0)")
.IsRequired();
builder
.Property(r => r.Comment)
.HasColumnName("comment")
.HasColumnType("varchar(128)")
.IsRequired();
builder
.Property(r => r.PostDateTimeUtc)
.HasColumnName("post_timestamp_utc")
.HasColumnType("timestamp")
.IsRequired();
builder
.Property(r => r.UserId)
.HasColumnName("user_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(r => r.User)
.WithMany(u => u.Reviews)
.HasForeignKey(r => r.UserId)
.HasConstraintName("fk_reviews_users_userId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(r => r.VehicleEnrollmentId)
.HasColumnName("vehicle_enrollment_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(r => r.VehicleEnrollment)
.WithMany(ve => ve.Reviews)
.HasForeignKey(r => r.VehicleEnrollmentId)
.HasConstraintName("fk_reviews_vehicleEnrollments_vehicleEnrollmentId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,49 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class RouteAddressConfiguration : EntityBaseConfiguration<RouteAddress>
{
public override void Configure(EntityTypeBuilder<RouteAddress> builder)
{
base.Configure(builder);
builder
.ToTable("route_addresses")
.HasKey(e => e.Id);
builder
.Property(ra => ra.Order)
.HasColumnName("order")
.HasColumnType("int")
.IsRequired();
builder
.Property(ra => ra.AddressId)
.HasColumnName("address_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(ra => ra.Address)
.WithMany(a => a.RouteAddresses)
.HasForeignKey(ra => ra.AddressId)
.HasConstraintName("fk_routeAddresses_addresses_addressId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(ra => ra.RouteId)
.HasColumnName("route_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(ra => ra.Route)
.WithMany(r => r.RouteAddresses)
.HasForeignKey(ra => ra.RouteId)
.HasConstraintName("fk_routeAddresses_routes_routeId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,61 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class RouteAddressDeatilsConfiguration : EntityBaseConfiguration<RouteAddressDetails>
{
public override void Configure(EntityTypeBuilder<RouteAddressDetails> builder)
{
base.Configure(builder);
builder
.ToTable("route_address_details")
.HasKey(e => e.Id);
builder
.Property(rad => rad.TimeToNextAddress)
.HasColumnName("time_to_next_address")
.HasColumnType("interval")
.IsRequired();
builder
.Property(rad => rad.CurrentAddressStopTime)
.HasColumnName("current_address_stop_time")
.HasColumnType("interval")
.IsRequired();
builder
.Property(rad => rad.CostToNextAddress)
.HasColumnName("cost_to_next_address")
.HasColumnType("numeric(16,4)")
.IsRequired();
builder
.Property(rad => rad.RouteAddressId)
.HasColumnName("route_address_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(rad => rad.RouteAddress)
.WithMany(ra => ra.RouteAddressDetails)
.HasForeignKey(rad => rad.RouteAddressId)
.HasConstraintName("fk_routeAddressDetails_routeAddress_routeAddressId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(rad => rad.VehicleEnrollmentId)
.HasColumnName("vehicle_enrollment_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(rad => rad.VehicleEnrollment)
.WithMany(ve => ve.RouteAddressDetails)
.HasForeignKey(rad => rad.VehicleEnrollmentId)
.HasConstraintName("fk_routeAddressDetails_vehicleEnrollments_vehicleEnrollmentId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,27 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class RouteConfiguration : EntityBaseConfiguration<Route>
{
public override void Configure(EntityTypeBuilder<Route> builder)
{
base.Configure(builder);
builder
.ToTable("routes")
.HasKey(e => e.Id);
builder
.HasMany(r => r.VehicleEnrollments)
.WithOne(ve => ve.Route)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(r => r.RouteAddresses)
.WithOne(ra => ra.Route)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,43 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class TicketConfiguration : EntityBaseConfiguration<Ticket>
{
public override void Configure(EntityTypeBuilder<Ticket> builder)
{
base.Configure(builder);
builder
.ToTable("tickets")
.HasKey(e => e.Id);
builder
.Property(t => t.VehicleEnrollmentId)
.HasColumnName("vehicle_enrollment_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(t => t.VehicleEnrollment)
.WithMany(ve => ve.Tickets)
.HasForeignKey(t => t.VehicleEnrollmentId)
.HasConstraintName("fk_tickets_vehicleEnrollments_vehicleEnrollmentId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(t => t.TicketGroupId)
.HasColumnName("ticket_group_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(t => t.TicketGroup)
.WithMany(ve => ve.Tickets)
.HasForeignKey(t => t.TicketGroupId)
.HasConstraintName("fk_tickets_ticketGroups_ticketGroupId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,47 @@
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class TicketDocumentConfiguration : EntityBaseConfiguration<TicketDocument>
{
public override void Configure(EntityTypeBuilder<TicketDocument> builder)
{
base.Configure(builder);
builder
.ToTable("ticket_documents")
.HasKey(e => e.Id);
builder
.Property(td => td.Type)
.HasColumnName("type")
.HasColumnType("varchar(32)")
.HasConversion(
e => e.ToString(),
s => (TicketDocumentType)Enum.Parse(typeof(TicketDocumentType), s)
)
.IsRequired();
builder
.Property(td => td.Information)
.HasColumnName("information")
.HasColumnType("varchar(256)")
.IsRequired();
builder
.Property(td => td.TicketGroupId)
.HasColumnName("ticket_group_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(td => td.TicketGroup)
.WithOne(tg => tg.TicketDocument)
.HasForeignKey<TicketGroup>(td => td.TicketDocumentId)
.HasConstraintName("fk_ticketDocuments_ticketGroups_ticketDocumentId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,119 @@
using AutobusApi.Domain.Entities;
using AutobusApi.Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class TicketGroupConfiguration : EntityBaseConfiguration<TicketGroup>
{
public override void Configure(EntityTypeBuilder<TicketGroup> builder)
{
base.Configure(builder);
builder
.ToTable("ticket_groups")
.HasKey(e => e.Id);
builder
.Property(tg => tg.BuyerFirstName)
.HasColumnName("buyer_first_name")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(tg => tg.BuyerLastName)
.HasColumnName("buyer_last_name")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(tg => tg.BuyerPhoneNumber)
.HasColumnName("buyer_phone_number")
.HasColumnType("varchar(16)")
.IsRequired();
builder
.Property(tg => tg.BuyerEmailAddress)
.HasColumnName("buyer_email")
.HasColumnType("varchar(256)")
.IsRequired();
builder
.Property(tg => tg.PassengerFirstName)
.HasColumnName("passenger_first_name")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(tg => tg.PassengerLastName)
.HasColumnName("passenger_last_name")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(tg => tg.PassengerPatronymic)
.HasColumnName("passenger_patronymic")
.HasColumnType("varchar(32)")
.IsRequired();
builder
.Property(tg => tg.PassengerSex)
.HasColumnName("passenger_sex")
.HasColumnType("varchar(16)")
.HasConversion(
e => e.ToString(),
s => (Sex)Enum.Parse(typeof(Sex), s)
)
.IsRequired();
builder
.Property(tg => tg.PassengerBirthDate)
.HasColumnName("passenger_birth_date")
.HasColumnType("date")
.IsRequired();
builder
.Property(tg => tg.PurchaseDateTimeUtc)
.HasColumnName("purchase_timestamp_utc")
.HasColumnType("timestamp")
.IsRequired();
builder
.Property(tg => tg.IsReturned)
.HasColumnName("is_returned")
.HasColumnType("boolean")
.IsRequired();
builder
.Property(tg => tg.BuyerEmailAddress)
.HasColumnName("buyer_email")
.HasColumnType("varchar(256)")
.IsRequired();
builder
.Property(tg => tg.UserId)
.HasColumnName("user_id")
.HasColumnType("int")
.IsRequired(false);
builder
.HasOne(tg => tg.User)
.WithMany(u => u.TicketGroups)
.HasForeignKey(tg => tg.UserId)
.HasConstraintName("fk_ticketGroups_users_userId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(tg => tg.Tickets)
.WithOne(t => t.TicketGroup)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(tg => tg.TicketDocument)
.WithOne(td => td.TicketGroup)
.HasForeignKey<TicketGroup>(tg => tg.TicketDocumentId)
.HasConstraintName("fk_ticketGroups_ticketDocuments_ticketDocumentId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,43 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class TrainCarriageConfiguration : EntityBaseConfiguration<TrainCarriage>
{
public override void Configure(EntityTypeBuilder<TrainCarriage> builder)
{
base.Configure(builder);
builder
.ToTable("train_carriages")
.HasKey(e => e.Id);
builder
.Property(b => b.TrainId)
.HasColumnName("train_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(tc => tc.Train)
.WithMany(t => t.TrainCarriage)
.HasForeignKey(tc => tc.TrainId)
.HasConstraintName("fk_trainCarriages_trains_trainId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(b => b.CarriageId)
.HasColumnName("carriage_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(tc => tc.Carriage)
.WithMany(c => c.TrainCarriage)
.HasForeignKey(tc => tc.TrainId)
.HasConstraintName("fk_trainCarriages_trains_carriageId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,38 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class TrainConfiguration : EntityBaseConfiguration<Train>
{
public override void Configure(EntityTypeBuilder<Train> builder)
{
builder
.ToTable("trains");
builder
.Property(b => b.Number)
.HasColumnName("number")
.HasColumnType("varchar(8)")
.IsRequired();
builder
.HasMany(t => t.TrainCarriage)
.WithOne(tc => tc.Train)
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(b => b.Id)
.HasColumnName("id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(b => b.Vehicle)
.WithOne()
.HasForeignKey<Train>(b => b.Id)
.HasConstraintName("fk_trains_vehicles_id")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,27 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class UserConfiguration : EntityBaseConfiguration<User>
{
public override void Configure(EntityTypeBuilder<User> builder)
{
base.Configure(builder);
builder
.ToTable("users")
.HasKey(e => e.Id);
builder
.HasMany(u => u.Reviews)
.WithOne(r => r.User)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(u => u.TicketGroups)
.WithOne(tg => tg.User)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,38 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class VehicleConfiguration : EntityBaseConfiguration<Vehicle>
{
public override void Configure(EntityTypeBuilder<Vehicle> builder)
{
base.Configure(builder);
builder
.UseTptMappingStrategy()
.ToTable("vehicles")
.HasKey(e => e.Id);
builder
.Property(v => v.CompanyId)
.HasColumnName("company_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(v => v.Company)
.WithMany(c => c.Vehicles)
.HasForeignKey(v => v.CompanyId)
.HasConstraintName("fk_vehicles_companies_companyId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(v => v.Enrollments)
.WithOne(ve => ve.Vehicle)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,69 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class VehicleEnrollmentConfiguration : EntityBaseConfiguration<VehicleEnrollment>
{
public override void Configure(EntityTypeBuilder<VehicleEnrollment> builder)
{
base.Configure(builder);
builder
.ToTable("vehicle_enrollments")
.HasKey(e => e.Id);
builder
.Property(ve => ve.DepartureDateTimeUtc)
.HasColumnName("departure_timestamp_utc")
.HasColumnType("timestamp")
.IsRequired();
builder
.Property(ve => ve.RouteId)
.HasColumnName("route_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(ve => ve.Route)
.WithMany(r => r.VehicleEnrollments)
.HasForeignKey(ve => ve.RouteId)
.HasConstraintName("fk_vehicleEnrollments_routes_routeId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(ve => ve.VehicleId)
.HasColumnName("vehicle_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(ve => ve.Vehicle)
.WithMany(v => v.Enrollments)
.HasForeignKey(ve => ve.VehicleId)
.HasConstraintName("fk_vehicleEnrollments_vehicles_vehicleId")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(ve => ve.RouteAddressDetails)
.WithOne(rad => rad.VehicleEnrollment)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(ve => ve.Tickets)
.WithOne(t => t.VehicleEnrollment)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(ve => ve.Crew)
.WithOne(c => c.VehicleEnrollment)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(ve => ve.Reviews)
.WithOne(r => r.VehicleEnrollment)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,43 @@
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AutobusApi.Persistence.Contexts.Configurations;
public class VehicleEnrollmentEmployeeConfiguration : EntityBaseConfiguration<VehicleEnrollmentEmployee>
{
public override void Configure(EntityTypeBuilder<VehicleEnrollmentEmployee> builder)
{
base.Configure(builder);
builder
.ToTable("vehicle_enrollment_employees")
.HasKey(e => e.Id);
builder
.Property(vee => vee.EmployeeId)
.HasColumnName("employee_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(vee => vee.Employee)
.WithMany(e => e.Shifts)
.HasForeignKey(vee => vee.EmployeeId)
.HasConstraintName("fk_vehicleEnrollmentEmployees_employees_employeeId")
.OnDelete(DeleteBehavior.Cascade);
builder
.Property(vee => vee.VehicleEnrollmentId)
.HasColumnName("vehicle_enrollment_id")
.HasColumnType("int")
.IsRequired();
builder
.HasOne(vee => vee.VehicleEnrollment)
.WithMany(ve => ve.Crew)
.HasForeignKey(vee => vee.VehicleEnrollmentId)
.HasConstraintName("fk_vehicleEnrollmentEmployees_vehicleEnrollments_vehicleEnrollmentId")
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -0,0 +1,64 @@
using System.Reflection;
using AutobusApi.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace AutoubsApi.Persistence.Contexts;
public class PostgresContext : DbContext
{
public PostgresContext(DbContextOptions<PostgresContext> options)
: base(options) { }
public DbSet<Country> Countries { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<City> Cities { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<RouteAddress> RouteAddresses { get; set; }
public DbSet<Route> Routes { get; set; }
public DbSet<RouteAddressDetails> RouteAddressDetails { get; set; }
public DbSet<VehicleEnrollment> VehicleEnrollments { get; set; }
public DbSet<Vehicle> Vehicles { get; set; }
public DbSet<Bus> Buses { get; set; }
public DbSet<Aircraft> Aircraft { get; set; }
public DbSet<Train> Trains { get; set; }
public DbSet<TrainCarriage> TrainCarriages { get; set; }
public DbSet<Carriage> Carriages { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<EmployeeDocument> EmployeeDocuments { get; set; }
public DbSet<VehicleEnrollmentEmployee> vehicleEnrollmentEmployees { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<TicketGroup> TicketGroups { get; set; }
public DbSet<Ticket> Tickets { get; set; }
public DbSet<TicketDocument> TicketDocuments { get; set; }
public DbSet<Review> Reviews { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasPostgresExtension("postgis");
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}

View File

@ -0,0 +1,17 @@
using AutobusApi.Domain.IEntities;
using NetTopologySuite.Geometries;
namespace AutobusApi.Persistence.Entities;
public class Coordinates : ICoordinates
{
private readonly Point point;
public Coordinates(double latitude, double longitude)
{
point = new Point(latitude, longitude);
}
public double Latitude { get => point.X; }
public double Longitude { get => point.Y; }
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,722 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using NetTopologySuite.Geometries;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AutobusApi.Persistence.Migrations
{
/// <inheritdoc />
public partial class initial_create : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("Npgsql:PostgresExtension:postgis", ",,");
migrationBuilder.CreateTable(
name: "carriages",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
type = table.Column<string>(type: "varchar(16)", nullable: false),
capacity = table.Column<int>(type: "int", nullable: false),
number = table.Column<int>(type: "int", nullable: false),
has_wifi = table.Column<bool>(type: "boolean", nullable: false),
has_outlets = table.Column<bool>(type: "boolean", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_carriages", x => x.id);
});
migrationBuilder.CreateTable(
name: "companies",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "varchar(64)", nullable: false),
legal_address = table.Column<string>(type: "varchar(256)", nullable: false),
contact_email = table.Column<string>(type: "varchar(256)", nullable: false),
contact_phone_number = table.Column<string>(type: "varchar(16)", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_companies", x => x.id);
});
migrationBuilder.CreateTable(
name: "countries",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "varchar(64)", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_countries", x => x.id);
});
migrationBuilder.CreateTable(
name: "routes",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_routes", x => x.id);
});
migrationBuilder.CreateTable(
name: "ticket_documents",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
type = table.Column<string>(type: "varchar(32)", nullable: false),
information = table.Column<string>(type: "varchar(256)", nullable: false),
ticket_group_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ticket_documents", x => x.id);
});
migrationBuilder.CreateTable(
name: "users",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_users", x => x.id);
});
migrationBuilder.CreateTable(
name: "employees",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
first_name = table.Column<string>(type: "varchar(32)", nullable: false),
last_name = table.Column<string>(type: "varchar(32)", nullable: false),
patronymic = table.Column<string>(type: "varchar(32)", nullable: false),
sex = table.Column<string>(type: "varchar(16)", nullable: false),
birth_date = table.Column<DateOnly>(type: "date", nullable: false),
employer_company_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_employees", x => x.id);
table.ForeignKey(
name: "fk_employees_companies_employerCompanyId",
column: x => x.employer_company_id,
principalTable: "companies",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "vehicles",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
company_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_vehicles", x => x.id);
table.ForeignKey(
name: "fk_vehicles_companies_companyId",
column: x => x.company_id,
principalTable: "companies",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "regions",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "varchar(64)", nullable: false),
country_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_regions", x => x.id);
table.ForeignKey(
name: "fk_regions_coutries_countryId",
column: x => x.country_id,
principalTable: "countries",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ticket_groups",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
buyer_first_name = table.Column<string>(type: "varchar(32)", nullable: false),
buyer_last_name = table.Column<string>(type: "varchar(32)", nullable: false),
buyer_phone_number = table.Column<string>(type: "varchar(16)", nullable: false),
buyer_email = table.Column<string>(type: "varchar(256)", nullable: false),
passenger_first_name = table.Column<string>(type: "varchar(32)", nullable: false),
passenger_last_name = table.Column<string>(type: "varchar(32)", nullable: false),
passenger_patronymic = table.Column<string>(type: "varchar(32)", nullable: false),
passenger_sex = table.Column<string>(type: "varchar(16)", nullable: false),
passenger_birth_date = table.Column<DateOnly>(type: "date", nullable: false),
purchase_timestamp_utc = table.Column<DateTime>(type: "timestamp", nullable: false),
is_returned = table.Column<bool>(type: "boolean", nullable: false),
user_id = table.Column<int>(type: "int", nullable: true),
TicketDocumentId = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ticket_groups", x => x.id);
table.ForeignKey(
name: "fk_ticketGroups_ticketDocuments_ticketDocumentId",
column: x => x.TicketDocumentId,
principalTable: "ticket_documents",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_ticketGroups_users_userId",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "employee_documents",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
type = table.Column<string>(type: "varchar(32)", nullable: false),
information = table.Column<string>(type: "varchar(256)", nullable: false),
employee_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_employee_documents", x => x.id);
table.ForeignKey(
name: "fk_employeeDocuments_employees_employeeId",
column: x => x.employee_id,
principalTable: "employees",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "aircrafts",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false),
number = table.Column<string>(type: "varchar(8)", nullable: false),
model = table.Column<string>(type: "varchar(64)", nullable: false),
capacity = table.Column<int>(type: "int", nullable: false),
has_wifi = table.Column<bool>(type: "boolean", nullable: false),
has_multimedia = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_aircrafts", x => x.id);
table.ForeignKey(
name: "fk_aircrafts_vehicles_id",
column: x => x.id,
principalTable: "vehicles",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "buses",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false),
number = table.Column<string>(type: "varchar(8)", nullable: false),
model = table.Column<string>(type: "varchar(64)", nullable: false),
capacity = table.Column<int>(type: "int", nullable: false),
has_climate_control = table.Column<bool>(type: "boolean", nullable: false),
has_wc = table.Column<bool>(type: "boolean", nullable: false),
has_wifi = table.Column<bool>(type: "boolean", nullable: false),
has_multimedia = table.Column<bool>(type: "boolean", nullable: false),
has_outlets = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_buses", x => x.id);
table.ForeignKey(
name: "fk_buses_vehicles_id",
column: x => x.id,
principalTable: "vehicles",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "trains",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false),
number = table.Column<string>(type: "varchar(8)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_trains", x => x.id);
table.ForeignKey(
name: "fk_trains_vehicles_id",
column: x => x.id,
principalTable: "vehicles",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "vehicle_enrollments",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
departure_timestamp_utc = table.Column<DateTime>(type: "timestamp", nullable: false),
vehicle_id = table.Column<int>(type: "int", nullable: false),
route_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_vehicle_enrollments", x => x.id);
table.ForeignKey(
name: "fk_vehicleEnrollments_routes_routeId",
column: x => x.route_id,
principalTable: "routes",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_vehicleEnrollments_vehicles_vehicleId",
column: x => x.vehicle_id,
principalTable: "vehicles",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "cities",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "varchar(64)", nullable: false),
region_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_cities", x => x.id);
table.ForeignKey(
name: "fk_cities_regions_regionId",
column: x => x.region_id,
principalTable: "regions",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "train_carriages",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
train_id = table.Column<int>(type: "int", nullable: false),
carriage_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_train_carriages", x => x.id);
table.ForeignKey(
name: "fk_trainCarriages_trains_carriageId",
column: x => x.train_id,
principalTable: "carriages",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_trainCarriages_trains_trainId",
column: x => x.train_id,
principalTable: "trains",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "reviews",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
rating = table.Column<decimal>(type: "numeric(1,0)", nullable: false),
comment = table.Column<string>(type: "varchar(128)", nullable: false),
post_timestamp_utc = table.Column<DateTime>(type: "timestamp", nullable: false),
user_id = table.Column<int>(type: "int", nullable: false),
vehicle_enrollment_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_reviews", x => x.id);
table.ForeignKey(
name: "fk_reviews_users_userId",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_reviews_vehicleEnrollments_vehicleEnrollmentId",
column: x => x.vehicle_enrollment_id,
principalTable: "vehicle_enrollments",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tickets",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ticket_group_id = table.Column<int>(type: "int", nullable: false),
vehicle_enrollment_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_tickets", x => x.id);
table.ForeignKey(
name: "fk_tickets_ticketGroups_ticketGroupId",
column: x => x.ticket_group_id,
principalTable: "ticket_groups",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_tickets_vehicleEnrollments_vehicleEnrollmentId",
column: x => x.vehicle_enrollment_id,
principalTable: "vehicle_enrollments",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "vehicle_enrollment_employees",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
employee_id = table.Column<int>(type: "int", nullable: false),
vehicle_enrollment_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_vehicle_enrollment_employees", x => x.id);
table.ForeignKey(
name: "fk_vehicleEnrollmentEmployees_employees_employeeId",
column: x => x.employee_id,
principalTable: "employees",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_vehicleEnrollmentEmployees_vehicleEnrollments_vehicleEnrollmentId",
column: x => x.vehicle_enrollment_id,
principalTable: "vehicle_enrollments",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "addresses",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
name = table.Column<string>(type: "varchar(64)", nullable: false),
location = table.Column<Point>(type: "geography(point)", nullable: false),
vehicle_type = table.Column<string>(type: "varchar(16)", nullable: false),
city_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_addresses", x => x.id);
table.ForeignKey(
name: "fk_addresses_city_id",
column: x => x.city_id,
principalTable: "cities",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "route_addresses",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
order = table.Column<int>(type: "int", nullable: false),
address_id = table.Column<int>(type: "int", nullable: false),
route_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_route_addresses", x => x.id);
table.ForeignKey(
name: "fk_routeAddresses_addresses_addressId",
column: x => x.address_id,
principalTable: "addresses",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_routeAddresses_routes_routeId",
column: x => x.route_id,
principalTable: "routes",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "route_address_details",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
time_to_next_address = table.Column<TimeSpan>(type: "interval", nullable: false),
cost_to_next_address = table.Column<double>(type: "numeric(16,4)", nullable: false),
current_address_stop_time = table.Column<TimeSpan>(type: "interval", nullable: false),
route_address_id = table.Column<int>(type: "int", nullable: false),
vehicle_enrollment_id = table.Column<int>(type: "int", nullable: false),
is_deleted = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_route_address_details", x => x.id);
table.ForeignKey(
name: "fk_routeAddressDetails_routeAddress_routeAddressId",
column: x => x.route_address_id,
principalTable: "route_addresses",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_routeAddressDetails_vehicleEnrollments_vehicleEnrollmentId",
column: x => x.vehicle_enrollment_id,
principalTable: "vehicle_enrollments",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_addresses_city_id",
table: "addresses",
column: "city_id");
migrationBuilder.CreateIndex(
name: "IX_cities_region_id",
table: "cities",
column: "region_id");
migrationBuilder.CreateIndex(
name: "IX_employee_documents_employee_id",
table: "employee_documents",
column: "employee_id");
migrationBuilder.CreateIndex(
name: "IX_employees_employer_company_id",
table: "employees",
column: "employer_company_id");
migrationBuilder.CreateIndex(
name: "IX_regions_country_id",
table: "regions",
column: "country_id");
migrationBuilder.CreateIndex(
name: "IX_reviews_user_id",
table: "reviews",
column: "user_id");
migrationBuilder.CreateIndex(
name: "IX_reviews_vehicle_enrollment_id",
table: "reviews",
column: "vehicle_enrollment_id");
migrationBuilder.CreateIndex(
name: "IX_route_address_details_route_address_id",
table: "route_address_details",
column: "route_address_id");
migrationBuilder.CreateIndex(
name: "IX_route_address_details_vehicle_enrollment_id",
table: "route_address_details",
column: "vehicle_enrollment_id");
migrationBuilder.CreateIndex(
name: "IX_route_addresses_address_id",
table: "route_addresses",
column: "address_id");
migrationBuilder.CreateIndex(
name: "IX_route_addresses_route_id",
table: "route_addresses",
column: "route_id");
migrationBuilder.CreateIndex(
name: "IX_ticket_groups_TicketDocumentId",
table: "ticket_groups",
column: "TicketDocumentId",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ticket_groups_user_id",
table: "ticket_groups",
column: "user_id");
migrationBuilder.CreateIndex(
name: "IX_tickets_ticket_group_id",
table: "tickets",
column: "ticket_group_id");
migrationBuilder.CreateIndex(
name: "IX_tickets_vehicle_enrollment_id",
table: "tickets",
column: "vehicle_enrollment_id");
migrationBuilder.CreateIndex(
name: "IX_train_carriages_train_id",
table: "train_carriages",
column: "train_id");
migrationBuilder.CreateIndex(
name: "IX_vehicle_enrollment_employees_employee_id",
table: "vehicle_enrollment_employees",
column: "employee_id");
migrationBuilder.CreateIndex(
name: "IX_vehicle_enrollment_employees_vehicle_enrollment_id",
table: "vehicle_enrollment_employees",
column: "vehicle_enrollment_id");
migrationBuilder.CreateIndex(
name: "IX_vehicle_enrollments_route_id",
table: "vehicle_enrollments",
column: "route_id");
migrationBuilder.CreateIndex(
name: "IX_vehicle_enrollments_vehicle_id",
table: "vehicle_enrollments",
column: "vehicle_id");
migrationBuilder.CreateIndex(
name: "IX_vehicles_company_id",
table: "vehicles",
column: "company_id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "aircrafts");
migrationBuilder.DropTable(
name: "buses");
migrationBuilder.DropTable(
name: "employee_documents");
migrationBuilder.DropTable(
name: "reviews");
migrationBuilder.DropTable(
name: "route_address_details");
migrationBuilder.DropTable(
name: "tickets");
migrationBuilder.DropTable(
name: "train_carriages");
migrationBuilder.DropTable(
name: "vehicle_enrollment_employees");
migrationBuilder.DropTable(
name: "route_addresses");
migrationBuilder.DropTable(
name: "ticket_groups");
migrationBuilder.DropTable(
name: "carriages");
migrationBuilder.DropTable(
name: "trains");
migrationBuilder.DropTable(
name: "employees");
migrationBuilder.DropTable(
name: "vehicle_enrollments");
migrationBuilder.DropTable(
name: "addresses");
migrationBuilder.DropTable(
name: "ticket_documents");
migrationBuilder.DropTable(
name: "users");
migrationBuilder.DropTable(
name: "routes");
migrationBuilder.DropTable(
name: "vehicles");
migrationBuilder.DropTable(
name: "cities");
migrationBuilder.DropTable(
name: "companies");
migrationBuilder.DropTable(
name: "regions");
migrationBuilder.DropTable(
name: "countries");
}
}
}

File diff suppressed because it is too large Load Diff