public void AddVehicle(VehicleRegistrationModel vehicleDTO) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <VehicleRegistrationModel, Vehicle>()).CreateMapper(); var vehicle = mapper.Map <VehicleRegistrationModel, Vehicle>(vehicleDTO); repository.Add(vehicle); repository.Save(); }
public ValidationResult ValidateVehicle(VehicleRegistrationModel vehicle) { var result = new ValidationResult(); if (!vehicle.IsTowStrapAvailable) { result.Errors.Add(new PropertyError(nameof(vehicle.IsTowStrapAvailable), "Vehicle must have a tow strap!")); } if (vehicle.Year > DateTime.Now.Year || vehicle.Year < DateTime.Now.Year - Constants.MaxVehicleAgeAllowed) { result.Errors.Add(new PropertyError(nameof(vehicle.Year), "Incorrect value")); } if (vehicle.LiftingHeight < 0 || vehicle.LiftingHeight > 100) { result.Errors.Add(new PropertyError(nameof(vehicle.LiftingHeight), "Incorrect value")); } if (vehicle.TireWear < 0 || vehicle.TireWear > 100) { result.Errors.Add(new PropertyError(nameof(vehicle.TireWear), "Incorrect value")); } if (vehicle.VehicleType == VehicleType.Car) { if (vehicle.TireWear >= 85) { result.Errors.Add(new PropertyError(nameof(vehicle.TireWear), "Tire wear must be less than 85%!")); } } else if (vehicle.VehicleType == VehicleType.Truck) { if (vehicle.LiftingHeight > 5) { result.Errors.Add(new PropertyError(nameof(vehicle.LiftingHeight), "Vehicle lifting more than 5'' is not allowed!")); } } return(result); }