From 39fddff553af55e286dd99ddcaccff6733c8eab3 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Wed, 24 May 2023 16:53:31 +0300 Subject: [PATCH] refactor: change VehicleEnrollment CRUD to perform only compound data manipulations This removes enpoints where you can CRUD only VehicleEnrollment database table and refines those where you can CRUD both VehicleEnrollment and RouteAddressDetail --- Server/Configurations/MapperInitializer.cs | 16 +- .../VehicleEnrollmentController.cs | 48 +- ...d_field_from_VehicleEnrollment.Designer.cs | 952 ++++++++++++++++++ ...sCancelled_field_from_VehicleEnrollment.cs | 37 + .../ApplicationDbContextModelSnapshot.cs | 6 - Server/Models/Route.cs | 4 +- Server/Models/Vehicle.cs | 4 +- Server/Models/VehicleEnrollment.cs | 24 +- Server/Program.cs | 1 - .../IVehicleEnrollmentManagementService.cs | 24 +- .../VehicleEnrollmentManagementService.cs | 271 +---- .../DatabaseModels/VehicleEnrollmentDto.cs | 29 +- .../Objects/VehicleEnrollmentParameters.cs | 11 +- .../VehicleEnrollmentWithDetailsParameters.cs | 24 - 14 files changed, 1078 insertions(+), 373 deletions(-) create mode 100644 Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.Designer.cs create mode 100644 Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.cs delete mode 100644 SharedModels/QueryParameters/Objects/VehicleEnrollmentWithDetailsParameters.cs diff --git a/Server/Configurations/MapperInitializer.cs b/Server/Configurations/MapperInitializer.cs index 89b196b..82623bb 100644 --- a/Server/Configurations/MapperInitializer.cs +++ b/Server/Configurations/MapperInitializer.cs @@ -10,6 +10,7 @@ public class MapperInitializer : Profile public MapperInitializer() { RecognizePostfixes("Utc"); + RecognizeDestinationPostfixes("Utc"); CreateMap().ReverseMap(); CreateMap().ReverseMap(); @@ -47,21 +48,10 @@ public class MapperInitializer : Profile CreateMap().ReverseMap(); CreateMap().ReverseMap(); - CreateMap().IncludeMembers(t => t.TicketGroup).ReverseMap(); + CreateMap().ReverseMap(); CreateMap(); - /* - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - CreateMap(); - CreateMap(); - */ CreateMap().ReverseMap(); - /* - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - */ CreateMap().ReverseMap(); CreateMap().ReverseMap(); @@ -81,8 +71,6 @@ public class MapperInitializer : Profile CreateMap().ReverseMap(); CreateMap().ReverseMap(); CreateMap().ReverseMap(); - CreateMap().ReverseMap(); - CreateMap().ReverseMap(); CreateMap(); CreateMap().ReverseMap(); diff --git a/Server/Controllers/VehicleEnrollmentController.cs b/Server/Controllers/VehicleEnrollmentController.cs index 3ae094b..3e3ee14 100644 --- a/Server/Controllers/VehicleEnrollmentController.cs +++ b/Server/Controllers/VehicleEnrollmentController.cs @@ -32,20 +32,6 @@ public class VehicleEnrollmentController : ControllerBase return CreatedAtAction(nameof(GetEnrollment), new {id = result.enrollment.Id}, result.enrollment); } - - [Authorize(Policy = "CompanyAccess")] - [HttpPost("withDetails")] - public async Task AddEnrollmentWithDetails(CreateVehicleEnrollmentWithDetailsDto enrollment) - { - var result = await _vehicleEnrollmentManagementService.AddEnrollmentWithDetails(enrollment); - - if (!result.isSucceed) - { - return result.actionResult; - } - - return CreatedAtAction(nameof(GetEnrollment), new {id = result.enrollment.Id}, result.enrollment); - } [Authorize(Policy = "DriverAccess")] [HttpGet] @@ -63,23 +49,7 @@ public class VehicleEnrollmentController : ControllerBase return Ok(result.enrollments); } - [Authorize(Policy = "DriverAccess")] - [HttpGet("withDetails")] - public async Task GetEnrollments([FromQuery] VehicleEnrollmentWithDetailsParameters parameters) - { - var result = await _vehicleEnrollmentManagementService.GetEnrollmentsWithDetails(parameters); - - if (!result.isSucceed) - { - return result.actionResult; - } - - Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(result.pagingMetadata)); - - return Ok(result.enrollments); - } - - [Authorize(Policy = "DriverAccess")] + [Authorize(Policy = "CompanyAccess")] [HttpGet("{id}")] public async Task GetEnrollment(int id, [FromQuery] string? fields) { @@ -92,26 +62,12 @@ public class VehicleEnrollmentController : ControllerBase return Ok(result.enrollment); } - - [Authorize(Policy = "CompanyAccess")] - [HttpGet("withDetails/{id}")] - public async Task GetEnrollmentWithDetails(int id, [FromQuery] string? fields) - { - var result = await _vehicleEnrollmentManagementService.GetEnrollmentWithDetails(id, fields); - - if (!result.isSucceed) - { - return result.actionResult; - } - - return Ok(result.enrollment); - } [Authorize(Policy = "CompanyAccess")] [HttpPut("{id}")] public async Task UpdateVehicle(int id, UpdateVehicleEnrollmentDto enrollment) { - var result = await _vehicleEnrollmentManagementService.UpdateEnrollment(enrollment); + var result = await _vehicleEnrollmentManagementService.UpdateEnrollment(id, enrollment); if (!result.isSucceed) { diff --git a/Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.Designer.cs b/Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.Designer.cs new file mode 100644 index 0000000..d6883e6 --- /dev/null +++ b/Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.Designer.cs @@ -0,0 +1,952 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Server.Data; + +#nullable disable + +namespace Server.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment")] + partial class Remove_IsCancelled_field_from_VehicleEnrollment + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property("Id") + .HasColumnType("text"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("text"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("text"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("Server.Models.Address", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CityId") + .HasColumnType("integer"); + + b.Property("Latitude") + .HasColumnType("double precision"); + + b.Property("Longitude") + .HasColumnType("double precision"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("CityId"); + + b.ToTable("Addresses"); + }); + + modelBuilder.Entity("Server.Models.City", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("StateId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("StateId"); + + b.ToTable("Cities"); + }); + + modelBuilder.Entity("Server.Models.Company", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OwnerId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("OwnerId") + .IsUnique(); + + b.ToTable("Companies"); + }); + + modelBuilder.Entity("Server.Models.CompanyDriver", b => + { + b.Property("CompanyId") + .HasColumnType("integer"); + + b.Property("DriverId") + .HasColumnType("text"); + + b.HasKey("CompanyId", "DriverId"); + + b.HasIndex("DriverId") + .IsUnique(); + + b.ToTable("CompanyDrivers"); + }); + + modelBuilder.Entity("Server.Models.Country", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Code") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Countries"); + }); + + modelBuilder.Entity("Server.Models.Review", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Comment") + .HasColumnType("text"); + + b.Property("PostDateTimeUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Rating") + .HasColumnType("integer"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.Property("VehicleEnrollmentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.HasIndex("VehicleEnrollmentId"); + + b.ToTable("Reviews"); + }); + + modelBuilder.Entity("Server.Models.Route", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Type") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Routes"); + }); + + modelBuilder.Entity("Server.Models.RouteAddress", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AddressId") + .HasColumnType("integer"); + + b.Property("Order") + .HasColumnType("integer"); + + b.Property("RouteAddressDetailsId") + .HasColumnType("integer"); + + b.Property("RouteId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("AddressId"); + + b.HasIndex("RouteId"); + + b.ToTable("RouteAddresses"); + }); + + modelBuilder.Entity("Server.Models.RouteAddressDetails", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CostToNextCity") + .HasColumnType("double precision"); + + b.Property("RouteAddressId") + .HasColumnType("integer"); + + b.Property("TimeSpanToNextCity") + .HasColumnType("interval"); + + b.Property("VehicleEnrollmentId") + .HasColumnType("integer"); + + b.Property("WaitTimeSpan") + .HasColumnType("interval"); + + b.HasKey("Id"); + + b.HasIndex("RouteAddressId"); + + b.HasIndex("VehicleEnrollmentId"); + + b.ToTable("RouteAddressDetails"); + }); + + modelBuilder.Entity("Server.Models.State", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CountryId") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("CountryId"); + + b.ToTable("States"); + }); + + modelBuilder.Entity("Server.Models.Ticket", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("FirstRouteAddressId") + .HasColumnType("integer"); + + b.Property("LastRouteAddressId") + .HasColumnType("integer"); + + b.Property("TicketGroupId") + .HasColumnType("integer"); + + b.Property("VehicleEnrollmentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TicketGroupId"); + + b.HasIndex("VehicleEnrollmentId"); + + b.ToTable("Tickets"); + }); + + modelBuilder.Entity("Server.Models.TicketGroup", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("IsReturned") + .HasColumnType("boolean"); + + b.Property("PurchaseDateTimeUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("TicketGroups"); + }); + + modelBuilder.Entity("Server.Models.User", b => + { + b.Property("Id") + .HasColumnType("text"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("BirthDate") + .HasColumnType("timestamp with time zone"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Document") + .HasColumnType("integer"); + + b.Property("DocumentDetails") + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Gender") + .HasColumnType("integer"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("Patronymic") + .IsRequired() + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.ToTable("AspNetUsers", (string)null); + }); + + modelBuilder.Entity("Server.Models.Vehicle", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("CompanyId") + .HasColumnType("integer"); + + b.Property("HasBelts") + .HasColumnType("boolean"); + + b.Property("HasClimateControl") + .HasColumnType("boolean"); + + b.Property("HasOutlet") + .HasColumnType("boolean"); + + b.Property("HasStewardess") + .HasColumnType("boolean"); + + b.Property("HasTV") + .HasColumnType("boolean"); + + b.Property("HasWC") + .HasColumnType("boolean"); + + b.Property("HasWiFi") + .HasColumnType("boolean"); + + b.Property("Number") + .IsRequired() + .HasColumnType("text"); + + b.Property("Type") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("CompanyId"); + + b.ToTable("Vehicles"); + }); + + modelBuilder.Entity("Server.Models.VehicleEnrollment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CancellationComment") + .HasColumnType("text"); + + b.Property("DepartureDateTimeUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("RouteId") + .HasColumnType("integer"); + + b.Property("VehicleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RouteId"); + + b.HasIndex("VehicleId"); + + b.ToTable("VehicleEnrollments"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Server.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Server.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Server.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Server.Models.Address", b => + { + b.HasOne("Server.Models.City", "City") + .WithMany("Addresses") + .HasForeignKey("CityId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("City"); + }); + + modelBuilder.Entity("Server.Models.City", b => + { + b.HasOne("Server.Models.State", "State") + .WithMany("Cities") + .HasForeignKey("StateId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("State"); + }); + + modelBuilder.Entity("Server.Models.Company", b => + { + b.HasOne("Server.Models.User", "Owner") + .WithOne("Company") + .HasForeignKey("Server.Models.Company", "OwnerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Owner"); + }); + + modelBuilder.Entity("Server.Models.CompanyDriver", b => + { + b.HasOne("Server.Models.Company", "Company") + .WithMany("CompanyDrivers") + .HasForeignKey("CompanyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.User", "Driver") + .WithOne("Employer") + .HasForeignKey("Server.Models.CompanyDriver", "DriverId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + + b.Navigation("Driver"); + }); + + modelBuilder.Entity("Server.Models.Review", b => + { + b.HasOne("Server.Models.User", "User") + .WithMany("Reviews") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.VehicleEnrollment", "VehicleEnrollment") + .WithMany("Reviews") + .HasForeignKey("VehicleEnrollmentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + + b.Navigation("VehicleEnrollment"); + }); + + modelBuilder.Entity("Server.Models.RouteAddress", b => + { + b.HasOne("Server.Models.Address", "Address") + .WithMany("RouteAddresses") + .HasForeignKey("AddressId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.Route", "Route") + .WithMany("RouteAddresses") + .HasForeignKey("RouteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Address"); + + b.Navigation("Route"); + }); + + modelBuilder.Entity("Server.Models.RouteAddressDetails", b => + { + b.HasOne("Server.Models.RouteAddress", "RouteAddress") + .WithMany("RouteAddressDetails") + .HasForeignKey("RouteAddressId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.VehicleEnrollment", "VehicleEnrollment") + .WithMany("RouteAddressDetails") + .HasForeignKey("VehicleEnrollmentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("RouteAddress"); + + b.Navigation("VehicleEnrollment"); + }); + + modelBuilder.Entity("Server.Models.State", b => + { + b.HasOne("Server.Models.Country", "Country") + .WithMany("States") + .HasForeignKey("CountryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Country"); + }); + + modelBuilder.Entity("Server.Models.Ticket", b => + { + b.HasOne("Server.Models.TicketGroup", "TicketGroup") + .WithMany("Tickets") + .HasForeignKey("TicketGroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.VehicleEnrollment", "VehicleEnrollment") + .WithMany("Tickets") + .HasForeignKey("VehicleEnrollmentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("TicketGroup"); + + b.Navigation("VehicleEnrollment"); + }); + + modelBuilder.Entity("Server.Models.TicketGroup", b => + { + b.HasOne("Server.Models.User", "User") + .WithMany("TicketGroups") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Server.Models.User", b => + { + b.OwnsMany("Server.Models.RefreshToken", "RefreshTokens", b1 => + { + b1.Property("UserId") + .HasColumnType("text"); + + b1.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b1.Property("Id")); + + b1.Property("CreationDateTime") + .HasColumnType("timestamp with time zone"); + + b1.Property("ExpiryDateTime") + .HasColumnType("timestamp with time zone"); + + b1.Property("Revoked") + .HasColumnType("timestamp with time zone"); + + b1.Property("Token") + .IsRequired() + .HasColumnType("text"); + + b1.HasKey("UserId", "Id"); + + b1.ToTable("RefreshToken"); + + b1.WithOwner() + .HasForeignKey("UserId"); + }); + + b.Navigation("RefreshTokens"); + }); + + modelBuilder.Entity("Server.Models.Vehicle", b => + { + b.HasOne("Server.Models.Company", "Company") + .WithMany("Vehicles") + .HasForeignKey("CompanyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + }); + + modelBuilder.Entity("Server.Models.VehicleEnrollment", b => + { + b.HasOne("Server.Models.Route", "Route") + .WithMany("VehicleEnrollments") + .HasForeignKey("RouteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Server.Models.Vehicle", "Vehicle") + .WithMany("VehicleEnrollments") + .HasForeignKey("VehicleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Route"); + + b.Navigation("Vehicle"); + }); + + modelBuilder.Entity("Server.Models.Address", b => + { + b.Navigation("RouteAddresses"); + }); + + modelBuilder.Entity("Server.Models.City", b => + { + b.Navigation("Addresses"); + }); + + modelBuilder.Entity("Server.Models.Company", b => + { + b.Navigation("CompanyDrivers"); + + b.Navigation("Vehicles"); + }); + + modelBuilder.Entity("Server.Models.Country", b => + { + b.Navigation("States"); + }); + + modelBuilder.Entity("Server.Models.Route", b => + { + b.Navigation("RouteAddresses"); + + b.Navigation("VehicleEnrollments"); + }); + + modelBuilder.Entity("Server.Models.RouteAddress", b => + { + b.Navigation("RouteAddressDetails"); + }); + + modelBuilder.Entity("Server.Models.State", b => + { + b.Navigation("Cities"); + }); + + modelBuilder.Entity("Server.Models.TicketGroup", b => + { + b.Navigation("Tickets"); + }); + + modelBuilder.Entity("Server.Models.User", b => + { + b.Navigation("Company") + .IsRequired(); + + b.Navigation("Employer"); + + b.Navigation("Reviews"); + + b.Navigation("TicketGroups"); + }); + + modelBuilder.Entity("Server.Models.Vehicle", b => + { + b.Navigation("VehicleEnrollments"); + }); + + modelBuilder.Entity("Server.Models.VehicleEnrollment", b => + { + b.Navigation("Reviews"); + + b.Navigation("RouteAddressDetails"); + + b.Navigation("Tickets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.cs b/Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.cs new file mode 100644 index 0000000..0a21dd0 --- /dev/null +++ b/Server/Migrations/20230524135132_Remove_IsCancelled_field_from_VehicleEnrollment.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Server.Migrations +{ + public partial class Remove_IsCancelled_field_from_VehicleEnrollment : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "DelayTimeSpan", + table: "VehicleEnrollments"); + + migrationBuilder.DropColumn( + name: "IsCanceled", + table: "VehicleEnrollments"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "DelayTimeSpan", + table: "VehicleEnrollments", + type: "interval", + nullable: true); + + migrationBuilder.AddColumn( + name: "IsCanceled", + table: "VehicleEnrollments", + type: "boolean", + nullable: false, + defaultValue: false); + } + } +} diff --git a/Server/Migrations/ApplicationDbContextModelSnapshot.cs b/Server/Migrations/ApplicationDbContextModelSnapshot.cs index 7a43def..425517b 100644 --- a/Server/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Server/Migrations/ApplicationDbContextModelSnapshot.cs @@ -589,15 +589,9 @@ namespace Server.Migrations b.Property("CancellationComment") .HasColumnType("text"); - b.Property("DelayTimeSpan") - .HasColumnType("interval"); - b.Property("DepartureDateTimeUtc") .HasColumnType("timestamp with time zone"); - b.Property("IsCanceled") - .HasColumnType("boolean"); - b.Property("RouteId") .HasColumnType("integer"); diff --git a/Server/Models/Route.cs b/Server/Models/Route.cs index 270702b..a53c520 100644 --- a/Server/Models/Route.cs +++ b/Server/Models/Route.cs @@ -15,7 +15,7 @@ public class Route public int GetCompanyEnrollmentCount(DateTime fromDate, DateTime toDate, int companyId) { return VehicleEnrollments.Count(ve => - !ve.IsCanceled && + !ve.IsCancelled() && ve.DepartureDateTimeUtc >= fromDate && ve.DepartureDateTimeUtc <= toDate && ve.Vehicle.CompanyId == companyId); } @@ -23,7 +23,7 @@ public class Route public int GetCompanyCanceledEnrollmentCount(DateTime fromDate, DateTime toDate, int companyId) { return VehicleEnrollments.Count(ve => - ve.IsCanceled && + ve.IsCancelled() && ve.DepartureDateTimeUtc >= fromDate && ve.DepartureDateTimeUtc <= toDate && ve.Vehicle.CompanyId == companyId); } diff --git a/Server/Models/Vehicle.cs b/Server/Models/Vehicle.cs index 9662bb7..6f4541e 100644 --- a/Server/Models/Vehicle.cs +++ b/Server/Models/Vehicle.cs @@ -29,7 +29,7 @@ public class Vehicle public int GetRouteEnrollmentCount(DateTime fromDate, DateTime toDate, int routeId) { return VehicleEnrollments.Count(ve => - !ve.IsCanceled && + !ve.IsCancelled() && ve.DepartureDateTimeUtc >= fromDate && ve.DepartureDateTimeUtc <= toDate && ve.RouteId == routeId); } @@ -37,7 +37,7 @@ public class Vehicle public int GetRouteCanceledEnrollmentCount(DateTime fromDate, DateTime toDate, int routeId) { return VehicleEnrollments.Count(ve => - ve.IsCanceled && + ve.IsCancelled() && ve.DepartureDateTimeUtc >= fromDate && ve.DepartureDateTimeUtc <= toDate && ve.RouteId == routeId); } diff --git a/Server/Models/VehicleEnrollment.cs b/Server/Models/VehicleEnrollment.cs index 3fc29c9..8b2e6c8 100644 --- a/Server/Models/VehicleEnrollment.cs +++ b/Server/Models/VehicleEnrollment.cs @@ -5,26 +5,22 @@ namespace Server.Models; public class VehicleEnrollment { - [Key] - public int Id { get; set; } - - [ForeignKey("VehicleId")] - public int VehicleId { get; set; } + [Key] public int Id { get; set; } + + [ForeignKey("VehicleId")] public int VehicleId { get; set; } public Vehicle Vehicle { get; set; } = null!; - - [ForeignKey("RouteId")] - public int RouteId { get; set; } + + [ForeignKey("RouteId")] public int RouteId { get; set; } public Route Route { get; set; } = null!; - + public virtual IList RouteAddressDetails { get; set; } = null!; - + public DateTime DepartureDateTimeUtc { get; set; } - public TimeSpan? DelayTimeSpan { get; set; } - - public bool IsCanceled { get; set; } = false; public string? CancellationComment { get; set; } = null!; - + public IList Tickets { get; set; } = null!; public IList Reviews { get; set; } = null!; + + public bool IsCancelled() => CancellationComment == null; } \ No newline at end of file diff --git a/Server/Program.cs b/Server/Program.cs index dad4ac9..c0be134 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -134,7 +134,6 @@ services.AddScoped, DataShaper>(); services.AddScoped, DataShaper>(); services.AddScoped, DataShaper>(); services.AddScoped, DataShaper>(); -services.AddScoped, DataShaper>(); services.AddScoped, DataShaper>(); services.AddScoped, DataShaper>(); services.AddScoped, DataShaper>(); diff --git a/Server/Services/IVehicleEnrollmentManagementService.cs b/Server/Services/IVehicleEnrollmentManagementService.cs index 5b99f3d..6c8a050 100644 --- a/Server/Services/IVehicleEnrollmentManagementService.cs +++ b/Server/Services/IVehicleEnrollmentManagementService.cs @@ -8,15 +8,17 @@ namespace Server.Services; public interface IVehicleEnrollmentManagementService { - Task<(bool isSucceed, IActionResult? actionResult, VehicleEnrollmentDto enrollment)> AddEnrollment(CreateVehicleEnrollmentDto createEnrollmentDto); - Task<(bool isSucceed, IActionResult? actionResult, VehicleEnrollmentWithDetailsDto enrollment)> AddEnrollmentWithDetails(CreateVehicleEnrollmentWithDetailsDto createEnrollmentDto); - Task<(bool isSucceed, IActionResult? actionResult, IEnumerable enrollments, - PagingMetadata pagingMetadata)> GetEnrollments(VehicleEnrollmentParameters parameters); - Task<(bool isSucceed, IActionResult? actionResult, IEnumerable enrollments, - PagingMetadata pagingMetadata)> GetEnrollmentsWithDetails(VehicleEnrollmentWithDetailsParameters parameters); - Task<(bool isSucceed, IActionResult? actionResult, ExpandoObject enrollment)> GetEnrollment(int id, string? fields); - Task<(bool isSucceed, IActionResult? actionResult, ExpandoObject enrollment)> GetEnrollmentWithDetails(int id, string? fields); - Task<(bool isSucceed, IActionResult? actionResult, VehicleEnrollmentDto enrollment)> UpdateEnrollment(UpdateVehicleEnrollmentDto updateEnrollmentDto); - Task<(bool isSucceed, IActionResult? actionResult)> DeleteEnrollment(int id); - Task IsEnrollmentExists(int id); + Task<(bool isSucceed, IActionResult actionResult, VehicleEnrollmentDto enrollment)> + AddEnrollment(CreateVehicleEnrollmentDto createEnrollmentDto); + + Task<(bool isSucceed, IActionResult actionResult, IEnumerable enrollments, PagingMetadata pagingMetadata)> + GetEnrollments(VehicleEnrollmentParameters parameters); + + Task<(bool isSucceed, IActionResult actionResult, ExpandoObject enrollment)> + GetEnrollment(int id, string? fields); + + Task<(bool isSucceed, IActionResult actionResult, VehicleEnrollmentDto enrollment)> + UpdateEnrollment(int vehicleEnrollmentId, UpdateVehicleEnrollmentDto updateEnrollmentDto); + + Task<(bool isSucceed, IActionResult actionResult)> DeleteEnrollment(int id); } \ No newline at end of file diff --git a/Server/Services/VehicleEnrollmentManagementService.cs b/Server/Services/VehicleEnrollmentManagementService.cs index 75e1e11..e36c171 100644 --- a/Server/Services/VehicleEnrollmentManagementService.cs +++ b/Server/Services/VehicleEnrollmentManagementService.cs @@ -1,6 +1,7 @@ using System.Dynamic; using AutoMapper; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.EntityFrameworkCore; using Server.Data; using Server.Helpers; @@ -18,45 +19,23 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe private readonly IMapper _mapper; private readonly ISortHelper _enrollmentSortHelper; private readonly IDataShaper _enrollmentDataShaper; - private readonly IDataShaper _enrollmentWithDetailsDataShaper; private readonly IPager _pager; private readonly ISessionUserService _sessionUserService; - public VehicleEnrollmentManagementService(ApplicationDbContext dbContext, - IMapper mapper, ISortHelper enrollmentSortHelper, - IDataShaper enrollmentDataShaper, IPager pager, - IDataShaper enrollmentWithDetailsDataShaper, - ISessionUserService sessionUserService) + public VehicleEnrollmentManagementService(ApplicationDbContext dbContext, IMapper mapper, + ISortHelper enrollmentSortHelper, IDataShaper enrollmentDataShaper, + IPager pager, ISessionUserService sessionUserService) { _dbContext = dbContext; _mapper = mapper; _enrollmentSortHelper = enrollmentSortHelper; _enrollmentDataShaper = enrollmentDataShaper; _pager = pager; - _enrollmentWithDetailsDataShaper = enrollmentWithDetailsDataShaper; _sessionUserService = sessionUserService; } - public async Task<(bool isSucceed, IActionResult? actionResult, VehicleEnrollmentDto enrollment)> AddEnrollment(CreateVehicleEnrollmentDto createEnrollmentDto) - { - if (_sessionUserService.GetAuthUserRole() != Identity.Roles.Administrator.ToString()) - { - if (!(await _sessionUserService.IsAuthUserCompanyOwner()).isCompanyOwner && - !await _sessionUserService.IsAuthUserCompanyVehicle(createEnrollmentDto.VehicleId)) - { - return (false, new UnauthorizedResult(), null!); - } - } - - var enrollment = _mapper.Map(createEnrollmentDto); - - await _dbContext.VehicleEnrollments.AddAsync(enrollment); - await _dbContext.SaveChangesAsync(); - - return (true, null, _mapper.Map(enrollment)); - } - - public async Task<(bool isSucceed, IActionResult? actionResult, VehicleEnrollmentWithDetailsDto enrollment)> AddEnrollmentWithDetails(CreateVehicleEnrollmentWithDetailsDto createEnrollmentDto) + public async Task<(bool isSucceed, IActionResult actionResult, VehicleEnrollmentDto enrollment)> + AddEnrollment(CreateVehicleEnrollmentDto createEnrollmentDto) { if (_sessionUserService.GetAuthUserRole() != Identity.Roles.Administrator.ToString()) { @@ -76,126 +55,11 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe .Include(ve => ve.RouteAddressDetails) .FirstAsync(ve => ve.Id == enrollment.Id); - return (true, null, _mapper.Map(enrollment)); + return (true, null!, _mapper.Map(enrollment)); } - public async Task<(bool isSucceed, IActionResult? actionResult, IEnumerable enrollments, - PagingMetadata pagingMetadata)> GetEnrollments(VehicleEnrollmentParameters parameters) - { - var dbEnrollments = _dbContext.VehicleEnrollments - .AsQueryable(); - - if (_sessionUserService.GetAuthUserRole() != Identity.Roles.Administrator.ToString()) - { - var result = await _sessionUserService.IsAuthUserCompanyOwner(); - if (!result.isCompanyOwner) - { - return (false, new UnauthorizedResult(), null!, null!); - } - - dbEnrollments = dbEnrollments.Include(e => e.Vehicle) - .Where(e => e.Vehicle.CompanyId == result.companyId); - } - - SearchByAllEnrollmentFields(ref dbEnrollments, parameters.Search); - FilterByEnrollmentVehicleId(ref dbEnrollments, parameters.VehicleId); - FilterByEnrollmentRouteId(ref dbEnrollments, parameters.RouteId); - FilterByEnrollmentDepartureDateTime(ref dbEnrollments, - parameters.FromDepartureDateTime, parameters.ToDepartureDateTime); - FilterByEnrollmentDelayedValue(ref dbEnrollments, parameters.IsDelayed); - FilterByEnrollmentCancelledValue(ref dbEnrollments, parameters.IsCanceled); - - var enrollmentDtos = _mapper.ProjectTo(dbEnrollments); - var shapedData = _enrollmentDataShaper.ShapeData(enrollmentDtos, parameters.Fields).AsQueryable(); - - try - { - shapedData = _enrollmentSortHelper.ApplySort(shapedData, parameters.Sort); - } - catch (Exception) - { - return (false, new BadRequestObjectResult("Invalid sorting string"), null!, null!); - } - - var pagingMetadata = _pager.ApplyPaging(ref shapedData, parameters.PageNumber, - parameters.PageSize); - - return (true, null, shapedData, pagingMetadata); - - void SearchByAllEnrollmentFields(ref IQueryable enrollment, - string? search) - { - if (!enrollment.Any() || String.IsNullOrWhiteSpace(search)) - { - return; - } - - enrollment = enrollment.Where(e => - e.CancellationComment != null && e.CancellationComment.ToLower().Contains(search.ToLower())); - } - - void FilterByEnrollmentVehicleId(ref IQueryable enrollments, - int? vehicleId) - { - if (!enrollments.Any() || vehicleId == null) - { - return; - } - - enrollments = enrollments.Where(e => e.VehicleId == vehicleId); - } - - void FilterByEnrollmentRouteId(ref IQueryable enrollments, - int? routeId) - { - if (!enrollments.Any() || routeId == null) - { - return; - } - - enrollments = enrollments.Where(e => e.RouteId == routeId); - } - - void FilterByEnrollmentDepartureDateTime(ref IQueryable enrollments, - DateTime? fromDateTime, DateTime? toDateTime) - { - if (!enrollments.Any() || fromDateTime == null || toDateTime == null) - { - return; - } - - enrollments = enrollments.Where(e => - e.DepartureDateTimeUtc >= fromDateTime.Value.ToUniversalTime() && - e.DepartureDateTimeUtc <= toDateTime.Value.ToUniversalTime()); - } - - void FilterByEnrollmentDelayedValue(ref IQueryable enrollments, - bool? isDelayed) - { - if (!enrollments.Any() || !isDelayed.HasValue) - { - return; - } - - enrollments = isDelayed.Value - ? enrollments.Where(e => e.DelayTimeSpan != null) - : enrollments.Where(e => e.DelayTimeSpan == null); - } - - void FilterByEnrollmentCancelledValue(ref IQueryable enrollments, - bool? isCancelled) - { - if (!enrollments.Any() || !isCancelled.HasValue) - { - return; - } - - enrollments = enrollments.Where(e => e.IsCanceled == isCancelled); - } - } - - public async Task<(bool isSucceed, IActionResult? actionResult, IEnumerable enrollments, - PagingMetadata pagingMetadata)> GetEnrollmentsWithDetails(VehicleEnrollmentWithDetailsParameters parameters) + public async Task<(bool isSucceed, IActionResult actionResult, IEnumerable enrollments, PagingMetadata pagingMetadata)> + GetEnrollments(VehicleEnrollmentParameters parameters) { var dbEnrollments = _dbContext.VehicleEnrollments .Include(ve => ve.RouteAddressDetails) @@ -216,17 +80,14 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe SearchByAllEnrollmentFields(ref dbEnrollments, parameters.Search); FilterByEnrollmentVehicleId(ref dbEnrollments, parameters.VehicleId); FilterByEnrollmentRouteId(ref dbEnrollments, parameters.RouteId); - FilterByEnrollmentDepartureDateTime(ref dbEnrollments, - parameters.FromDepartureDateTime, parameters.ToDepartureDateTime); - FilterByEnrollmentDelayedValue(ref dbEnrollments, parameters.IsDelayed); - FilterByEnrollmentCancelledValue(ref dbEnrollments, parameters.IsCanceled); - FilterByEnrollmentTotalDuration(ref dbEnrollments, - parameters.FromTotalTripDuration, parameters.ToTotalTripDuration); - FilterByEnrollmentTotalCost(ref dbEnrollments, parameters.FromCost, - parameters.ToCost); + FilterByEnrollmentDepartureDateTime(ref dbEnrollments, parameters.FromDepartureDateTime, parameters.ToDepartureDateTime); + FilterByEnrollmentCancelledValue(ref dbEnrollments, parameters.IsCancelled); + FilterByEnrollmentTotalDuration(ref dbEnrollments, parameters.FromTotalTripDuration, parameters.ToTotalTripDuration); + FilterByEnrollmentTotalCost(ref dbEnrollments, parameters.FromCost, parameters.ToCost); - var enrollmentDtos = _mapper.ProjectTo(dbEnrollments); - var shapedData = _enrollmentWithDetailsDataShaper.ShapeData(enrollmentDtos, parameters.Fields).AsQueryable(); + var enrollmentDtos = _mapper.ProjectTo(dbEnrollments); + + var shapedData = _enrollmentDataShaper.ShapeData(enrollmentDtos, parameters.Fields).AsQueryable(); try { @@ -240,10 +101,9 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe var pagingMetadata = _pager.ApplyPaging(ref shapedData, parameters.PageNumber, parameters.PageSize); - return (true, null, shapedData, pagingMetadata); + return (true, null!, shapedData, pagingMetadata); - void SearchByAllEnrollmentFields(ref IQueryable enrollment, - string? search) + void SearchByAllEnrollmentFields(ref IQueryable enrollment, string? search) { if (!enrollment.Any() || String.IsNullOrWhiteSpace(search)) { @@ -254,8 +114,7 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe e.CancellationComment != null && e.CancellationComment.ToLower().Contains(search.ToLower())); } - void FilterByEnrollmentVehicleId(ref IQueryable enrollments, - int? vehicleId) + void FilterByEnrollmentVehicleId(ref IQueryable enrollments, int? vehicleId) { if (!enrollments.Any() || vehicleId == null) { @@ -265,8 +124,7 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe enrollments = enrollments.Where(e => e.VehicleId == vehicleId); } - void FilterByEnrollmentRouteId(ref IQueryable enrollments, - int? routeId) + void FilterByEnrollmentRouteId(ref IQueryable enrollments, int? routeId) { if (!enrollments.Any() || routeId == null) { @@ -276,8 +134,7 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe enrollments = enrollments.Where(e => e.RouteId == routeId); } - void FilterByEnrollmentDepartureDateTime(ref IQueryable enrollments, - DateTime? fromDateTime, DateTime? toDateTime) + void FilterByEnrollmentDepartureDateTime(ref IQueryable enrollments, DateTime? fromDateTime, DateTime? toDateTime) { if (!enrollments.Any() || fromDateTime == null || toDateTime == null) { @@ -289,32 +146,17 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe e.DepartureDateTimeUtc <= toDateTime.Value.ToUniversalTime()); } - void FilterByEnrollmentDelayedValue(ref IQueryable enrollments, - bool? isDelayed) - { - if (!enrollments.Any() || !isDelayed.HasValue) - { - return; - } - - enrollments = isDelayed.Value - ? enrollments.Where(e => e.DelayTimeSpan != null) - : enrollments.Where(e => e.DelayTimeSpan == null); - } - - void FilterByEnrollmentCancelledValue(ref IQueryable enrollments, - bool? isCancelled) + void FilterByEnrollmentCancelledValue(ref IQueryable enrollments, bool? isCancelled) { if (!enrollments.Any() || !isCancelled.HasValue) { return; } - - enrollments = enrollments.Where(e => e.IsCanceled == isCancelled); + + enrollments = enrollments.Where(e => (bool) isCancelled ? e.CancellationComment != null : e.CancellationComment == null); } - void FilterByEnrollmentTotalDuration(ref IQueryable enrollments, - TimeSpan? fromDuration, TimeSpan? toDuration) + void FilterByEnrollmentTotalDuration(ref IQueryable enrollments, TimeSpan? fromDuration, TimeSpan? toDuration) { if (!enrollments.Any() ) { @@ -343,8 +185,7 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe filteredEnrollmentsIds.Any(id => id == e.Id)); } - void FilterByEnrollmentTotalCost(ref IQueryable enrollments, - double? fromCost, double? toCost) + void FilterByEnrollmentTotalCost(ref IQueryable enrollments, double? fromCost, double? toCost) { if (!enrollments.Any() ) { @@ -374,36 +215,8 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe } } - public async Task<(bool isSucceed, IActionResult? actionResult, ExpandoObject enrollment)> GetEnrollment(int id, string? fields) - { - if (!await IsEnrollmentExists(id)) - { - return (false, new NotFoundResult(), null!); - } - - if (_sessionUserService.GetAuthUserRole() != Identity.Roles.Administrator.ToString()) - { - if (!await _sessionUserService.IsAuthUserCompanyVehicleEnrollment(id)) - { - return (false, new UnauthorizedResult(), null!); - } - } - - var dbEnrollment = await _dbContext.VehicleEnrollments.Where(e => e.Id == id) - .FirstAsync(); - - if (String.IsNullOrWhiteSpace(fields)) - { - fields = VehicleEnrollmentParameters.DefaultFields; - } - - var enrollmentDto = _mapper.Map(dbEnrollment); - var shapedData = _enrollmentDataShaper.ShapeData(enrollmentDto, fields); - - return (true, null, shapedData); - } - - public async Task<(bool isSucceed, IActionResult? actionResult, ExpandoObject enrollment)> GetEnrollmentWithDetails(int id, string? fields) + public async Task<(bool isSucceed, IActionResult actionResult, ExpandoObject enrollment)> + GetEnrollment(int id, string? fields) { if (!await IsEnrollmentExists(id)) { @@ -424,19 +237,25 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe if (String.IsNullOrWhiteSpace(fields)) { - fields = VehicleEnrollmentWithDetailsParameters.DefaultFields; + fields = VehicleEnrollmentParameters.DefaultFields; } - - var enrollmentDto = _mapper.Map(dbEnrollment); - var shapedData = _enrollmentWithDetailsDataShaper.ShapeData(enrollmentDto, fields); - return (true, null, shapedData); + var enrollmentDto = _mapper.Map(dbEnrollment); + var shapedData = _enrollmentDataShaper.ShapeData(enrollmentDto, fields); + + return (true, null!, shapedData); } - public async Task<(bool isSucceed, IActionResult? actionResult, VehicleEnrollmentDto enrollment)> UpdateEnrollment(UpdateVehicleEnrollmentDto updateEnrollmentDto) + public async Task<(bool isSucceed, IActionResult actionResult, VehicleEnrollmentDto enrollment)> + UpdateEnrollment(int vehicleEnrollmentId, UpdateVehicleEnrollmentDto updateEnrollmentDto) { + if (vehicleEnrollmentId != updateEnrollmentDto.Id) + { + return (false, new BadRequestObjectResult("Query id and object id must match"), null!); + } + var enrollment = _mapper.Map(updateEnrollmentDto); - _dbContext.Entry(enrollment).State = EntityState.Modified; + _dbContext.VehicleEnrollments.Update(enrollment); if (_sessionUserService.GetAuthUserRole() != Identity.Roles.Administrator.ToString()) { @@ -463,10 +282,10 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe var dbEnrollment = await _dbContext.VehicleEnrollments.FirstAsync(e => e.Id == enrollment.Id); - return (true, null, _mapper.Map(dbEnrollment)); + return (true, null!, _mapper.Map(dbEnrollment)); } - public async Task<(bool isSucceed, IActionResult? actionResult)> DeleteEnrollment(int id) + public async Task<(bool isSucceed, IActionResult actionResult)> DeleteEnrollment(int id) { if (_sessionUserService.GetAuthUserRole() != Identity.Roles.Administrator.ToString()) { @@ -486,10 +305,10 @@ public class VehicleEnrollmentManagementService : IVehicleEnrollmentManagementSe _dbContext.VehicleEnrollments.Remove(dbEnrollment); await _dbContext.SaveChangesAsync(); - return (true, null); + return (true, null!); } - public async Task IsEnrollmentExists(int id) + private async Task IsEnrollmentExists(int id) { return await _dbContext.VehicleEnrollments.AnyAsync(e => e.Id == id); } diff --git a/SharedModels/DataTransferObjects/DatabaseModels/VehicleEnrollmentDto.cs b/SharedModels/DataTransferObjects/DatabaseModels/VehicleEnrollmentDto.cs index f6ddfc9..87eb99a 100644 --- a/SharedModels/DataTransferObjects/DatabaseModels/VehicleEnrollmentDto.cs +++ b/SharedModels/DataTransferObjects/DatabaseModels/VehicleEnrollmentDto.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace SharedModels.DataTransferObjects.Model; @@ -6,47 +7,29 @@ public class VehicleEnrollmentDto : CreateVehicleEnrollmentDto { public int Id { get; set; } - public IList Reviews { get; set; } = null!; + public string? CancellationComment { get; set; } - public bool IsCanceled { get; set; } - public string? CancelationComment { get; set; } + [Required] + public new IList RouteAddressDetails { get; set; } = null!; } public class CreateVehicleEnrollmentDto { [Required] public int VehicleId { get; set; } - public VehicleDto? Vehicle { get; set; } = null!; [Required] public int RouteId { get; set; } - public RouteDto? Route { get; set; } = null!; [Required] [DataType(DataType.DateTime)] public DateTime DepartureDateTime { get; set; } -} - -public class UpdateVehicleEnrollmentDto : CreateVehicleEnrollmentDto -{ - [Required] - public int Id { get; set; } - public TimeSpan DelayTimeSpan { get; set; } = TimeSpan.Zero; - - public bool? IsCanceled { get; set; } = false; - public string? CancelationComment { get; set; } -} - -public class CreateVehicleEnrollmentWithDetailsDto : CreateVehicleEnrollmentDto -{ + [Required] public IList RouteAddressDetails { get; set; } = null!; } -public class VehicleEnrollmentWithDetailsDto : VehicleEnrollmentDto -{ - public IList RouteAddressDetails { get; set; } = null!; -} +public class UpdateVehicleEnrollmentDto : VehicleEnrollmentDto { } public class InReviewVehicleEnrollmentDto { diff --git a/SharedModels/QueryParameters/Objects/VehicleEnrollmentParameters.cs b/SharedModels/QueryParameters/Objects/VehicleEnrollmentParameters.cs index b6899e9..b91dd53 100644 --- a/SharedModels/QueryParameters/Objects/VehicleEnrollmentParameters.cs +++ b/SharedModels/QueryParameters/Objects/VehicleEnrollmentParameters.cs @@ -2,8 +2,8 @@ namespace SharedModels.QueryParameters.Objects; public class VehicleEnrollmentParameters : ParametersBase { - public const string DefaultFields = "id,vehicleId,vehicle,routeId,route,departureDateTime," + - "tickets,reviews,delayTimeSpan,isCanceled,cancelationComment"; + public const string DefaultFields = "id,vehicleId,vehicle,routeId,route,departureDateTime,tickets,reviews," + + "isCancelled,cancellationComment,routeAddressDetails"; public VehicleEnrollmentParameters() { @@ -14,6 +14,9 @@ public class VehicleEnrollmentParameters : ParametersBase public int? RouteId { get; set; } public DateTime? FromDepartureDateTime { get; set; } public DateTime? ToDepartureDateTime { get; set; } - public bool? IsDelayed { get; set; } - public bool? IsCanceled {get; set; } + public bool? IsCancelled {get; set; } + public TimeSpan? FromTotalTripDuration { get; set; } + public TimeSpan? ToTotalTripDuration { get; set; } + public double? FromCost { get; set; } + public double? ToCost { get; set; } } \ No newline at end of file diff --git a/SharedModels/QueryParameters/Objects/VehicleEnrollmentWithDetailsParameters.cs b/SharedModels/QueryParameters/Objects/VehicleEnrollmentWithDetailsParameters.cs deleted file mode 100644 index 79970c8..0000000 --- a/SharedModels/QueryParameters/Objects/VehicleEnrollmentWithDetailsParameters.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace SharedModels.QueryParameters.Objects; - -public class VehicleEnrollmentWithDetailsParameters : ParametersBase -{ - public const string DefaultFields = "id,vehicleId,vehicle,routeId,route,departureDateTimeUtc," + - "tickets,reviews,delayTimeSpan,isCancelled," + - "cancellationComment,routeAddressDetails"; - - public VehicleEnrollmentWithDetailsParameters() - { - Fields = DefaultFields; - } - - public int? VehicleId { get; set; } - public int? RouteId { get; set; } - public DateTime? FromDepartureDateTime { get; set; } - public DateTime? ToDepartureDateTime { get; set; } - public bool? IsDelayed { get; set; } - public bool? IsCanceled {get; set; } - public TimeSpan? FromTotalTripDuration { get; set; } - public TimeSpan? ToTotalTripDuration { get; set; } - public double? FromCost { get; set; } - public double? ToCost { get; set; } -} \ No newline at end of file