Exemplo n.º 1
0
        public async Task <IActionResult> UpdateVehicle(int id, [FromBody] VehicleResource vehicleResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var vehicle = await repository.GetVehicle(id);

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

            vehicle            = mapper.Map <VehicleResource, Vehicle>(vehicleResource);
            vehicle.LastUpdate = DateTime.Now;

            await unitOfWork.CompleteAsync();

            vehicle = await repository.GetVehicle(vehicle.Id);

            var result = mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
Exemplo n.º 2
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));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UpdateVehicleAsync(int id, [FromBody] SaveVehicleResource saveVehicle)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //We need to load features to calculate existing features
            var domainVehicle = await _vehiclesRepository.GetWithDependenciesAsync(id);

            if (domainVehicle == null)
            {
                ModelState.AddModelError("Id", $"Vehicle with Id = {id} not found!");
                return(BadRequest(ModelState));
            }

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

            try {
                _mapper.Map(saveVehicle, domainVehicle);
                // ReSharper disable once PossibleNullReferenceException
                domainVehicle.LastUpdate = DateTime.Now;
                await _unitOfWork.CompeleteAsync();

                domainVehicle = await _vehiclesRepository.GetWithDependenciesAsync(id);

                VehicleResource result = _mapper.Map <Vehicle, VehicleResource>(domainVehicle);
                result.Id = id;
                return(Ok(result));
            } catch (Exception e) {
                return(StatusCode(500, e.InnerException?.Message ?? e.Message));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var model = await context.Models.FindAsync(vehicleResource.ModelId);

            if (model == null)
            {
                ModelState.AddModelError("ModelId", "Invalid modelId.");
                return(BadRequest(ModelState));
            }


            var vehicle = mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            vehicle.LastUpdate = DateTime.Now;
            context.Vehicles.Add(vehicle);
            await context.SaveChangesAsync();

            var result = mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            var vehicle = mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            context.Vehicles.Add(vehicle);
            await context.SaveChangesAsync();

            var result = mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> GetVehicleAsync(int id)
        {
            Vehicle domainVehicle = await _vehiclesRepository.GetWithDependenciesAsync(id);

            if (domainVehicle == null)
            {
                return(NotFound(id));
            }

            VehicleResource result = _mapper.Map <VehicleResource>(domainVehicle);

            return(Ok(result));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> CreateVehicle
            ([FromBody] VehicleResource vehicleResource)
        {
            throw new Exception();
            if (!ModelState.IsValid)
            {
                return(BadRequest(null));
            }

            var vehicle    = mapper.Map <VehicleResource, Vehicle>(vehicleResource);
            var retvehicle = await repository.AddVehicle(vehicle);

            return(Ok(mapper.Map <Vehicle, VehicleResource>(retvehicle)));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var vehicle = _mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            vehicle.LastUpdate = DateTime.Now;
            context.Vehicles.Add(vehicle);
            await context.SaveChangesAsync();

            var result = Mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> UpdateVehicleAsync(int id, [FromBody] VehicleResource vehicleResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var vehicle = await context.Vehicles.Include(v => v.Features).SingleOrDefaultAsync(v => v.Id == id);

            mapper.Map <VehicleResource, Vehicle>(vehicleResource);
            vehicle.LastUpdate = DateTime.Now;
            await context.SaveChangesAsync();

            var result = mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            // map api resource to domain object
            var vehicle = mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            vehicle.LastUpdate = DateTime.Now;
            // add to context and save the changes
            context.Vehicles.Add(vehicle);
            await context.SaveChangesAsync();   // SaveChangesAsync has caused a loop on domain model

            // add a new mapping on MappingProfile that do mapping reversal
            // after save the changes we going to map the vehicle object back to vehicleResource
            // this will prevent Self referencing loop errors
            var result = mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
Exemplo n.º 11
0
        public IActionResult CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            var vehicle = mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            return(Ok(vehicle));
        }