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 UniqueInvoiceSpec_CreatingNewInvoice_WhenNotSatisfied_ReturnsFalse() { var invoice = new Invoice { VehicleId = 1 }; var spec = new UniqueInvoiceSpecification(_invoiceRepo); var result = spec.IsSatisfiedBy(invoice); Assert.False(result); }
private bool CheckInvoiceIsUniqueToVehicle(Invoice invoice) { var spec = new UniqueInvoiceSpecification(_unitOfWork.Invoices); return(spec.IsSatisfiedBy(invoice)); }