public static CarPrimitive GetPrimitive(this Car entity) { CarPrimitive primitive = new CarPrimitive(); primitive.Id = entity.Id; primitive.RegistrationNumber = entity.RegistrationNumber; primitive.Name = entity.Name; primitive.InternalName = entity.InternalName; primitive.CarType = entity.CarType; primitive.Deleted = entity.Deleted; primitive.Capacity = entity.Capacity; primitive.TransportType = entity.TransportType; primitive.Driver_Id = entity.Driver_Id; primitive.Deactivated = entity.Deactivated; return primitive; }
public static CarPrimitive GetPrimitiveCopy(this CarPrimitive primitiveToCopy) { if (primitiveToCopy == null) return null; CarPrimitive primitive = new CarPrimitive(); primitive.Id = primitiveToCopy.Id; primitive.RegistrationNumber = primitiveToCopy.RegistrationNumber; primitive.Name = primitiveToCopy.Name; primitive.InternalName = primitiveToCopy.InternalName; primitive.CarType = primitiveToCopy.CarType; primitive.Deleted = primitiveToCopy.Deleted; primitive.Capacity = primitiveToCopy.Capacity; primitive.TransportType = primitiveToCopy.TransportType; primitive.Driver_Id = primitiveToCopy.Driver_Id; primitive.Deactivated = primitiveToCopy.Deactivated; return primitive; }
/// <summary> /// Updates the car.oo /// </summary> /// <param name="carPrimitive">The car primitive.</param> public void CreateOrUpdateCar(CarPrimitive carPrimitive) { try { List<ValidationResult> validationResults = carPrimitive.ValidateClientSide(); if (validationResults != null && validationResults.Count > 0) { throw new FaultException<List<ValidationResult>>(validationResults, "Walidacja obiektu się nie powiodła."); } using (SmartWorkingEntities context = new SmartWorkingEntities()) { Car car = carPrimitive.GetEntity(); Car existingObject = context.Cars.Where(x => x.Id == car.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && car.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new else if (car.Id <= 0) { if (car.ValidateForNewEntity(validationResults, context)) { context.Cars.AddObject(car); } } //Item was retrieved, and the item passed has a valid ID, do an update else { if (car.ValidateForExistingEntity(validationResults, context)) { context.Cars.ApplyCurrentValues(car); } } if (validationResults != null && validationResults.Count > 0) { throw new FaultException<List<ValidationResult>>(validationResults, "Walidacja obiektu się nie powiodła."); } context.SaveChanges(); } } catch(FaultException<List<ValidationResult>>) { throw; } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Deletes the car. /// </summary> /// <param name="carPrimitive">The car primitive.</param> public void DeleteCar(CarPrimitive carPrimitive) { try { //TODO: if is not used in any DeliveryNotes than delete. using (SmartWorkingEntities context = new SmartWorkingEntities()) { Car car = context.Cars.Where(x => x.Id == carPrimitive.Id).FirstOrDefault(); if (car != null) { car.Deleted = DateTime.Now; context.SaveChanges(); } else { throw new Exception("This car does not exist in db."); } } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public CarPrimitive EditCar(IModalDialogService modalDialogService, IServiceFactory serviceFactory, CarPrimitive selectedCar) { var viewModel = new UpdateCarViewModel(modalDialogService, serviceFactory); viewModel.Car = selectedCar; viewModel.DialogMode = DialogMode.Update; ModalDialogHelper<UpdateCar>.ShowDialog(viewModel); return viewModel.Car; }