diff --git a/src/BSMS.Application/Contracts/Persistence/ITicketStatusRepository.cs b/src/BSMS.Application/Contracts/Persistence/ITicketStatusRepository.cs deleted file mode 100644 index 43593cf..0000000 --- a/src/BSMS.Application/Contracts/Persistence/ITicketStatusRepository.cs +++ /dev/null @@ -1,7 +0,0 @@ -using BSMS.Core.Entities; - -namespace BSMS.Application.Contracts.Persistence; - -public interface ITicketStatusRepository : IGenericRepository -{ -} \ No newline at end of file diff --git a/src/BSMS.Application/Contracts/Persistence/ITripStatusRepository.cs b/src/BSMS.Application/Contracts/Persistence/ITripStatusRepository.cs deleted file mode 100644 index f1faa66..0000000 --- a/src/BSMS.Application/Contracts/Persistence/ITripStatusRepository.cs +++ /dev/null @@ -1,7 +0,0 @@ -using BSMS.Core.Entities; - -namespace BSMS.Application.Contracts.Persistence; - -public interface ITripStatusRepository : IGenericRepository -{ -} \ No newline at end of file diff --git a/src/BSMS.Application/MappingProfiles/TicketProfile.cs b/src/BSMS.Application/MappingProfiles/TicketProfile.cs index 4ad45b4..08804ef 100644 --- a/src/BSMS.Application/MappingProfiles/TicketProfile.cs +++ b/src/BSMS.Application/MappingProfiles/TicketProfile.cs @@ -1,5 +1,6 @@ using BSMS.Application.Features.Ticket.Commands.Create; using BSMS.Core.Entities; +using BSMS.Core.Enums; using Mapster; namespace BSMS.Application.MappingProfiles; @@ -9,9 +10,9 @@ public class TicketProfile : IRegister public void Register(TypeAdapterConfig config) { config.NewConfig() - .AfterMapping((_, dest) => + .AfterMapping((_, dest) => { - dest.IsSold = false; + dest.Status = TicketStatus.Active; }); } -} \ No newline at end of file +} diff --git a/src/BSMS.Core/Entities/Ticket.cs b/src/BSMS.Core/Entities/Ticket.cs index e805c20..b70c29e 100644 --- a/src/BSMS.Core/Entities/Ticket.cs +++ b/src/BSMS.Core/Entities/Ticket.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations.Schema; +using BSMS.Core.Enums; namespace BSMS.Core.Entities; @@ -12,10 +13,10 @@ public class Ticket [Column(TypeName = "decimal(18,2)")] public decimal Price { get; set; } public bool IsSold { get; set; } + public TicketStatus Status {get; set; } public Stop StartStop { get; set; } = null!; public Stop EndStop { get; set; } = null!; public Seat Seat { get; set; } = null!; public TicketPayment Payment { get; set; } = null!; - public List Statuses { get; set; } } diff --git a/src/BSMS.Core/Entities/TicketStatus.cs b/src/BSMS.Core/Entities/TicketStatus.cs deleted file mode 100644 index e838da6..0000000 --- a/src/BSMS.Core/Entities/TicketStatus.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace BSMS.Core.Entities; -/// -/// represent ticket status chronology -/// -public class TicketStatus -{ - public int TicketStatusId { get; set; } - public int TicketId { get; set; } - - public Enums.TicketStatus Status { get; set; } - public DateTime CreatedDate { get; set; } - - public Ticket Ticket { get; set; } -} \ No newline at end of file diff --git a/src/BSMS.Core/Entities/Trip.cs b/src/BSMS.Core/Entities/Trip.cs index 124a917..2f015d1 100644 --- a/src/BSMS.Core/Entities/Trip.cs +++ b/src/BSMS.Core/Entities/Trip.cs @@ -9,10 +9,16 @@ public class Trip public int TripId { get; set; } public int BusScheduleEntryId { get; set; } - public DateTime DepartureTime { get; set; } - public DateTime ArrivalTime { get; set; } + /// + /// actual time when trip has started + /// + public DateTime? DepartureTime { get; set; } + /// + /// actual time when trip has ended + /// + public DateTime? ArrivalTime { get; set; } + public TripStatus Status {get; set; } public BusScheduleEntry BusScheduleEntry { get; set; } public List BoughtTickets { get; set; } - public List Statuses { get; set; } } diff --git a/src/BSMS.Core/Entities/TripStatus.cs b/src/BSMS.Core/Entities/TripStatus.cs deleted file mode 100644 index c82217e..0000000 --- a/src/BSMS.Core/Entities/TripStatus.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace BSMS.Core.Entities; -/// -/// represent trip status chronology -/// -public class TripStatus -{ - public int TripStatusId { get; set; } - public int TripId { get; set; } - - public Enums.TripStatus Status { get; set; } - public DateTime CreatedDate { get; set; } - - public Trip Trip { get; set; } -} \ No newline at end of file diff --git a/src/BSMS.Core/Enums/TripStatus.cs b/src/BSMS.Core/Enums/TripStatus.cs index 1e7494e..995c01a 100644 --- a/src/BSMS.Core/Enums/TripStatus.cs +++ b/src/BSMS.Core/Enums/TripStatus.cs @@ -4,8 +4,6 @@ public enum TripStatus { Scheduled = 100, InTransit = 200, - Delayed = 300, Canceled = 400, - Disrupted = 500, - Completed = 600 + Completed = 500 } \ No newline at end of file diff --git a/src/BSMS.Infrastructure/Persistence/BusStationContext.cs b/src/BSMS.Infrastructure/Persistence/BusStationContext.cs index c3e6dfe..638b508 100644 --- a/src/BSMS.Infrastructure/Persistence/BusStationContext.cs +++ b/src/BSMS.Infrastructure/Persistence/BusStationContext.cs @@ -27,8 +27,6 @@ public BusStationContext(DbContextOptions options) public DbSet Tickets { get; set; } public DbSet TicketPayments { get; set; } public DbSet Trips { get; set; } - public DbSet TicketStatuses { get; set; } - public DbSet TripStatuses { get; set; } public DbSet Users { get; set; } public DbSet BusesDetailsView { get; set; } @@ -42,8 +40,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .ToTable(tb => tb.UseSqlOutputClause(false)); modelBuilder.Entity() .ToTable(tb => tb.UseSqlOutputClause(false)); - modelBuilder.Entity() - .ToTable(tb => tb.UseSqlOutputClause(false)); modelBuilder.Entity() .ToTable(tb => tb.UseSqlOutputClause(false)); modelBuilder.Entity() @@ -80,12 +76,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .HasConversion() .HasMaxLength(20); - modelBuilder.Entity() + modelBuilder.Entity() .Property(ts => ts.Status) .HasConversion() .HasMaxLength(20); - modelBuilder.Entity() + modelBuilder.Entity() .Property(ts => ts.Status) .HasConversion() .HasMaxLength(20); @@ -95,15 +91,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .WithOne(p => p.Ticket) .OnDelete(DeleteBehavior.ClientCascade); - modelBuilder.Entity() - .HasMany(t => t.Statuses) - .WithOne(s => s.Ticket) - .OnDelete(DeleteBehavior.ClientCascade); - - modelBuilder.Entity() - .HasMany(t => t.Statuses) - .WithOne(s => s.Trip) - .OnDelete(DeleteBehavior.ClientCascade); modelBuilder.HasDbFunction( typeof(BusStationContext).GetMethod(nameof(StopsBelongToSameRoute), diff --git a/src/BSMS.Infrastructure/Persistence/Migrations/20240501081028_DeleteTripAndTicketStatusTables.Designer.cs b/src/BSMS.Infrastructure/Persistence/Migrations/20240501081028_DeleteTripAndTicketStatusTables.Designer.cs new file mode 100644 index 0000000..9dd0b0d --- /dev/null +++ b/src/BSMS.Infrastructure/Persistence/Migrations/20240501081028_DeleteTripAndTicketStatusTables.Designer.cs @@ -0,0 +1,731 @@ +// +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("20240501081028_DeleteTripAndTicketStatusTables")] + partial class DeleteTripAndTicketStatusTables + { + /// + 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("City") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("ContactEmail") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("ContactPhone") + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.Property("Country") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Street") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("ZipCode") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("nvarchar(10)"); + + 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.Property("OverallDistance") + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("int") + .HasComputedColumnSql("dbo.CalculateTotalDistanceForRoute([RouteId])"); + + b.HasKey("RouteId"); + + b.ToTable("Routes"); + + b.HasAnnotation("SqlServer:UseSqlOutputClause", false); + }); + + 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("DistanceToPrevious") + .HasColumnType("int"); + + 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"); + + b.HasAnnotation("SqlServer:UseSqlOutputClause", false); + }); + + 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.Property("Status") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.HasKey("TicketId"); + + b.HasIndex("EndStopId"); + + b.HasIndex("SeatId"); + + b.HasIndex("StartStopId"); + + b.ToTable("Tickets"); + + b.HasAnnotation("SqlServer:UseSqlOutputClause", false); + }); + + modelBuilder.Entity("BSMS.Core.Entities.TicketPayment", b => + { + b.Property("TicketPaymentId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TicketPaymentId")); + + b.Property("PassengerId") + .HasColumnType("int"); + + b.Property("PaymentDate") + .HasColumnType("datetime2"); + + b.Property("PaymentType") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.Property("TicketId") + .HasColumnType("int"); + + b.Property("TripId") + .HasColumnType("int"); + + b.HasKey("TicketPaymentId"); + + b.HasIndex("PassengerId"); + + b.HasIndex("TicketId") + .IsUnique(); + + 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") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + + b.HasKey("TripId"); + + b.HasIndex("BusScheduleEntryId"); + + b.ToTable("Trips"); + }); + + modelBuilder.Entity("BSMS.Core.Entities.User", b => + { + b.Property("UserId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("UserId")); + + b.Property("Email") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("LastLoginDate") + .HasColumnType("datetime2"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("varbinary(max)"); + + b.Property("PasswordSalt") + .IsRequired() + .HasColumnType("varbinary(max)"); + + b.Property("Role") + .HasColumnType("int"); + + b.Property("Username") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("UserId"); + + b.ToTable("Users"); + }); + + 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") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BSMS.Core.Entities.Ticket", "Ticket") + .WithOne("Payment") + .HasForeignKey("BSMS.Core.Entities.TicketPayment", "TicketId") + .OnDelete(DeleteBehavior.ClientCascade) + .IsRequired(); + + 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") + .IsRequired(); + }); + + modelBuilder.Entity("BSMS.Core.Entities.Trip", b => + { + b.Navigation("BoughtTickets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/BSMS.Infrastructure/Persistence/Migrations/20240501081028_DeleteTripAndTicketStatusTables.cs b/src/BSMS.Infrastructure/Persistence/Migrations/20240501081028_DeleteTripAndTicketStatusTables.cs new file mode 100644 index 0000000..a2b7339 --- /dev/null +++ b/src/BSMS.Infrastructure/Persistence/Migrations/20240501081028_DeleteTripAndTicketStatusTables.cs @@ -0,0 +1,209 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BSMS.Infrastructure.Persistence.Migrations +{ + /// + public partial class DeleteTripAndTicketStatusTables : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql(@" + DROP TRIGGER IF EXISTS HandleSeatStateOnStatusInsert; + DROP TRIGGER IF EXISTS AddActiveTicketStatus"); + migrationBuilder.DropTable( + name: "TicketStatuses"); + + migrationBuilder.DropTable( + name: "TripStatuses"); + + migrationBuilder.AlterColumn( + name: "DepartureTime", + table: "Trips", + type: "datetime2", + nullable: true, + oldClrType: typeof(DateTime), + oldType: "datetime2"); + + migrationBuilder.AlterColumn( + name: "ArrivalTime", + table: "Trips", + type: "datetime2", + nullable: true, + oldClrType: typeof(DateTime), + oldType: "datetime2"); + + migrationBuilder.AddColumn( + name: "Status", + table: "Trips", + type: "nvarchar(20)", + maxLength: 20, + nullable: false, + defaultValue: ""); + + migrationBuilder.AddColumn( + name: "Status", + table: "Tickets", + type: "nvarchar(20)", + maxLength: 20, + nullable: false, + defaultValue: ""); + + migrationBuilder.Sql(@" + CREATE OR ALTER TRIGGER [dbo].[HandleSeatStateOnTicketInsert] + ON [dbo].[Tickets] + AFTER INSERT + AS + BEGIN + DECLARE @TicketId INT, @SeatId INT; + DECLARE @Status NVARCHAR(20); + + SELECT @TicketId = inserted.TicketId, @Status = inserted.Status + FROM inserted; + + EXEC HandleSeatAvailability @TicketId, @Status; + END; + GO + "); + + migrationBuilder.Sql(@" + CREATE OR ALTER TRIGGER [dbo].[HandleSeatStateOnTicketUpdate] + ON [dbo].[Tickets] + AFTER UPDATE + AS + BEGIN + DECLARE @TicketId INT, @SeatId INT; + DECLARE @Status NVARCHAR(20); + + SELECT @TicketId = updated.TicketId, @Status = updated.Status + FROM updated; + + EXEC HandleSeatAvailability @TicketId, @Status; + END;"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql(@" + DROP TRIGGER IF EXISTS HandleSeatStateOnTicketUpdate; + DROP TRIGGER IF EXISTS HandleSeatStateOnTicketInsert;"); + + migrationBuilder.DropColumn( + name: "Status", + table: "Trips"); + + migrationBuilder.DropColumn( + name: "Status", + table: "Tickets"); + + migrationBuilder.AlterColumn( + name: "DepartureTime", + table: "Trips", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + oldClrType: typeof(DateTime), + oldType: "datetime2", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "ArrivalTime", + table: "Trips", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + oldClrType: typeof(DateTime), + oldType: "datetime2", + oldNullable: true); + + migrationBuilder.CreateTable( + name: "TicketStatuses", + columns: table => new + { + TicketStatusId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TicketId = table.Column(type: "int", nullable: false), + CreatedDate = table.Column(type: "datetime2", nullable: false), + Status = table.Column(type: "nvarchar(20)", maxLength: 20, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TicketStatuses", x => x.TicketStatusId); + table.ForeignKey( + name: "FK_TicketStatuses_Tickets_TicketId", + column: x => x.TicketId, + principalTable: "Tickets", + principalColumn: "TicketId"); + }); + + migrationBuilder.CreateTable( + name: "TripStatuses", + columns: table => new + { + TripStatusId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TripId = table.Column(type: "int", nullable: false), + CreatedDate = table.Column(type: "datetime2", nullable: false), + Status = table.Column(type: "nvarchar(20)", maxLength: 20, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TripStatuses", x => x.TripStatusId); + table.ForeignKey( + name: "FK_TripStatuses_Trips_TripId", + column: x => x.TripId, + principalTable: "Trips", + principalColumn: "TripId"); + }); + + migrationBuilder.CreateIndex( + name: "IX_TicketStatuses_TicketId", + table: "TicketStatuses", + column: "TicketId"); + + migrationBuilder.CreateIndex( + name: "IX_TripStatuses_TripId", + table: "TripStatuses", + column: "TripId"); + + migrationBuilder.Sql(@" + CREATE OR ALTER TRIGGER [dbo].[AddActiveTicketStatus] + ON [dbo].[Tickets] + AFTER INSERT + AS + BEGIN + DECLARE @TicketId INT, @SeatId INT; + DECLARE @CreatedDate DATETIME = GETUTCDATE(); + DECLARE @Status NVARCHAR(20) = 'Active'; + + SELECT @TicketId = inserted.TicketId, @Status = Status + FROM inserted; + + INSERT INTO TicketStatuses (TicketId, CreatedDate, Status) + VALUES (@TicketId, @CreatedDate, @Status); + END; + GO"); + + migrationBuilder.Sql(@" + ALTER TRIGGER [dbo].[HandleSeatStateOnStatusInsert] + ON [dbo].[TicketStatuses] + AFTER INSERT + AS + BEGIN + DECLARE @TicketId INT; + DECLARE @NewStatus NVARCHAR(20); + + SELECT @TicketId = inserted.TicketId, + @NewStatus = inserted.Status + FROM inserted; + + EXEC HandleSeatAvailability @TicketId, @NewStatus; + + END;"); + } + } +} diff --git a/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs b/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs index 81717a3..0d0ea89 100644 --- a/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs +++ b/src/BSMS.Infrastructure/Persistence/Migrations/BusStationContextModelSnapshot.cs @@ -358,6 +358,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("StartStopId") .HasColumnType("int"); + b.Property("Status") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("nvarchar(20)"); + b.HasKey("TicketId"); b.HasIndex("EndStopId"); @@ -408,34 +413,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("TicketPayments"); }); - modelBuilder.Entity("BSMS.Core.Entities.TicketStatus", b => - { - b.Property("TicketStatusId") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TicketStatusId")); - - b.Property("CreatedDate") - .HasColumnType("datetime2"); - - b.Property("Status") - .IsRequired() - .HasMaxLength(20) - .HasColumnType("nvarchar(20)"); - - b.Property("TicketId") - .HasColumnType("int"); - - b.HasKey("TicketStatusId"); - - b.HasIndex("TicketId"); - - b.ToTable("TicketStatuses"); - - b.HasAnnotation("SqlServer:UseSqlOutputClause", false); - }); - modelBuilder.Entity("BSMS.Core.Entities.Trip", b => { b.Property("TripId") @@ -444,31 +421,13 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TripId")); - b.Property("ArrivalTime") + b.Property("ArrivalTime") .HasColumnType("datetime2"); b.Property("BusScheduleEntryId") .HasColumnType("int"); - b.Property("DepartureTime") - .HasColumnType("datetime2"); - - b.HasKey("TripId"); - - b.HasIndex("BusScheduleEntryId"); - - b.ToTable("Trips"); - }); - - modelBuilder.Entity("BSMS.Core.Entities.TripStatus", b => - { - b.Property("TripStatusId") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("TripStatusId")); - - b.Property("CreatedDate") + b.Property("DepartureTime") .HasColumnType("datetime2"); b.Property("Status") @@ -476,14 +435,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(20) .HasColumnType("nvarchar(20)"); - b.Property("TripId") - .HasColumnType("int"); - - b.HasKey("TripStatusId"); + b.HasKey("TripId"); - b.HasIndex("TripId"); + b.HasIndex("BusScheduleEntryId"); - b.ToTable("TripStatuses"); + b.ToTable("Trips"); }); modelBuilder.Entity("BSMS.Core.Entities.User", b => @@ -695,17 +651,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("Trip"); }); - modelBuilder.Entity("BSMS.Core.Entities.TicketStatus", b => - { - b.HasOne("BSMS.Core.Entities.Ticket", "Ticket") - .WithMany("Statuses") - .HasForeignKey("TicketId") - .OnDelete(DeleteBehavior.ClientCascade) - .IsRequired(); - - b.Navigation("Ticket"); - }); - modelBuilder.Entity("BSMS.Core.Entities.Trip", b => { b.HasOne("BSMS.Core.Entities.BusScheduleEntry", "BusScheduleEntry") @@ -717,17 +662,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Navigation("BusScheduleEntry"); }); - modelBuilder.Entity("BSMS.Core.Entities.TripStatus", b => - { - b.HasOne("BSMS.Core.Entities.Trip", "Trip") - .WithMany("Statuses") - .HasForeignKey("TripId") - .OnDelete(DeleteBehavior.ClientCascade) - .IsRequired(); - - b.Navigation("Trip"); - }); - modelBuilder.Entity("BSMS.Core.Entities.Bus", b => { b.Navigation("BusReviews"); @@ -782,15 +716,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) { b.Navigation("Payment") .IsRequired(); - - b.Navigation("Statuses"); }); modelBuilder.Entity("BSMS.Core.Entities.Trip", b => { b.Navigation("BoughtTickets"); - - b.Navigation("Statuses"); }); #pragma warning restore 612, 618 } diff --git a/src/BSMS.Infrastructure/Persistence/Repositories/TicketStatusRepository.cs b/src/BSMS.Infrastructure/Persistence/Repositories/TicketStatusRepository.cs deleted file mode 100644 index 74b152a..0000000 --- a/src/BSMS.Infrastructure/Persistence/Repositories/TicketStatusRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ -using BSMS.Application.Contracts.Persistence; -using BSMS.Core.Entities; - -namespace BSMS.Infrastructure.Persistence.Repositories; - -public class TicketStatusRepository : GenericRepository, ITicketStatusRepository -{ - public TicketStatusRepository(BusStationContext context) : base(context) - { - } -} \ No newline at end of file diff --git a/src/BSMS.Infrastructure/Persistence/Repositories/TripStatusRepository.cs b/src/BSMS.Infrastructure/Persistence/Repositories/TripStatusRepository.cs deleted file mode 100644 index 86d63b9..0000000 --- a/src/BSMS.Infrastructure/Persistence/Repositories/TripStatusRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ -using BSMS.Application.Contracts.Persistence; -using BSMS.Core.Entities; - -namespace BSMS.Infrastructure.Persistence.Repositories; - -public class TripStatusRepository : GenericRepository, ITripStatusRepository -{ - public TripStatusRepository(BusStationContext context) : base(context) - { - } -} \ No newline at end of file