public Invoice SaveInvoice(Invoice invoice) { var vehicle = _unitOfWork.Vehicles.GetById(invoice.VehicleId); var vehicleExistsSpec = new VehicleExistsSpecification(); if (!vehicleExistsSpec.IsSatisfiedBy(vehicle)) { throw new VehicleNotFoundException(invoice.VehicleId); } if (invoice.Id == 0 || invoice.VehicleId != vehicle.Id) { var uniqueInvoiceSpec = new UniqueInvoiceSpecification(_unitOfWork.Invoices); if (!uniqueInvoiceSpec.IsSatisfiedBy(invoice)) { throw new InvoiceAlreadyExistsForVehicleException(invoice.VehicleId); } } SetVehicleToSoldStatus(vehicle); if (invoice.Id == 0) { invoice.InvoiceNumber = GenerateInvoiceNumber(invoice.VehicleId); _unitOfWork.Invoices.Add(invoice); } _unitOfWork.SaveChanges(); return(invoice); }
public void VehicleExistsSpecificationSpec_WhenNotSatisfied_ReturnsFalse() { Vehicle vehicle = null; var spec = new VehicleExistsSpecification(); var result = spec.IsSatisfiedBy(vehicle); Assert.False(result); }
public void VehicleExistsSpecificationSpec_WhenSatisfied_ReturnsTrue() { var vehicle = new Vehicle { Id = 1, }; var spec = new VehicleExistsSpecification(); var result = spec.IsSatisfiedBy(vehicle); Assert.True(result); }
public Photo SavePhoto(Photo photo, bool isMain = false) { var vehicle = _unitOfWork.Vehicles.GetById(photo.VehicleId); var spec = new VehicleExistsSpecification(); if (!spec.IsSatisfiedBy(vehicle)) { throw new VehicleNotFoundException(photo.VehicleId); } if (photo.IsMain || isMain) { var mainPhoto = _unitOfWork.Photos.GetVehicleMainPhoto(photo.VehicleId); if (mainPhoto != null) { mainPhoto.IsMain = false; } } if (!CheckCurrentMainPhotoExists(photo.VehicleId)) { photo.IsMain = true; } if (photo.Id == 0) { _unitOfWork.Photos.Add(photo); } _unitOfWork.SaveChanges(); return(photo); }