public async Task <BaseResult <TruckViewModel.Response> > InsertTruck(TruckViewModel.Request truck) { try { if (truck.Modelo.ToUpper() != "FH" && truck.Modelo.ToUpper() != "FM") { throw new ArgumentException("Apenas os Valores FH e FM são aceitos no modelo de caminhão"); } var modelo = (await _modelRepo.GetAllAsync()).Single(x => x.Name == truck.Modelo.ToUpper()); var dbObject = _mapper.Map <TruckViewModel.Request, Truck>(truck); dbObject.Model = modelo; await _truckRepo.SaveAsync(dbObject); return(new BaseResult <TruckViewModel.Response> { StatusCode = System.Net.HttpStatusCode.OK, Result = _mapper.Map <Truck, TruckViewModel.Response>(dbObject) }); } catch (Exception ex) { return(new BaseResult <TruckViewModel.Response> { StatusCode = System.Net.HttpStatusCode.BadRequest, Error = ex.Message }); } }
public async Task <BaseResult <TruckViewModel.Response> > UpdateTruck(TruckViewModel.Request truck, int id) { try { if (truck.Modelo.ToUpper() != "FH" && truck.Modelo.ToUpper() != "FM") { throw new ArgumentException("Apenas os Valores FH e FM são aceitos no modelo de caminhão"); } var modelo = (await _modelRepo.GetAllAsync()).Single(x => x.Name == truck.Modelo.ToUpper()); var dbObject = await _truckRepo.GetByIdAsync(id); if (dbObject == null) { return new BaseResult <TruckViewModel.Response> { StatusCode = System.Net.HttpStatusCode.NotFound, Result = _mapper.Map <Truck, TruckViewModel.Response>(dbObject) } } ; dbObject.ManufactureYear = truck.AnoFabricacao; dbObject.ModelYear = truck.AnoModelo; dbObject.Name = truck.Nome; dbObject.Model = modelo; await _truckRepo.UpdateAsync(dbObject); return(new BaseResult <TruckViewModel.Response> { StatusCode = System.Net.HttpStatusCode.OK }); } catch (Exception ex) { return(new BaseResult <TruckViewModel.Response> { StatusCode = System.Net.HttpStatusCode.BadRequest, Error = ex.Message }); } }
public async Task <IActionResult> UpdateTruck([FromRoute] int id, [FromBody] TruckViewModel.Request value) { var data = await _truckService.UpdateTruck(value, id); return(ApplicationResult(data, data.StatusCode)); }
public async Task <IActionResult> InsertTruck([FromBody] TruckViewModel.Request value) { var data = await _truckService.InsertTruck(value); return(ApplicationResult(data, data.StatusCode)); }