From 08263e692886291d0afad16ebe8b76364ddfd937 Mon Sep 17 00:00:00 2001 From: Andriy Borkovich Date: Wed, 10 Apr 2024 01:17:13 +0300 Subject: [PATCH] make driver-company relation strict, done refactoring --- src/BSMS.API/Controllers/BusController.cs | 2 +- .../Controllers/BusReviewController.cs | 3 +- src/BSMS.API/Controllers/CompanyController.cs | 2 +- src/BSMS.API/Controllers/DriverController.cs | 2 +- .../Controllers/PassengerController.cs | 2 +- src/BSMS.API/Controllers/RouteController.cs | 2 +- .../Bus/Commands/Create/CreateBusCommand.cs | 2 +- .../Create/CreateBusCommandValidator.cs | 9 +- .../Create/CreateDriverCommandHandler.cs | 3 +- .../Create/CreateDriverCommandValidator.cs | 4 +- src/BSMS.Core/Entities/Bus.cs | 2 +- src/BSMS.Core/Entities/Driver.cs | 2 +- .../Persistence/BusStationContext.cs | 4 + ...akeStrictDriverCompanyRelation.Designer.cs | 655 ++++++++++++++++++ ...9220254_MakeStrictDriverCompanyRelation.cs | 59 ++ .../BusStationContextModelSnapshot.cs | 8 +- 16 files changed, 739 insertions(+), 22 deletions(-) create mode 100644 src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.Designer.cs create mode 100644 src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.cs diff --git a/src/BSMS.API/Controllers/BusController.cs b/src/BSMS.API/Controllers/BusController.cs index 75cb72e..e54c4ab 100644 --- a/src/BSMS.API/Controllers/BusController.cs +++ b/src/BSMS.API/Controllers/BusController.cs @@ -19,7 +19,7 @@ public class BusController(ISender sender) : ControllerBase /// Bus data and its schedule data /// ID of the created bus [HttpPost("Create")] - public async Task> Create(CreateBusCommand command) + public async Task> Create(CreateBusCommand command) { var result = await sender.Send(command); diff --git a/src/BSMS.API/Controllers/BusReviewController.cs b/src/BSMS.API/Controllers/BusReviewController.cs index 41d53a8..17509b3 100644 --- a/src/BSMS.API/Controllers/BusReviewController.cs +++ b/src/BSMS.API/Controllers/BusReviewController.cs @@ -1,5 +1,6 @@ using BSMS.API.Extensions; using BSMS.Application.Features.BusReview.Commands.Create; +using BSMS.Application.Features.Common; using MediatR; using Microsoft.AspNetCore.Mvc; @@ -16,7 +17,7 @@ public class BusReviewController(ISender sender) : ControllerBase /// Bus and passenger IDs, marks data /// ID of the created review [HttpPost("Create")] - public async Task> Create(CreateBusReviewCommand command) + public async Task> Create(CreateBusReviewCommand command) { var result = await sender.Send(command); diff --git a/src/BSMS.API/Controllers/CompanyController.cs b/src/BSMS.API/Controllers/CompanyController.cs index 36c6421..ee5450c 100644 --- a/src/BSMS.API/Controllers/CompanyController.cs +++ b/src/BSMS.API/Controllers/CompanyController.cs @@ -18,7 +18,7 @@ public class CompanyController(ISender sender) : ControllerBase /// Company's data /// ID of the created company [HttpPost("Create")] - public async Task> Create(CreateCompanyCommand command) + public async Task> Create(CreateCompanyCommand command) { var result = await sender.Send(command); diff --git a/src/BSMS.API/Controllers/DriverController.cs b/src/BSMS.API/Controllers/DriverController.cs index d219342..3b14b44 100644 --- a/src/BSMS.API/Controllers/DriverController.cs +++ b/src/BSMS.API/Controllers/DriverController.cs @@ -18,7 +18,7 @@ public class DriverController(ISender sender) : ControllerBase /// Driver data /// ID of the created driver [HttpPost("Create")] - public async Task> Create(CreateDriverCommand command) + public async Task> Create(CreateDriverCommand command) { var result = await sender.Send(command); diff --git a/src/BSMS.API/Controllers/PassengerController.cs b/src/BSMS.API/Controllers/PassengerController.cs index 901a56d..0e3a44d 100644 --- a/src/BSMS.API/Controllers/PassengerController.cs +++ b/src/BSMS.API/Controllers/PassengerController.cs @@ -19,7 +19,7 @@ public class PassengerController(ISender sender) : ControllerBase /// Passenger's data /// ID of the created passenger [HttpPost("Create")] - public async Task> Create(CreatePassengerCommand command) + public async Task> Create(CreatePassengerCommand command) { var result = await sender.Send(command); diff --git a/src/BSMS.API/Controllers/RouteController.cs b/src/BSMS.API/Controllers/RouteController.cs index 75b9539..c2da202 100644 --- a/src/BSMS.API/Controllers/RouteController.cs +++ b/src/BSMS.API/Controllers/RouteController.cs @@ -18,7 +18,7 @@ public class RouteController(ISender sender) : ControllerBase /// Contains origin and destination of route and list of it's stops names /// ID of the created route [HttpPost("Create")] - public async Task> Create(CreateRouteCommand command) + public async Task> Create(CreateRouteCommand command) { var result = await sender.Send(command); diff --git a/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommand.cs b/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommand.cs index d04b710..65dcb12 100644 --- a/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommand.cs +++ b/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommand.cs @@ -9,7 +9,7 @@ public record CreateBusCommand( string Brand, int Capacity, string Number, - int? DriverId, + int DriverId, List BusScheduleEntries) : IRequest>; public record CreateBusSchedule( diff --git a/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommandValidator.cs b/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommandValidator.cs index 8dee0f5..7c05c12 100644 --- a/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommandValidator.cs +++ b/src/BSMS.Application/Features/Bus/Commands/Create/CreateBusCommandValidator.cs @@ -69,14 +69,9 @@ private bool HaveNoTimeIntersections(List newBusSchedules) return !enumerable.Exists(group => HasTimeIntersectionsInGroup(group.ToList())); } - private async Task DriverExists(int? driverId, CancellationToken cancellationToken) + private async Task DriverExists(int driverId, CancellationToken cancellationToken) { - if (driverId is not null) - { - return await _driverRepository.AnyAsync(d => d.DriverId == driverId); - } - - return true; + return await _driverRepository.AnyAsync(d => d.DriverId == driverId); } private bool HasTimeIntersectionsInGroup(List schedules) diff --git a/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandHandler.cs b/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandHandler.cs index 763a9c4..41ed306 100644 --- a/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandHandler.cs +++ b/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandHandler.cs @@ -13,8 +13,7 @@ public record CreateDriverCommand( string FirstName, string LastName, string DriverLicense, - int? CompanyId, - int? BusId) : IRequest>; + int CompanyId) : IRequest>; public class CreateDriverCommandHandler( IDriverRepository repository, diff --git a/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandValidator.cs b/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandValidator.cs index 09de4f7..b94aaa1 100644 --- a/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandValidator.cs +++ b/src/BSMS.Application/Features/Driver/Commands/Create/CreateDriverCommandValidator.cs @@ -34,8 +34,8 @@ public CreateDriverCommandValidator(ICompanyRepository companyRepository) .WithMessage("Chosen company must exist!"); } - private Task CompanyExists(int? companyId, CancellationToken cancellationToken) + private Task CompanyExists(int companyId, CancellationToken cancellationToken) { - return companyId is not null ? _companyRepository.AnyAsync(c => c.CompanyId == companyId) : Task.FromResult(true); + return _companyRepository.AnyAsync(c => c.CompanyId == companyId); } } \ No newline at end of file diff --git a/src/BSMS.Core/Entities/Bus.cs b/src/BSMS.Core/Entities/Bus.cs index e2c4224..771a045 100644 --- a/src/BSMS.Core/Entities/Bus.cs +++ b/src/BSMS.Core/Entities/Bus.cs @@ -17,6 +17,6 @@ public class Bus /// public List BusScheduleEntries { get; set; } public List BusReviews { get; set; } - public Driver? Driver { get; set; } + public Driver Driver { get; set; } public List Seats { get; set; } } diff --git a/src/BSMS.Core/Entities/Driver.cs b/src/BSMS.Core/Entities/Driver.cs index 78d98f3..b8b2c9e 100644 --- a/src/BSMS.Core/Entities/Driver.cs +++ b/src/BSMS.Core/Entities/Driver.cs @@ -5,7 +5,7 @@ namespace BSMS.Core.Entities; public class Driver { public int DriverId { get; set; } - public int? CompanyId { get; set; } + public int CompanyId { get; set; } [StringLength(50)] public string FirstName { get; set; } = null!; diff --git a/src/BSMS.Infrastructure/Persistence/BusStationContext.cs b/src/BSMS.Infrastructure/Persistence/BusStationContext.cs index 235a3a2..fbd0adf 100644 --- a/src/BSMS.Infrastructure/Persistence/BusStationContext.cs +++ b/src/BSMS.Infrastructure/Persistence/BusStationContext.cs @@ -31,6 +31,10 @@ public BusStationContext(DbContextOptions options) protected override void OnModelCreating(ModelBuilder modelBuilder) { + // prevent from SaveChanges() exception caused by trigger + modelBuilder.Entity() + .ToTable(tb => tb.UseSqlOutputClause(false)); + modelBuilder.Entity() .HasMany(s => s.TicketStartStops) .WithOne(t => t.StartStop) diff --git a/src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.Designer.cs b/src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.Designer.cs new file mode 100644 index 0000000..a226725 --- /dev/null +++ b/src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.Designer.cs @@ -0,0 +1,655 @@ +// +using System; +using BSMS.Infrastructure.Persistence; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BSMS.Infrastructure.Persistence.Migrations +{ + [DbContext(typeof(BusStationContext))] + [Migration("20240409220254_MakeStrictDriverCompanyRelation")] + partial class MakeStrictDriverCompanyRelation + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BSMS.Core.Entities.Bus", b => + { + b.Property("BusId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("BusId")); + + b.Property("Brand") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Capacity") + .HasColumnType("int"); + + b.Property("DriverId") + .HasColumnType("int"); + + b.Property("Number") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.HasKey("BusId"); + + b.HasIndex("DriverId"); + + b.ToTable("Buses"); + + b.HasAnnotation("SqlServer:UseSqlOutputClause", false); + }); + + modelBuilder.Entity("BSMS.Core.Entities.BusReview", b => + { + b.Property("BusReviewId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("BusReviewId")); + + b.Property("BusId") + .HasColumnType("int"); + + b.Property("ComfortRating") + .HasColumnType("int"); + + b.Property("Comments") + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("InternetConnectionRating") + .HasColumnType("int"); + + b.Property("PassengerId") + .HasColumnType("int"); + + b.Property("PriceQualityRatioRating") + .HasColumnType("int"); + + b.Property("PunctualityRating") + .HasColumnType("int"); + + b.Property("ReviewDate") + .HasColumnType("datetime2"); + + b.Property("SanitaryConditionsRating") + .HasColumnType("int"); + + b.HasKey("BusReviewId"); + + b.HasIndex("BusId"); + + b.HasIndex("PassengerId"); + + b.ToTable("BusReviews"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.BusScheduleEntry", b => + { + b.Property("BusScheduleEntryId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("BusScheduleEntryId")); + + b.Property("ArrivalTime") + .HasColumnType("time"); + + b.Property("BusId") + .HasColumnType("int"); + + b.Property("Day") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.Property("DepartureTime") + .HasColumnType("time"); + + b.Property("MoveDirection") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.Property("RouteId") + .HasColumnType("int"); + + b.HasKey("BusScheduleEntryId"); + + b.HasIndex("BusId"); + + b.HasIndex("RouteId"); + + b.ToTable("BusScheduleEntries"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Company", b => + { + b.Property("CompanyId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("CompanyId")); + + b.Property("Address") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("ContactEmail") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("ContactPhone") + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("CompanyId"); + + b.ToTable("Companies"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Driver", b => + { + b.Property("DriverId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("DriverId")); + + b.Property("CompanyId") + .HasColumnType("int"); + + b.Property("DriverLicense") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("FirstName") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("LastName") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("DriverId"); + + b.HasIndex("CompanyId"); + + b.ToTable("Drivers"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Passenger", b => + { + b.Property("PassengerId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("PassengerId")); + + b.Property("Email") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("FirstName") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("LastName") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("PhoneNumber") + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.HasKey("PassengerId"); + + b.ToTable("Passengers"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Route", b => + { + b.Property("RouteId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RouteId")); + + b.Property("Destination") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Origin") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("RouteId"); + + b.ToTable("Routes"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Seat", b => + { + b.Property("SeatId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("SeatId")); + + b.Property("BusId") + .HasColumnType("int"); + + b.Property("IsFree") + .HasColumnType("bit"); + + b.Property("SeatNumber") + .HasColumnType("int"); + + b.HasKey("SeatId"); + + b.HasIndex("BusId"); + + b.ToTable("Seats"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Stop", b => + { + b.Property("StopId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("StopId")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("PreviousStopId") + .HasColumnType("int"); + + b.Property("RouteId") + .HasColumnType("int"); + + b.HasKey("StopId"); + + b.HasIndex("PreviousStopId"); + + b.HasIndex("RouteId"); + + b.ToTable("Stops"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Ticket", b => + { + b.Property("TicketId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TicketId")); + + b.Property("EndStopId") + .HasColumnType("int"); + + b.Property("IsSold") + .HasColumnType("bit"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.Property("SeatId") + .HasColumnType("int"); + + b.Property("StartStopId") + .HasColumnType("int"); + + b.HasKey("TicketId"); + + b.HasIndex("EndStopId"); + + b.HasIndex("SeatId"); + + b.HasIndex("StartStopId"); + + b.ToTable("Tickets"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.TicketPayment", b => + { + b.Property("TicketPaymentId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TicketPaymentId")); + + b.Property("Date") + .HasColumnType("datetime2"); + + b.Property("PassengerId") + .HasColumnType("int"); + + b.Property("PaymentType") + .HasColumnType("int"); + + b.Property("TicketId") + .HasColumnType("int"); + + b.Property("TicketStatus") + .HasColumnType("int"); + + b.Property("TripId") + .HasColumnType("int"); + + b.HasKey("TicketPaymentId"); + + b.HasIndex("PassengerId"); + + b.HasIndex("TicketId") + .IsUnique() + .HasFilter("[TicketId] IS NOT NULL"); + + b.HasIndex("TripId"); + + b.ToTable("TicketPayments"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Trip", b => + { + b.Property("TripId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TripId")); + + b.Property("ArrivalTime") + .HasColumnType("datetime2"); + + b.Property("BusScheduleEntryId") + .HasColumnType("int"); + + b.Property("DepartureTime") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.HasKey("TripId"); + + b.HasIndex("BusScheduleEntryId"); + + b.ToTable("Trips"); + }); + + modelBuilder.Entity("BSMS.Core.Views.BusDetailsView", b => + { + b.Property("BusId") + .HasColumnType("int"); + + b.Property("Brand") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Capacity") + .HasColumnType("int"); + + b.Property("CompanyName") + .HasColumnType("nvarchar(max)"); + + b.Property("DriverName") + .HasColumnType("nvarchar(max)"); + + b.Property("Number") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Rating") + .HasColumnType("float"); + + b.HasKey("BusId"); + + b.ToTable((string)null); + + b.ToView("BusDetailsView", (string)null); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Bus", b => + { + b.HasOne("BSMS.Core.Entities.Driver", "Driver") + .WithMany("Buses") + .HasForeignKey("DriverId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Driver"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.BusReview", b => + { + b.HasOne("BSMS.Core.Entities.Bus", "Bus") + .WithMany("BusReviews") + .HasForeignKey("BusId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BSMS.Core.Entities.Passenger", "Passenger") + .WithMany("BusReviews") + .HasForeignKey("PassengerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bus"); + + b.Navigation("Passenger"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.BusScheduleEntry", b => + { + b.HasOne("BSMS.Core.Entities.Bus", "Bus") + .WithMany("BusScheduleEntries") + .HasForeignKey("BusId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BSMS.Core.Entities.Route", "Route") + .WithMany("BusScheduleEntries") + .HasForeignKey("RouteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bus"); + + b.Navigation("Route"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Driver", b => + { + b.HasOne("BSMS.Core.Entities.Company", "Company") + .WithMany("Drivers") + .HasForeignKey("CompanyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Seat", b => + { + b.HasOne("BSMS.Core.Entities.Bus", "Bus") + .WithMany("Seats") + .HasForeignKey("BusId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bus"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Stop", b => + { + b.HasOne("BSMS.Core.Entities.Stop", "PreviousStop") + .WithMany() + .HasForeignKey("PreviousStopId"); + + b.HasOne("BSMS.Core.Entities.Route", "Route") + .WithMany("Stops") + .HasForeignKey("RouteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("PreviousStop"); + + b.Navigation("Route"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Ticket", b => + { + b.HasOne("BSMS.Core.Entities.Stop", "EndStop") + .WithMany("TicketEndStops") + .HasForeignKey("EndStopId") + .IsRequired(); + + b.HasOne("BSMS.Core.Entities.Seat", "Seat") + .WithMany("Tickets") + .HasForeignKey("SeatId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BSMS.Core.Entities.Stop", "StartStop") + .WithMany("TicketStartStops") + .HasForeignKey("StartStopId") + .IsRequired(); + + b.Navigation("EndStop"); + + b.Navigation("Seat"); + + b.Navigation("StartStop"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.TicketPayment", b => + { + b.HasOne("BSMS.Core.Entities.Passenger", "Passenger") + .WithMany("Payments") + .HasForeignKey("PassengerId"); + + b.HasOne("BSMS.Core.Entities.Ticket", "Ticket") + .WithOne("Payment") + .HasForeignKey("BSMS.Core.Entities.TicketPayment", "TicketId"); + + b.HasOne("BSMS.Core.Entities.Trip", "Trip") + .WithMany("BoughtTickets") + .HasForeignKey("TripId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Passenger"); + + b.Navigation("Ticket"); + + b.Navigation("Trip"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Trip", b => + { + b.HasOne("BSMS.Core.Entities.BusScheduleEntry", "BusScheduleEntry") + .WithMany("Trips") + .HasForeignKey("BusScheduleEntryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("BusScheduleEntry"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Bus", b => + { + b.Navigation("BusReviews"); + + b.Navigation("BusScheduleEntries"); + + b.Navigation("Seats"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.BusScheduleEntry", b => + { + b.Navigation("Trips"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Company", b => + { + b.Navigation("Drivers"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Driver", b => + { + b.Navigation("Buses"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Passenger", b => + { + b.Navigation("BusReviews"); + + b.Navigation("Payments"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Route", b => + { + b.Navigation("BusScheduleEntries"); + + b.Navigation("Stops"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Seat", b => + { + b.Navigation("Tickets"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Stop", b => + { + b.Navigation("TicketEndStops"); + + b.Navigation("TicketStartStops"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Ticket", b => + { + b.Navigation("Payment"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Trip", b => + { + b.Navigation("BoughtTickets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.cs b/src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.cs new file mode 100644 index 0000000..bfb9193 --- /dev/null +++ b/src/BSMS.Infrastructure/Persistence/Migrations/20240409220254_MakeStrictDriverCompanyRelation.cs @@ -0,0 +1,59 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BSMS.Infrastructure.Persistence.Migrations +{ + /// + public partial class MakeStrictDriverCompanyRelation : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Drivers_Companies_CompanyId", + table: "Drivers"); + + migrationBuilder.AlterColumn( + name: "CompanyId", + table: "Drivers", + type: "int", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "int", + oldNullable: true); + + migrationBuilder.AddForeignKey( + name: "FK_Drivers_Companies_CompanyId", + table: "Drivers", + column: "CompanyId", + principalTable: "Companies", + principalColumn: "CompanyId", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Drivers_Companies_CompanyId", + table: "Drivers"); + + migrationBuilder.AlterColumn( + name: "CompanyId", + table: "Drivers", + type: "int", + nullable: true, + oldClrType: typeof(int), + oldType: "int"); + + migrationBuilder.AddForeignKey( + name: "FK_Drivers_Companies_CompanyId", + table: "Drivers", + column: "CompanyId", + principalTable: "Companies", + principalColumn: "CompanyId"); + } + } +} diff --git a/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs b/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs index 542751f..3aa7d35 100644 --- a/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs +++ b/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs @@ -51,6 +51,8 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasIndex("DriverId"); b.ToTable("Buses"); + + b.HasAnnotation("SqlServer:UseSqlOutputClause", false); }); modelBuilder.Entity("BSMS.Core.Entities.BusReview", b => @@ -176,7 +178,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("DriverId")); - b.Property("CompanyId") + b.Property("CompanyId") .HasColumnType("int"); b.Property("DriverLicense") @@ -491,7 +493,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.HasOne("BSMS.Core.Entities.Company", "Company") .WithMany("Drivers") - .HasForeignKey("CompanyId"); + .HasForeignKey("CompanyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); b.Navigation("Company"); });