public async Task ContractRentableItem_Delete_DoesDelete() { //Arrange RentableItem rentableItem = new RentableItem { Brand = Guid.NewGuid().ToString(), ModelName = Guid.NewGuid().ToString(), VinNumber = Guid.NewGuid().ToString(), SerialNumber = Guid.NewGuid().ToString(), PlateNumber = Guid.NewGuid().ToString(), RegistrationDate = DateTime.Now }; await rentableItem.Create(DbContext); Random rnd = new Random(); DateTime contractEnd = DateTime.Now; DateTime contractStart = DateTime.Now; Contract contract = new Contract { Status = 66, ContractEnd = contractEnd, ContractNr = rnd.Next(1, 255), ContractStart = contractStart, CustomerId = rnd.Next(1, 255) }; await contract.Create(DbContext); ContractRentableItem contractRentableItem = new ContractRentableItem { ContractId = contract.Id, RentableItemId = rentableItem.Id }; await contractRentableItem.Create(DbContext); //Act await contractRentableItem.Delete(DbContext); ContractRentableItem dbContractRentableItem = DbContext.ContractRentableItem.AsNoTracking().First(); List <ContractRentableItem> contractRentableItems = DbContext.ContractRentableItem.AsNoTracking().ToList(); List <ContractRentableItemVersion> versionList = DbContext.ContractRentableItemVersion.AsNoTracking().ToList(); //Assert Assert.NotNull(dbContractRentableItem); Assert.AreEqual(1, contractRentableItems.Count); Assert.AreEqual(2, versionList.Count); Assert.AreEqual(contractRentableItem.ContractId, dbContractRentableItem.ContractId); Assert.AreEqual(contractRentableItem.RentableItemId, dbContractRentableItem.RentableItemId); Assert.AreEqual(Constants.WorkflowStates.Removed, dbContractRentableItem.WorkflowState); }
public async Task <OperationResult> Update(ContractModel updateModel) { try { Contract contract = await _dbContext.Contract.SingleOrDefaultAsync(x => x.Id == updateModel.Id); if (contract != null) { contract.ContractEnd = updateModel.ContractEnd; contract.ContractNr = updateModel.ContractNr; contract.ContractStart = updateModel.ContractStart; contract.CustomerId = updateModel.CustomerId; await contract.Update(_dbContext); } if (updateModel.DeleteIds.Count > 0) { Contract dbContract = await _dbContext.Contract.FirstOrDefaultAsync(x => x.ContractNr == updateModel.ContractNr); foreach (var rentableItemId in updateModel.DeleteIds) { ContractRentableItem deleteContractRentableItem = await _dbContext.ContractRentableItem.FirstOrDefaultAsync(x => x.ContractId == dbContract.Id && x.RentableItemId == rentableItemId); await deleteContractRentableItem.Delete(_dbContext); } } foreach (var rentableItemId in updateModel.RentableItemIds) { Contract dbContract = await _dbContext.Contract.FirstOrDefaultAsync(x => x.ContractNr == updateModel.ContractNr); ContractRentableItem contractRentableItem = await _dbContext.ContractRentableItem.FirstOrDefaultAsync(x => x.ContractId == dbContract.Id && x.RentableItemId == rentableItemId); ContractRentableItem checkContractRentableItem = await _dbContext.ContractRentableItem.FirstOrDefaultAsync( x => x.ContractId == dbContract.Id && x.RentableItemId == rentableItemId); if (checkContractRentableItem != null) { contractRentableItem.WorkflowState = Constants.WorkflowStates.Created; await contractRentableItem.Update(_dbContext); } else { ContractRentableItem createContractRentableItem = new ContractRentableItem(); createContractRentableItem.ContractId = dbContract.Id; createContractRentableItem.RentableItemId = rentableItemId; await createContractRentableItem.Create(_dbContext); } } return(new OperationResult(true, _rentableItemsLocalizationService.GetString("ContractsUpdatedSuccessfully"))); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, _rentableItemsLocalizationService.GetString("ErrorWhileUpdatingContract"))); } }