Skip to content

Commit

Permalink
added stops distance sql functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyBorkovich committed Apr 30, 2024
1 parent 670928c commit 7adfd48
Show file tree
Hide file tree
Showing 20 changed files with 1,965 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface IGenericRepository<T> where T: class
IQueryable<T> GetAll();
Task<T?> GetByIdAsync(int id);
Task InsertAsync(T entity);
Task BulkInsertAsync(IEnumerable<T> entitites);
Task UpdateAsync(T entity);
Task DeleteAsync(T entity);
Task<bool> AnyAsync(Expression<Func<T, bool>> conditions);
Expand Down
2 changes: 2 additions & 0 deletions src/BSMS.Application/Contracts/Persistence/IStopRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ namespace BSMS.Application.Contracts.Persistence;
public interface IStopRepository : IGenericRepository<Stop>
{
bool StopsBelongToSameRoute(int firstStopId, int secondStopId);

void ExecuteCustomizedInsert(int routeId, string stopName, int distanceToPrevious);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public record CreateRouteCommand(
string Destination,
List<CreateStops> StopsList) : IRequest<MethodResult<CreatedEntityResponse>>;

// only intermediate stops since first and last is generated by SQL trigger
public record CreateStops (
string Name);
string Name,
int DistanceToPrevious);
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.Net;
using BSMS.Application.Contracts;
using BSMS.Application.Contracts.Persistence;
using BSMS.Application.Extensions;
using BSMS.Application.Features.Common;
using BSMS.Application.Helpers;
using BSMS.Core.Entities;
using FluentValidation;
using MapsterMapper;
using MediatR;

namespace BSMS.Application.Features.Route.Commands.Create;

public class CreateRouteCommandHandler(
IRouteRepository repository,
IRouteRepository routeRepository,
IStopRepository stopRepository,
IMapper mapper,
IValidator<CreateRouteCommand> validator,
MethodResultFactory methodResultFactory)
Expand All @@ -28,9 +29,12 @@ public async Task<MethodResult<CreatedEntityResponse>> Handle(CreateRouteCommand
return result;
}

// insert separately to avoid concurrency exception caused by SQL trigger
var route = mapper.Map<Core.Entities.Route>(request);

await repository.InsertAsync(route);
await routeRepository.InsertAsync(route);

var stops = mapper.Map<List<Stop>>(request.StopsList);
stops.ForEach(s => stopRepository.ExecuteCustomizedInsert(route.RouteId, s.Name, s.DistanceToPrevious.Value));

result.Data = new CreatedEntityResponse(route.RouteId);

Expand Down
3 changes: 0 additions & 3 deletions src/BSMS.Application/MappingProfiles/RouteProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ public class RouteProfile : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.NewConfig<CreateRouteCommand, Route>()
.Map(dest => dest.Stops, src => src.StopsList);

config.NewConfig<Route, GetAllRoutesResponse>()
.Map(dest => dest.Name, src => $"{src.Origin} - {src.Destination}");
}
Expand Down
2 changes: 2 additions & 0 deletions src/BSMS.Core/Entities/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Route
[StringLength(50)]
public string Destination { get; set; } = null!;

public int OverallDistance { get; set; }

public List<Stop> Stops { get; set; }
public List<BusScheduleEntry> BusScheduleEntries { get; set; }
}
1 change: 1 addition & 0 deletions src/BSMS.Core/Entities/Stop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Stop

[StringLength(50)]
public string Name { get; set; }
public int? DistanceToPrevious { get; set; }

public Stop? PreviousStop { get; set; }
public Route Route { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/BSMS.Core/Enums/TicketStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public enum TicketStatus
Active = 100,
InUse = 200,
Used = 300,
Canceled = 400
Cancelled = 400
}
1 change: 1 addition & 0 deletions src/BSMS.Infrastructure/BSMS.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.4" />
<PackageReference Include="StackExchange.Redis" Version="2.7.33" />
<PackageReference Include="EntityFrameworkCore.Triggered" Version="3.2.2" />
<PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.102.2.3" />
</ItemGroup>

<ItemGroup>
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 @@ -109,5 +109,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
typeof(BusStationContext).GetMethod(nameof(StopsBelongToSameRoute),
[typeof(int), typeof(int)])!)
.HasName("StopsBelongToSameRoute");

modelBuilder.Entity<Route>()
.Property(r => r.OverallDistance)
.HasComputedColumnSql("dbo.CalculateTotalDistanceForRoute([RouteId])");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @NewStatus NVARCHAR(20))
DECLARE @IsFree BIT;
-- check new status
IF @NewStatus = 'Active' OR @NewStatus = 'Canceled' OR @NewStatus = 'Expired'
IF @NewStatus = 'Active' OR @NewStatus = 'Cancelled'
SET @IsFree = 1;
ELSE
SET @IsFree = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ WHILE @CurrentStopId IS NOT NULL
-- Update the last stop ID
SET @LastStopId = @CurrentStopId;
-- Get the next stop ID
SET @NextStopId = dbo.FindNextStop(@CurrentStopId, @RouteId);
SET @NextStopId = dbo.FindNextStop(@RouteId, @CurrentStopId);
-- Update the current stop ID
SET @CurrentStopId = @NextStopId;
END;
Expand Down
Loading

0 comments on commit 7adfd48

Please sign in to comment.