public async Task <RemoveFileFromIndexResponse?> Handle(RemoveFileFromIndexRequest message) { if (Path.IsPathFullyQualified(message.RelativeFilename)) { return(ReturnError(new InvalidOperationError("The path is fully qualified. Only relative paths are stored", ErrorCode.PathFullyQualified))); } var directory = message.PhotoDirectory; using (var dataContext = directory.GetDataContext()) { // check if the file already exists var existingFile = await dataContext.FileRepository.FirstOrDefaultBySpecs(new FindByFilenameSpec(message.RelativeFilename), new IncludeFileLocationsSpec()); if (existingFile == null) { return(ReturnError(new InvalidOperationError("The file is not indexed.", ErrorCode.FileNotIndexed))); } var fileLocation = existingFile.Files.First(x => directory.PathComparer.Equals( message.RelativeFilename, message.RelativeFilename)); existingFile.RemoveLocation(fileLocation.RelativeFilename); await dataContext.FileRepository.RemoveFileLocation(fileLocation); if (!existingFile.Files.Any()) { await dataContext.FileRepository.Delete(existingFile); } return(new RemoveFileFromIndexResponse(!existingFile.Files.Any())); } }
public async Task CantRemoveFileThatIsNotIndexed() { // arrange var mockDirectory = new Mock <IPhotoDirectory>(); var mockFileRepository = new Mock <IIndexedFileRepository>(); var mockDataContext = new Mock <IPhotoDirectoryDataContext>(); mockDataContext.SetupGet(x => x.FileRepository).Returns(mockFileRepository.Object); mockFileRepository.Setup(x => x.FirstOrDefaultBySpecs(It.IsAny <ISpecification <IndexedFile>[]>())).ReturnsAsync((IndexedFile)null); mockDirectory.Setup(x => x.GetDataContext()).Returns(mockDataContext.Object); var useCase = new RemoveFileFromIndexUseCase(); var request = new RemoveFileFromIndexRequest("test.xml", mockDirectory.Object); // act await useCase.Handle(request); // assert ErrorUtils.AssertError(useCase, ErrorType.InvalidOperation, ErrorCode.FileNotIndexed); }