Exemplo n.º 1
0
        public IHttpActionResult FindRoutes(string apiKey, string startLocationName, string endLocationName, bool recommended, string goods, double weight)
        {
            if (!_securityService.IsAuthenticated(apiKey, true))
            {
                return(Unauthorized());
            }

            var lowerStart = startLocationName.ToLower();
            var lowerEnd   = endLocationName.ToLower();

            var startLocation = _locationService.FindLocation(lowerStart);

            if (startLocation == null)
            {
                return(NotFound());
            }

            var endLocation = _locationService.FindLocation(lowerEnd);

            if (endLocation == null)
            {
                return(NotFound());
            }

            if (weight > 40)
            {
                return(BadRequest("Weights over 40 kg are not supported"));
            }

            var canParse = Enum.TryParse <PackageType>(goods, true, out var packageType);

            if (!canParse)
            {
                return(BadRequest("Unknown type of goods"));
            }
            if (!_allowedPackageTypes.Contains(packageType))
            {
                return(BadRequest("That type of goods is not allowed"));
            }

            var cheapestRoute = _routeService.FindCheapest(startLocation, endLocation, true);
            var fastestRoute  = _routeService.FindFastest(startLocation, endLocation, true);

            var viewRoutes = new[]
            {
                RouteMapper.ViewExternalRouteFromRoute(cheapestRoute, startLocationName, endLocationName, packageType, recommended),
                RouteMapper.ViewExternalRouteFromRoute(fastestRoute, startLocationName, endLocationName, packageType, recommended)
            };

            return(Ok(viewRoutes));
        }