/// <summary> /// Get Vehicle by id /// </summary> /// <param name="id">Vehicle id</param> /// <returns>Vehicle json view model</returns> public IHttpActionResult Get(int id) { try { // get log.Debug("_vehicleService.GetVehicle - vehicleId: " + id + " "); var vehicle = new VehicleViewModel(_vehicleService.GetVehicle(id)); log.Debug("_vehicleService.GetVehicle - " + VehicleViewModel.FormatVehicleViewModel(vehicle)); log.Debug("result: 'success'"); //return Json(vehicle, JsonRequestBehavior.AllowGet); //return Content(JsonConvert.SerializeObject(vehicle), "application/json"); //return vehicle; //return JsonConvert.SerializeObject(vehicle); return(Ok(vehicle)); } catch (Exception e) { // error log.Error(e.ToString()); throw; } }
private VehicleDTO Create(VehicleViewModel viewModel) { try { log.Debug(VehicleViewModel.FormatVehicleViewModel(viewModel)); VehicleDTO vehicle = new VehicleDTO(); // copy values viewModel.UpdateDTO(vehicle, null); //RequestContext.Principal.Identity.GetUserId()); // audit vehicle.CreateBy = null; //RequestContext.Principal.Identity.GetUserId(); vehicle.CreateOn = DateTime.UtcNow; // add log.Debug("_vehicleService.AddVehicle - " + VehicleDTO.FormatVehicleDTO(vehicle)); int id = _vehicleService.AddVehicle(vehicle); vehicle.VehicleId = id; log.Debug("result: 'success', id: " + id); return(vehicle); } catch (Exception e) { // error log.Error(e.ToString()); throw; } }
private VehicleDTO Update(VehicleViewModel viewModel) { try { log.Debug(VehicleViewModel.FormatVehicleViewModel(viewModel)); // get log.Debug("_vehicleService.GetVehicle - vehicleId: " + viewModel.VehicleId + " "); var existingVehicle = _vehicleService.GetVehicle(viewModel.VehicleId); log.Debug("_vehicleService.GetVehicle - " + VehicleDTO.FormatVehicleDTO(existingVehicle)); if (existingVehicle != null) { // copy values viewModel.UpdateDTO(existingVehicle, null); //RequestContext.Principal.Identity.GetUserId()); // update log.Debug("_vehicleService.UpdateVehicle - " + VehicleDTO.FormatVehicleDTO(existingVehicle)); _vehicleService.UpdateVehicle(existingVehicle); log.Debug("result: 'success'"); } else { log.Error("existingVehicle: null, VehicleId: " + viewModel.VehicleId); } return(existingVehicle); } catch (Exception e) { // error log.Error(e.ToString()); throw; } }
//[ValidateAntiForgeryToken] /// <summary> /// Save a list of Vehicle /// </summary> /// <param name="viewModels">Vehicle view models</param> /// <param name="id">(not used)</param> /// <returns>true if the operation is successfull</returns> public IHttpActionResult SaveList(VehicleViewModel[] viewModels, int?id) { try { log.Debug("SaveList"); if (viewModels != null) { // save list foreach (VehicleViewModel viewModel in viewModels) { log.Debug(VehicleViewModel.FormatVehicleViewModel(viewModel)); if (viewModel.VehicleId > 0) { var t = Update(viewModel); } else { var t = Create(viewModel); } } } else { log.Error("viewModels: null"); } //return Json(true); //return JsonConvert.SerializeObject(true); return(Ok(true)); } catch (Exception e) { // error log.Error(e.ToString()); throw; } }