Ejemplo n.º 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)));
        }
        public Aircraft CreateAircraft(AircraftFormCreate aircraftForm)
        {
            AircraftEntity newAircraftEntity = _mapper.Map <AircraftEntity>(aircraftForm);

            newAircraftEntity.Id = Guid.NewGuid();

            _context.Aircrafts.Add(newAircraftEntity);

            var created = _context.SaveChanges();

            if (created < 1)
            {
                throw new InvalidOperationException("Could not create new aircraft.");
            }

            return(_mapper.Map <Aircraft>(newAircraftEntity));
        }