コード例 #1
0
        // GET: Staff
        /// <summary>
        /// Vehicle List page shows all the vehicles and highlights any with open invesitgations
        /// </summary>
        /// <param name="indexModel">Model that contains the filter params and list of vehicles</param>
        /// <returns></returns>
        //Takes indexModel which contains the filtering options and applies it to the list of vehicles to be shown to the user.
        public async Task <IActionResult> Index([FromQuery] VehIndexModel indexModel = null)
        {
            //Set the DD lists as null to avoid errors
            IEnumerable <VehicleModel> vehicles       = null;
            IEnumerable <VehicleModel> vehicleConcern = null;
            IEnumerable <MakeModel>    makeList       = null;
            IEnumerable <string>       stateList      = null;

            try
            {
                //Create model based on the passed values. At first will be null so everything set to default.
                //Once filter submitted, these will have values to filter the vehicles
                VehFilterAPI apiModel = VehFilterAPI.CreateModel(indexModel.selectedState, indexModel.selectedMake, indexModel.lPlateSearch);
                //Call API to get filtered vehicles
                IEnumerable <VehicleDTO> vehicleDTOs = await _beService.GetVehiclesAsync(apiModel);

                vehicles = VehicleModel.CreateModelNumerable(vehicleDTOs);

                //Create seperate list for vehicles with damage concerns so can display them at the top
                vehicleConcern = vehicles.Where(x => x.state == "Under Investigation").ToList();
                //Drop those in the concern list to avoid duplicates
                vehicles = vehicles.Except(vehicleConcern);

                //Call API to get Make lists
                makeList = MakeModel.CreateModelNumerable(await _beService.GetMakesAsync());
                //Create model based off the new lists e.g.
                indexModel = VehIndexModel.CreateModel(vehicles, vehicleConcern, makeList, indexModel.selectedMake, indexModel.selectedState, indexModel.lPlateSearch);
            }
            catch
            {
                vehicles  = Array.Empty <VehicleModel>();
                makeList  = Array.Empty <MakeModel>();
                stateList = Array.Empty <string>();

                indexModel = VehIndexModel.CreateModel(null, null, null, Guid.Empty, null, null);
            }
            //Return view with model
            return(View("VehicleList", indexModel));
        }