public async Task UpdateAsync(RouteBase routeBaseUpdate) { // Error if route base does not exist var routeBase = await ValidateRouteBaseExistsAsync(routeBaseUpdate.Id); var routeBaseControl = routeBase; // Error if origin and destination are the same point PointsValidation.ValidateEqualPointsAsync(routeBaseUpdate.Origin?.Description, routeBaseUpdate.Destination?.Description); if (!string.IsNullOrWhiteSpace(routeBaseUpdate.Origin?.Description)) { // Error if point description does not exist var origin = await PointsValidation.ValidatePointExistsAsync(_pointRepository, routeBaseUpdate.Origin.Description); routeBase.Origin = origin; } if (!string.IsNullOrWhiteSpace(routeBaseUpdate.Destination?.Description)) { // Error if point description does not exist var destination = await PointsValidation.ValidatePointExistsAsync(_pointRepository, routeBaseUpdate.Destination.Description); routeBase.Destination = destination; } // Error if route base with such points already exists await ValidateRouteBaseAlreadyExistsAsync(routeBase); await _routeBaseRepository.UpdateAsync(routeBase); RemoveDependentObjectsFromCache(routeBaseControl); }
public async Task <RouteBase> CreateAsync(RouteBase routeBase) { // Error if origin and destination are the same point PointsValidation.ValidateEqualPointsAsync(routeBase.Origin?.Description, routeBase.Destination?.Description); // Error if point description does not exist var origin = await PointsValidation.ValidatePointExistsAsync(_pointRepository, routeBase.Origin?.Description); var destination = await PointsValidation.ValidatePointExistsAsync(_pointRepository, routeBase.Destination?.Description); var routeBaseToAdd = new RouteBase { Origin = origin, Destination = destination }; // Error if route base with such points already exists await ValidateRouteBaseAlreadyExistsAsync(routeBaseToAdd); await _routeBaseRepository.AddAsync(routeBaseToAdd); var routeBases = await _routeBaseRepository.GetAsync(); var routeReturn = routeBases.FirstOrDefault(x => (x.Origin?.Description?.Equals(routeBaseToAdd.Origin.Description) == true || x.OriginId == routeBaseToAdd.OriginId) && (x.Destination?.Description?.Equals(routeBaseToAdd.Destination.Description) == true || x.DestinationId == routeBaseToAdd.DestinationId)); return(routeReturn); }
public async Task <Step> CreateAsync(Step step) { // Error if start and end are the same point PointsValidation.ValidateEqualPointsAsync(step.Start?.Description, step.End?.Description); // Error if point description does not exist var start = await PointsValidation.ValidatePointExistsAsync(_pointRepository, step.Start?.Description); var end = await PointsValidation.ValidatePointExistsAsync(_pointRepository, step.End?.Description); var stepToAdd = new Step { Start = start, End = end }; // Error if step with such points already exists await ValidateStepAlreadyExistsAsync(stepToAdd); // Error if time/cost is zero or less ValidatePositiveTimeCost(step.Cost); ValidatePositiveTimeCost(step.Time); stepToAdd.Cost = step.Cost; stepToAdd.Time = step.Time; await _stepRepository.AddAsync(stepToAdd); _cacheManager.RemoveBulkWithExpression(CacheKeys.Route); return((await _stepRepository.GetAsync()).FirstOrDefault(x => (x.Start?.Description?.Equals(stepToAdd.Start.Description) == true || x.StartId == stepToAdd.Start.Id) && (x.End?.Description?.Equals(stepToAdd.End.Description) == true || x.EndId == stepToAdd.End.Id))); }
/// <summary>Calculates a route base for a given origin and destination.</summary> /// <param name="originPoint">The origin point.</param> /// <param name="destinationPoint">The destination point.</param> /// <returns>The route base.</returns> private async Task <RouteBase> CalculateRouteBaseAsync(string originPoint, string destinationPoint) { // Error if origin and destination are the same point PointsValidation.ValidateEqualPointsAsync(originPoint, destinationPoint); // Error if point description does not exist var origin = await PointsValidation.ValidatePointExistsAsync(_pointRepository, originPoint); var destination = await PointsValidation.ValidatePointExistsAsync(_pointRepository, destinationPoint); var routeBase = new RouteBase { Origin = origin, Destination = destination }; // Error if route base with such points does not exist await ValidateRouteBaseExistsAsync(routeBase); // Error if there is not any step to begin the route, or if there is not any step to finish the route await ValidateStepStartExistsAsync(origin); await ValidateStepEndExistsAsync(destination); return(routeBase); }
public async Task UpdateAsync(Step stepUpdate) { var step = await ValidateStepExistsAsync(stepUpdate.Id); // Error if start and end are the same point PointsValidation.ValidateEqualPointsAsync(stepUpdate.Start?.Description, stepUpdate.End?.Description); if (!string.IsNullOrWhiteSpace(stepUpdate.Start?.Description)) { // Error if point description does not exist var start = await PointsValidation.ValidatePointExistsAsync(_pointRepository, stepUpdate.Start.Description); step.Start = start; } if (!string.IsNullOrWhiteSpace(stepUpdate.End?.Description)) { // Error if point description does not exist var end = await PointsValidation.ValidatePointExistsAsync(_pointRepository, stepUpdate.End.Description); step.End = end; } // Error if step with such points already exists await ValidateStepAlreadyExistsAsync(step); if (stepUpdate.Cost > 0) { step.Cost = stepUpdate.Cost; } if (stepUpdate.Time > 0) { step.Time = stepUpdate.Time; } await _stepRepository.UpdateAsync(step); _cacheManager.RemoveBulkWithExpression(CacheKeys.Route); }