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
This commit is contained in:
cuqmbr 2023-05-24 16:53:31 +03:00
parent 63cc9a9edc
commit 39fddff553
14 changed files with 1078 additions and 373 deletions

View File

@ -10,6 +10,7 @@ public class MapperInitializer : Profile
public MapperInitializer()
{
RecognizePostfixes("Utc");
RecognizeDestinationPostfixes("Utc");
CreateMap<Country, CountryDto>().ReverseMap();
CreateMap<Country, CreateCountryDto>().ReverseMap();
@ -47,21 +48,10 @@ public class MapperInitializer : Profile
CreateMap<Route, CreateRouteWithAddressesDto>().ReverseMap();
CreateMap<Route, RouteWithAddressesDto>().ReverseMap();
CreateMap<Ticket, TicketDto>().IncludeMembers(t => t.TicketGroup).ReverseMap();
CreateMap<Ticket, TicketDto>().ReverseMap();
CreateMap<TicketGroup, TicketDto>();
/*
CreateMap<Ticket, CreateTicketDto>().ReverseMap();
CreateMap<Ticket, UpdateTicketDto>().ReverseMap();
CreateMap<Ticket, CreateInTicketGroupTicketDto>().ReverseMap();
CreateMap<Ticket, InTicketGroupTicketDto>();
CreateMap<Ticket, InVehicleEnrollmentTicketDto>();
*/
CreateMap<TicketGroup, TicketGroupDto>().ReverseMap();
/*
CreateMap<TicketGroup, CreateTicketGroupDto>().ReverseMap();
CreateMap<TicketGroup, UpdateTicketGroupDto>().ReverseMap();
*/
CreateMap<Review, ReviewDto>().ReverseMap();
CreateMap<Review, CreateReviewDto>().ReverseMap();
@ -81,8 +71,6 @@ public class MapperInitializer : Profile
CreateMap<VehicleEnrollment, VehicleEnrollmentDto>().ReverseMap();
CreateMap<VehicleEnrollment, CreateVehicleEnrollmentDto>().ReverseMap();
CreateMap<VehicleEnrollment, UpdateVehicleEnrollmentDto>().ReverseMap();
CreateMap<VehicleEnrollment, VehicleEnrollmentWithDetailsDto>().ReverseMap();
CreateMap<VehicleEnrollment, CreateVehicleEnrollmentWithDetailsDto>().ReverseMap();
CreateMap<VehicleEnrollment, InReviewVehicleEnrollmentDto>();
CreateMap<User, UserDto>().ReverseMap();

View File

@ -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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> UpdateVehicle(int id, UpdateVehicleEnrollmentDto enrollment)
{
var result = await _vehicleEnrollmentManagementService.UpdateEnrollment(enrollment);
var result = await _vehicleEnrollmentManagementService.UpdateEnrollment(id, enrollment);
if (!result.isSucceed)
{

View File

@ -0,0 +1,952 @@
// <auto-generated />
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<string>("Id")
.HasColumnType("text");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("text");
b.Property<string>("Name")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("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<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClaimType")
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("text");
b.Property<string>("RoleId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims", (string)null);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClaimType")
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("text");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims", (string)null);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider")
.HasColumnType("text");
b.Property<string>("ProviderKey")
.HasColumnType("text");
b.Property<string>("ProviderDisplayName")
.HasColumnType("text");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("text");
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins", (string)null);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("text");
b.Property<string>("RoleId")
.HasColumnType("text");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles", (string)null);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId")
.HasColumnType("text");
b.Property<string>("LoginProvider")
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("text");
b.Property<string>("Value")
.HasColumnType("text");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens", (string)null);
});
modelBuilder.Entity("Server.Models.Address", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("CityId")
.HasColumnType("integer");
b.Property<double>("Latitude")
.HasColumnType("double precision");
b.Property<double>("Longitude")
.HasColumnType("double precision");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("CityId");
b.ToTable("Addresses");
});
modelBuilder.Entity("Server.Models.City", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<int>("StateId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("StateId");
b.ToTable("Cities");
});
modelBuilder.Entity("Server.Models.Company", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("OwnerId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("OwnerId")
.IsUnique();
b.ToTable("Companies");
});
modelBuilder.Entity("Server.Models.CompanyDriver", b =>
{
b.Property<int>("CompanyId")
.HasColumnType("integer");
b.Property<string>("DriverId")
.HasColumnType("text");
b.HasKey("CompanyId", "DriverId");
b.HasIndex("DriverId")
.IsUnique();
b.ToTable("CompanyDrivers");
});
modelBuilder.Entity("Server.Models.Country", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Code")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Countries");
});
modelBuilder.Entity("Server.Models.Review", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Comment")
.HasColumnType("text");
b.Property<DateTime>("PostDateTimeUtc")
.HasColumnType("timestamp with time zone");
b.Property<int>("Rating")
.HasColumnType("integer");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("text");
b.Property<int>("VehicleEnrollmentId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("UserId");
b.HasIndex("VehicleEnrollmentId");
b.ToTable("Reviews");
});
modelBuilder.Entity("Server.Models.Route", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Routes");
});
modelBuilder.Entity("Server.Models.RouteAddress", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("AddressId")
.HasColumnType("integer");
b.Property<int>("Order")
.HasColumnType("integer");
b.Property<int>("RouteAddressDetailsId")
.HasColumnType("integer");
b.Property<int>("RouteId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("AddressId");
b.HasIndex("RouteId");
b.ToTable("RouteAddresses");
});
modelBuilder.Entity("Server.Models.RouteAddressDetails", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<double>("CostToNextCity")
.HasColumnType("double precision");
b.Property<int>("RouteAddressId")
.HasColumnType("integer");
b.Property<TimeSpan>("TimeSpanToNextCity")
.HasColumnType("interval");
b.Property<int>("VehicleEnrollmentId")
.HasColumnType("integer");
b.Property<TimeSpan>("WaitTimeSpan")
.HasColumnType("interval");
b.HasKey("Id");
b.HasIndex("RouteAddressId");
b.HasIndex("VehicleEnrollmentId");
b.ToTable("RouteAddressDetails");
});
modelBuilder.Entity("Server.Models.State", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("CountryId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("CountryId");
b.ToTable("States");
});
modelBuilder.Entity("Server.Models.Ticket", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("FirstRouteAddressId")
.HasColumnType("integer");
b.Property<int>("LastRouteAddressId")
.HasColumnType("integer");
b.Property<int>("TicketGroupId")
.HasColumnType("integer");
b.Property<int>("VehicleEnrollmentId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TicketGroupId");
b.HasIndex("VehicleEnrollmentId");
b.ToTable("Tickets");
});
modelBuilder.Entity("Server.Models.TicketGroup", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<bool>("IsReturned")
.HasColumnType("boolean");
b.Property<DateTime>("PurchaseDateTimeUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("UserId")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("TicketGroups");
});
modelBuilder.Entity("Server.Models.User", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.Property<int>("AccessFailedCount")
.HasColumnType("integer");
b.Property<DateTime?>("BirthDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("text");
b.Property<int?>("Document")
.HasColumnType("integer");
b.Property<string>("DocumentDetails")
.HasColumnType("text");
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("boolean");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("text");
b.Property<int?>("Gender")
.HasColumnType("integer");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("LockoutEnabled")
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("timestamp with time zone");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("PasswordHash")
.HasColumnType("text");
b.Property<string>("Patronymic")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.HasColumnType("text");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("boolean");
b.Property<string>("SecurityStamp")
.HasColumnType("text");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("boolean");
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");
b.ToTable("AspNetUsers", (string)null);
});
modelBuilder.Entity("Server.Models.Vehicle", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Capacity")
.HasColumnType("integer");
b.Property<int>("CompanyId")
.HasColumnType("integer");
b.Property<bool>("HasBelts")
.HasColumnType("boolean");
b.Property<bool>("HasClimateControl")
.HasColumnType("boolean");
b.Property<bool>("HasOutlet")
.HasColumnType("boolean");
b.Property<bool>("HasStewardess")
.HasColumnType("boolean");
b.Property<bool>("HasTV")
.HasColumnType("boolean");
b.Property<bool>("HasWC")
.HasColumnType("boolean");
b.Property<bool>("HasWiFi")
.HasColumnType("boolean");
b.Property<string>("Number")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("CompanyId");
b.ToTable("Vehicles");
});
modelBuilder.Entity("Server.Models.VehicleEnrollment", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("CancellationComment")
.HasColumnType("text");
b.Property<DateTime>("DepartureDateTimeUtc")
.HasColumnType("timestamp with time zone");
b.Property<int>("RouteId")
.HasColumnType("integer");
b.Property<int>("VehicleId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("RouteId");
b.HasIndex("VehicleId");
b.ToTable("VehicleEnrollments");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Server.Models.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Server.Models.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", 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<string>", 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<string>("UserId")
.HasColumnType("text");
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b1.Property<int>("Id"));
b1.Property<DateTime>("CreationDateTime")
.HasColumnType("timestamp with time zone");
b1.Property<DateTime>("ExpiryDateTime")
.HasColumnType("timestamp with time zone");
b1.Property<DateTime?>("Revoked")
.HasColumnType("timestamp with time zone");
b1.Property<string>("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
}
}
}

View File

@ -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<TimeSpan>(
name: "DelayTimeSpan",
table: "VehicleEnrollments",
type: "interval",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsCanceled",
table: "VehicleEnrollments",
type: "boolean",
nullable: false,
defaultValue: false);
}
}
}

View File

@ -589,15 +589,9 @@ namespace Server.Migrations
b.Property<string>("CancellationComment")
.HasColumnType("text");
b.Property<TimeSpan?>("DelayTimeSpan")
.HasColumnType("interval");
b.Property<DateTime>("DepartureDateTimeUtc")
.HasColumnType("timestamp with time zone");
b.Property<bool>("IsCanceled")
.HasColumnType("boolean");
b.Property<int>("RouteId")
.HasColumnType("integer");

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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> 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<Ticket> Tickets { get; set; } = null!;
public IList<Review> Reviews { get; set; } = null!;
public bool IsCancelled() => CancellationComment == null;
}

View File

@ -134,7 +134,6 @@ services.AddScoped<IDataShaper<ReviewDto>, DataShaper<ReviewDto>>();
services.AddScoped<IDataShaper<CompanyDto>, DataShaper<CompanyDto>>();
services.AddScoped<IDataShaper<VehicleDto>, DataShaper<VehicleDto>>();
services.AddScoped<IDataShaper<VehicleEnrollmentDto>, DataShaper<VehicleEnrollmentDto>>();
services.AddScoped<IDataShaper<VehicleEnrollmentWithDetailsDto>, DataShaper<VehicleEnrollmentWithDetailsDto>>();
services.AddScoped<IDataShaper<RouteDto>, DataShaper<RouteDto>>();
services.AddScoped<IDataShaper<RouteWithAddressesDto>, DataShaper<RouteWithAddressesDto>>();
services.AddScoped<IDataShaper<RouteAddressDto>, DataShaper<RouteAddressDto>>();

View File

@ -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<ExpandoObject> enrollments,
PagingMetadata<ExpandoObject> pagingMetadata)> GetEnrollments(VehicleEnrollmentParameters parameters);
Task<(bool isSucceed, IActionResult? actionResult, IEnumerable<ExpandoObject> enrollments,
PagingMetadata<ExpandoObject> 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<bool> IsEnrollmentExists(int id);
Task<(bool isSucceed, IActionResult actionResult, VehicleEnrollmentDto enrollment)>
AddEnrollment(CreateVehicleEnrollmentDto createEnrollmentDto);
Task<(bool isSucceed, IActionResult actionResult, IEnumerable<ExpandoObject> enrollments, PagingMetadata<ExpandoObject> 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);
}

View File

@ -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<ExpandoObject> _enrollmentSortHelper;
private readonly IDataShaper<VehicleEnrollmentDto> _enrollmentDataShaper;
private readonly IDataShaper<VehicleEnrollmentWithDetailsDto> _enrollmentWithDetailsDataShaper;
private readonly IPager<ExpandoObject> _pager;
private readonly ISessionUserService _sessionUserService;
public VehicleEnrollmentManagementService(ApplicationDbContext dbContext,
IMapper mapper, ISortHelper<ExpandoObject> enrollmentSortHelper,
IDataShaper<VehicleEnrollmentDto> enrollmentDataShaper, IPager<ExpandoObject> pager,
IDataShaper<VehicleEnrollmentWithDetailsDto> enrollmentWithDetailsDataShaper,
ISessionUserService sessionUserService)
public VehicleEnrollmentManagementService(ApplicationDbContext dbContext, IMapper mapper,
ISortHelper<ExpandoObject> enrollmentSortHelper, IDataShaper<VehicleEnrollmentDto> enrollmentDataShaper,
IPager<ExpandoObject> 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<VehicleEnrollment>(createEnrollmentDto);
await _dbContext.VehicleEnrollments.AddAsync(enrollment);
await _dbContext.SaveChangesAsync();
return (true, null, _mapper.Map<VehicleEnrollmentDto>(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<VehicleEnrollmentWithDetailsDto>(enrollment));
return (true, null!, _mapper.Map<VehicleEnrollmentDto>(enrollment));
}
public async Task<(bool isSucceed, IActionResult? actionResult, IEnumerable<ExpandoObject> enrollments,
PagingMetadata<ExpandoObject> 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<VehicleEnrollmentDto>(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<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
int? vehicleId)
{
if (!enrollments.Any() || vehicleId == null)
{
return;
}
enrollments = enrollments.Where(e => e.VehicleId == vehicleId);
}
void FilterByEnrollmentRouteId(ref IQueryable<VehicleEnrollment> enrollments,
int? routeId)
{
if (!enrollments.Any() || routeId == null)
{
return;
}
enrollments = enrollments.Where(e => e.RouteId == routeId);
}
void FilterByEnrollmentDepartureDateTime(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
bool? isCancelled)
{
if (!enrollments.Any() || !isCancelled.HasValue)
{
return;
}
enrollments = enrollments.Where(e => e.IsCanceled == isCancelled);
}
}
public async Task<(bool isSucceed, IActionResult? actionResult, IEnumerable<ExpandoObject> enrollments,
PagingMetadata<ExpandoObject> pagingMetadata)> GetEnrollmentsWithDetails(VehicleEnrollmentWithDetailsParameters parameters)
public async Task<(bool isSucceed, IActionResult actionResult, IEnumerable<ExpandoObject> enrollments, PagingMetadata<ExpandoObject> 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<VehicleEnrollmentWithDetailsDto>(dbEnrollments);
var shapedData = _enrollmentWithDetailsDataShaper.ShapeData(enrollmentDtos, parameters.Fields).AsQueryable();
var enrollmentDtos = _mapper.ProjectTo<VehicleEnrollmentDto>(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<VehicleEnrollment> enrollment,
string? search)
void SearchByAllEnrollmentFields(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
int? vehicleId)
void FilterByEnrollmentVehicleId(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
int? routeId)
void FilterByEnrollmentRouteId(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
DateTime? fromDateTime, DateTime? toDateTime)
void FilterByEnrollmentDepartureDateTime(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
bool? isCancelled)
void FilterByEnrollmentCancelledValue(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
TimeSpan? fromDuration, TimeSpan? toDuration)
void FilterByEnrollmentTotalDuration(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollment> enrollments,
double? fromCost, double? toCost)
void FilterByEnrollmentTotalCost(ref IQueryable<VehicleEnrollment> 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<VehicleEnrollmentDto>(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<VehicleEnrollmentWithDetailsDto>(dbEnrollment);
var shapedData = _enrollmentWithDetailsDataShaper.ShapeData(enrollmentDto, fields);
return (true, null, shapedData);
var enrollmentDto = _mapper.Map<VehicleEnrollmentDto>(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<VehicleEnrollment>(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<VehicleEnrollmentDto>(dbEnrollment));
return (true, null!, _mapper.Map<VehicleEnrollmentDto>(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<bool> IsEnrollmentExists(int id)
private async Task<bool> IsEnrollmentExists(int id)
{
return await _dbContext.VehicleEnrollments.AnyAsync(e => e.Id == id);
}

View File

@ -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<InVehicleEnrollmentReviewDto> Reviews { get; set; } = null!;
public string? CancellationComment { get; set; }
public bool IsCanceled { get; set; }
public string? CancelationComment { get; set; }
[Required]
public new IList<RouteAddressDetailsInVehicleEnrollmentDto> 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<CreateRouteAddressDetailsInVehicleEnrollmentDto> RouteAddressDetails { get; set; } = null!;
}
public class VehicleEnrollmentWithDetailsDto : VehicleEnrollmentDto
{
public IList<RouteAddressDetailsInVehicleEnrollmentDto> RouteAddressDetails { get; set; } = null!;
}
public class UpdateVehicleEnrollmentDto : VehicleEnrollmentDto { }
public class InReviewVehicleEnrollmentDto
{

View File

@ -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; }
}

View File

@ -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; }
}