public static VehicleDb ToVehicleDb(this VehicleBase vehicleBase) { VehicleDb vehicleDb = new VehicleDb() { Id = vehicleBase.Id, Make = vehicleBase.Make, Model = vehicleBase.Model, VehicleType = EVehicleType.Car }; Car car = vehicleBase as Car; if (car != null) { vehicleDb.Engine = car.Engine; vehicleDb.Doors = car.Doors; vehicleDb.Wheels = car.Wheels; vehicleDb.BodyType = car.BodyType; } Boat boat = vehicleBase as Boat; if (boat != null) { vehicleDb.Length = boat.Length; vehicleDb.Name = boat.Name; vehicleDb.VehicleType = EVehicleType.Boat; } return(vehicleDb); }
public bool UpdateVehicle(VehicleDb Vehicle) { _context.Entry(Vehicle).State = EntityState.Modified; try { _context.SaveChanges(); return(true); } catch (DbUpdateConcurrencyException e) { if (_context.Vehicles.Any(v => v.Id == Vehicle.Id)) { return(false); } else { throw new VehicleNotExistException(e); } } catch (Exception ex) { throw new DBOperationException(ex); } }
public static VehicleDb ConvertFrom(this AccidentStatisticDb accidentStatistic, Vehicle vehicle) { var result = new VehicleDb { AccidentStatistic = accidentStatistic, VehicleType = vehicle.VehicleType }; return(result); }
public void CreateVehicle(VehicleDb vehicle) { _context.Add(vehicle); try { _context.SaveChanges(); } catch (Exception ex) { throw new DBOperationException(ex); } }
public bool UpdateBoatById(Boat boat, int id) { //VehicleDb vehicle = _vehicleRepository.GetAllVehicles().FirstOrDefault(vehicle => vehicle.Id == id) //if (vehicle.VehicleType != EVehicleType.Boat) //{ // throw new Exception("error occured when try to add vehicle to database."); //} VehicleDb vehicle = boat.ToVehicleDb(); return(_vehicleRepository.UpdateVehicle(vehicle)); }
public void DeleteVehicleById(int id) { var vehicle = new VehicleDb() { Id = id }; _context.Remove(vehicle); try { _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { return; } }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { VehicleDb db = new VehicleDb(); List <Vehicle> model; Vehicle vehicle = (Vehicle)validationContext.ObjectInstance; model = db.Vehicle.Where(i => i.Reg == value.ToString() && i.Id != vehicle.Id).ToList(); if (model.Count != 0) { model.Clear(); return(new ValidationResult("the registration Number is in used ... try another")); } else { model.Clear(); return(ValidationResult.Success); } }
public static VehicleBase ToVehicleBase(this VehicleDb vehicleDb) { switch (vehicleDb.VehicleType) { case EVehicleType.Car: Car car = new Car(vehicleDb.Make, vehicleDb.Model, vehicleDb.Engine, vehicleDb.Doors, vehicleDb.Wheels, vehicleDb.BodyType) { Id = vehicleDb.Id }; return(car); case EVehicleType.Boat: Boat boat = new Boat(vehicleDb.Make, vehicleDb.Model, vehicleDb.Length, vehicleDb.Name) { Id = vehicleDb.Id }; return(boat); default: throw new Exception("inefficient vehicle type"); } }
public VehicleBaseBusiness() { vehicleDb = new VehicleDb(); vehicleMakerDb = new VehicleMakerDb(); vehicleModelDb = new VehicleModelDb(); }