示例#1
0
        public IActionResult CreateAircraft([FromBody] AircraftFormCreate newAircraftForm)
        {
            if (newAircraftForm == null)
            {
                return(BadRequest());
            }

            // ASP.NET does validation automatically

            // Do some business rules check here, i.e. Check if Aircraft is over 15 years old
            // If Aircraft doesnt meet requirement, return BadRequest

            /* Example:
             * var maxAge = 15;
             * bool tooOld = aircraft.Age > 15
             * if (tooOld) return BadRequest(new ApiError($"The max age is 15 years"));
             */

            Aircraft newCreatedAircraft = _aircraftService.CreateAircraft(newAircraftForm);

            return(CreatedAtRoute(
                       routeName: nameof(GetAircraftById),
                       routeValues: new { aircraftId = newCreatedAircraft.Id },
                       value: CreateLinks(newCreatedAircraft)));
        }