Esempio n. 1
0
        public async Task <IActionResult> CreateVehicleAsync([FromBody] SaveVehicleResource saveVehicle)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!await ValidateVehicleAsync(saveVehicle))
            {
                return(BadRequest(ModelState));
            }

            try {
                Vehicle domainVehicle = _mapper.Map <SaveVehicleResource, Vehicle>(saveVehicle);
                domainVehicle.LastUpdate = DateTime.Now;
                await _vehiclesRepository.CreateAsync(domainVehicle);

                await _unitOfWork.CompeleteAsync();

                int id = domainVehicle.Id;
                domainVehicle.LastUpdate = DateTime.Now;
                domainVehicle            = await _vehiclesRepository.GetWithDependenciesAsync(id);

                VehicleResource result = _mapper.Map <Vehicle, VehicleResource>(domainVehicle);
                return(Ok(result));
            } catch (Exception e) {
                return(StatusCode(500, e.InnerException?.Message ?? e.Message));
            }
        }