Skip to content

Commit

Permalink
make driver-company relation strict, done refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyBorkovich committed Apr 9, 2024
1 parent dc60d35 commit 08263e6
Show file tree
Hide file tree
Showing 16 changed files with 739 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/BSMS.API/Controllers/BusController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BusController(ISender sender) : ControllerBase
/// <param name="command">Bus data and its schedule data</param>
/// <returns>ID of the created bus</returns>
[HttpPost("Create")]
public async Task<ActionResult<int>> Create(CreateBusCommand command)
public async Task<ActionResult<CreatedEntityResponse>> Create(CreateBusCommand command)
{
var result = await sender.Send(command);

Expand Down
3 changes: 2 additions & 1 deletion src/BSMS.API/Controllers/BusReviewController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BSMS.API.Extensions;
using BSMS.Application.Features.BusReview.Commands.Create;
using BSMS.Application.Features.Common;
using MediatR;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -16,7 +17,7 @@ public class BusReviewController(ISender sender) : ControllerBase
/// <param name="command">Bus and passenger IDs, marks data</param>
/// <returns>ID of the created review</returns>
[HttpPost("Create")]
public async Task<ActionResult<int>> Create(CreateBusReviewCommand command)
public async Task<ActionResult<CreatedEntityResponse>> Create(CreateBusReviewCommand command)
{
var result = await sender.Send(command);

Expand Down
2 changes: 1 addition & 1 deletion src/BSMS.API/Controllers/CompanyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CompanyController(ISender sender) : ControllerBase
/// <param name="command">Company's data</param>
/// <returns>ID of the created company</returns>
[HttpPost("Create")]
public async Task<ActionResult<int>> Create(CreateCompanyCommand command)
public async Task<ActionResult<CreatedEntityResponse>> Create(CreateCompanyCommand command)
{
var result = await sender.Send(command);

Expand Down
2 changes: 1 addition & 1 deletion src/BSMS.API/Controllers/DriverController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DriverController(ISender sender) : ControllerBase
/// <param name="command">Driver data</param>
/// <returns>ID of the created driver</returns>
[HttpPost("Create")]
public async Task<ActionResult<int>> Create(CreateDriverCommand command)
public async Task<ActionResult<CreatedEntityResponse>> Create(CreateDriverCommand command)
{
var result = await sender.Send(command);

Expand Down
2 changes: 1 addition & 1 deletion src/BSMS.API/Controllers/PassengerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PassengerController(ISender sender) : ControllerBase
/// <param name="command">Passenger's data</param>
/// <returns>ID of the created passenger</returns>
[HttpPost("Create")]
public async Task<ActionResult<int>> Create(CreatePassengerCommand command)
public async Task<ActionResult<CreatedEntityResponse>> Create(CreatePassengerCommand command)
{
var result = await sender.Send(command);

Expand Down
2 changes: 1 addition & 1 deletion src/BSMS.API/Controllers/RouteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RouteController(ISender sender) : ControllerBase
/// <param name="command">Contains origin and destination of route and list of it's stops names</param>
/// <returns>ID of the created route</returns>
[HttpPost("Create")]
public async Task<ActionResult<int>> Create(CreateRouteCommand command)
public async Task<ActionResult<CreatedEntityResponse>> Create(CreateRouteCommand command)
{
var result = await sender.Send(command);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record CreateBusCommand(
string Brand,
int Capacity,
string Number,
int? DriverId,
int DriverId,
List<CreateBusSchedule> BusScheduleEntries) : IRequest<MethodResult<CreatedEntityResponse>>;

public record CreateBusSchedule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,9 @@ private bool HaveNoTimeIntersections(List<CreateBusSchedule> newBusSchedules)
return !enumerable.Exists(group => HasTimeIntersectionsInGroup(group.ToList()));
}

private async Task<bool> DriverExists(int? driverId, CancellationToken cancellationToken)
private async Task<bool> DriverExists(int driverId, CancellationToken cancellationToken)
{
if (driverId is not null)
{
return await _driverRepository.AnyAsync(d => d.DriverId == driverId);
}

return true;
return await _driverRepository.AnyAsync(d => d.DriverId == driverId);
}

private bool HasTimeIntersectionsInGroup(List<CreateBusSchedule> schedules)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public record CreateDriverCommand(
string FirstName,
string LastName,
string DriverLicense,
int? CompanyId,
int? BusId) : IRequest<MethodResult<CreatedEntityResponse>>;
int CompanyId) : IRequest<MethodResult<CreatedEntityResponse>>;

public class CreateDriverCommandHandler(
IDriverRepository repository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public CreateDriverCommandValidator(ICompanyRepository companyRepository)
.WithMessage("Chosen company must exist!");
}

private Task<bool> CompanyExists(int? companyId, CancellationToken cancellationToken)
private Task<bool> CompanyExists(int companyId, CancellationToken cancellationToken)
{
return companyId is not null ? _companyRepository.AnyAsync(c => c.CompanyId == companyId) : Task.FromResult(true);
return _companyRepository.AnyAsync(c => c.CompanyId == companyId);
}
}
2 changes: 1 addition & 1 deletion src/BSMS.Core/Entities/Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class Bus
/// </summary>
public List<BusScheduleEntry> BusScheduleEntries { get; set; }

Check warning on line 18 in src/BSMS.Core/Entities/Bus.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'BusScheduleEntries' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public List<BusReview> BusReviews { get; set; }

Check warning on line 19 in src/BSMS.Core/Entities/Bus.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'BusReviews' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public Driver? Driver { get; set; }
public Driver Driver { get; set; }

Check warning on line 20 in src/BSMS.Core/Entities/Bus.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Driver' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public List<Seat> Seats { get; set; }

Check warning on line 21 in src/BSMS.Core/Entities/Bus.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Seats' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
2 changes: 1 addition & 1 deletion src/BSMS.Core/Entities/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace BSMS.Core.Entities;
public class Driver
{
public int DriverId { get; set; }
public int? CompanyId { get; set; }
public int CompanyId { get; set; }

[StringLength(50)]
public string FirstName { get; set; } = null!;
Expand Down
4 changes: 4 additions & 0 deletions src/BSMS.Infrastructure/Persistence/BusStationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public BusStationContext(DbContextOptions<BusStationContext> options)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// prevent from SaveChanges() exception caused by trigger
modelBuilder.Entity<Bus>()
.ToTable(tb => tb.UseSqlOutputClause(false));

modelBuilder.Entity<Stop>()
.HasMany(s => s.TicketStartStops)
.WithOne(t => t.StartStop)
Expand Down
Loading

0 comments on commit 08263e6

Please sign in to comment.