diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index 64a259503..40e9eb1f1 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -70,8 +70,9 @@ namespace API.Controllers if (await _userRepository.SaveAllAsync()) - { - //TODO: Enqueue scan library task + { + var createdLibrary = await _libraryRepository.GetLibraryForNameAsync(library.Name); + BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(createdLibrary.Id)); return Ok(); } @@ -128,27 +129,22 @@ namespace API.Controllers [Authorize(Policy = "RequireAdminRole")] [HttpPost("scan")] - public async Task ScanLibrary(int libraryId) + public ActionResult ScanLibrary(int libraryId) { - var library = await _libraryRepository.GetLibraryDtoForIdAsync(libraryId); - - // We have to send a json encoded Library (aka a DTO) to the Background Job thread. - // Because we use EF, we have circular dependencies back to Library and it will crap out - // TODO: Refactor this to use libraryId and move Library call in method. - BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(library)); + BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(libraryId)); return Ok(); } [HttpGet("libraries-for")] public async Task>> GetLibrariesForUser(string username) { - return Ok(await _libraryRepository.GetLibrariesForUsernameAysnc(username)); + return Ok(await _libraryRepository.GetLibrariesDtoForUsernameAsync(username)); } [HttpGet("series")] public async Task>> GetSeriesForLibrary(int libraryId) { - return Ok(await _seriesRepository.GetSeriesForLibraryIdAsync(libraryId)); + return Ok(await _seriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId)); } } } \ No newline at end of file diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.cs index dbdfaf93e..4c5e66a41 100644 --- a/API/Controllers/SeriesController.cs +++ b/API/Controllers/SeriesController.cs @@ -27,13 +27,13 @@ namespace API.Controllers [HttpGet("{seriesId}")] public async Task> GetSeries(int seriesId) { - return Ok(await _seriesRepository.GetSeriesByIdAsync(seriesId)); + return Ok(await _seriesRepository.GetSeriesDtoByIdAsync(seriesId)); } [HttpGet("volumes")] public async Task>> GetVolumes(int seriesId) { - return Ok(await _seriesRepository.GetVolumesAsync(seriesId)); + return Ok(await _seriesRepository.GetVolumesDtoAsync(seriesId)); } } } \ No newline at end of file diff --git a/API/Controllers/UsersController.cs b/API/Controllers/UsersController.cs index b860316df..535345b75 100644 --- a/API/Controllers/UsersController.cs +++ b/API/Controllers/UsersController.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Threading.Tasks; using API.DTOs; -using API.Entities; using API.Extensions; using API.Interfaces; using Microsoft.AspNetCore.Authorization; @@ -51,7 +50,7 @@ namespace API.Controllers if (user == null) return BadRequest("Could not validate user"); - var libs = await _libraryRepository.GetLibrariesForUsernameAysnc(user.UserName); + var libs = await _libraryRepository.GetLibrariesDtoForUsernameAsync(user.UserName); return Ok(libs.Any(x => x.Id == libraryId)); } diff --git a/API/Data/LibraryRepository.cs b/API/Data/LibraryRepository.cs index ce986279f..ef8b416d3 100644 --- a/API/Data/LibraryRepository.cs +++ b/API/Data/LibraryRepository.cs @@ -36,16 +36,7 @@ namespace API.Data return _context.SaveChanges() > 0; } - public Library GetLibraryForName(string libraryName) - { - return _context.Library - .Where(x => x.Name == libraryName) - .Include(f => f.Folders) - .Include(s => s.Series) - .Single(); - } - - public async Task> GetLibrariesForUsernameAysnc(string userName) + public async Task> GetLibrariesDtoForUsernameAsync(string userName) { return await _context.Library .Include(l => l.AppUsers) @@ -53,21 +44,22 @@ namespace API.Data .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } + public async Task GetLibraryForNameAsync(string libraryName) + { + return await _context.Library + .Where(x => x.Name == libraryName) + .Include(f => f.Folders) + .Include(s => s.Series) + .SingleAsync(); + } + public async Task> GetLibrariesAsync() { return await _context.Library .Include(f => f.Folders) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } - - public async Task GetLibraryDtoForIdAsync(int libraryId) - { - return await _context.Library - .Where(x => x.Id == libraryId) - .Include(f => f.Folders) - .ProjectTo(_mapper.ConfigurationProvider).SingleAsync(); - } - + public async Task GetLibraryForIdAsync(int libraryId) { return await _context.Library diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.cs index a9623275b..011550348 100644 --- a/API/Data/SeriesRepository.cs +++ b/API/Data/SeriesRepository.cs @@ -46,7 +46,7 @@ namespace API.Data return _context.Series.SingleOrDefault(x => x.Name == name); } - public async Task> GetSeriesForLibraryIdAsync(int libraryId) + public async Task> GetSeriesDtoForLibraryIdAsync(int libraryId) { return await _context.Series .Where(series => series.LibraryId == libraryId) @@ -54,22 +54,14 @@ namespace API.Data .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } - public async Task> GetVolumesAsync(int seriesId) + public async Task> GetVolumesDtoAsync(int seriesId) { return await _context.Volume .Where(vol => vol.SeriesId == seriesId) .OrderBy(volume => volume.Number) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); } - - public IEnumerable GetVolumesDto(int seriesId) - { - return _context.Volume - .Where(vol => vol.SeriesId == seriesId) - .OrderBy(vol => vol.Number) - .ProjectTo(_mapper.ConfigurationProvider).ToList(); - } - + public IEnumerable GetVolumes(int seriesId) { return _context.Volume @@ -78,7 +70,7 @@ namespace API.Data .ToList(); } - public async Task GetSeriesByIdAsync(int seriesId) + public async Task GetSeriesDtoByIdAsync(int seriesId) { return await _context.Series.Where(x => x.Id == seriesId) .ProjectTo(_mapper.ConfigurationProvider).SingleAsync(); diff --git a/API/Interfaces/IDirectoryService.cs b/API/Interfaces/IDirectoryService.cs index 818aa9451..351e67a68 100644 --- a/API/Interfaces/IDirectoryService.cs +++ b/API/Interfaces/IDirectoryService.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using API.DTOs; namespace API.Interfaces { @@ -7,6 +6,6 @@ namespace API.Interfaces { IEnumerable ListDirectory(string rootPath); - void ScanLibrary(LibraryDto library); + void ScanLibrary(int libraryId); } } \ No newline at end of file diff --git a/API/Interfaces/ILibraryRepository.cs b/API/Interfaces/ILibraryRepository.cs index 409068fc3..93a0832ae 100644 --- a/API/Interfaces/ILibraryRepository.cs +++ b/API/Interfaces/ILibraryRepository.cs @@ -10,16 +10,10 @@ namespace API.Interfaces void Update(Library library); Task SaveAllAsync(); Task> GetLibrariesAsync(); - /// - /// Checks to see if a library of the same name exists. We only allow unique library names, no duplicates per LibraryType. - /// - /// - /// Task LibraryExists(string libraryName); - Task GetLibraryDtoForIdAsync(int libraryId); Task GetLibraryForIdAsync(int libraryId); bool SaveAll(); - Library GetLibraryForName(string libraryName); - Task> GetLibrariesForUsernameAysnc(string userName); + Task> GetLibrariesDtoForUsernameAsync(string userName); + Task GetLibraryForNameAsync(string libraryName); } } \ No newline at end of file diff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.cs index ddd085cec..fe2a6b6b3 100644 --- a/API/Interfaces/ISeriesRepository.cs +++ b/API/Interfaces/ISeriesRepository.cs @@ -12,11 +12,10 @@ namespace API.Interfaces Task GetSeriesByNameAsync(string name); Series GetSeriesByName(string name); bool SaveAll(); - Task> GetSeriesForLibraryIdAsync(int libraryId); - Task> GetVolumesAsync(int seriesId); - IEnumerable GetVolumesDto(int seriesId); + Task> GetSeriesDtoForLibraryIdAsync(int libraryId); + Task> GetVolumesDtoAsync(int seriesId); IEnumerable GetVolumes(int seriesId); - Task GetSeriesByIdAsync(int seriesId); + Task GetSeriesDtoByIdAsync(int seriesId); } } \ No newline at end of file diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs index 9d40069e7..706967bc1 100644 --- a/API/Services/DirectoryService.cs +++ b/API/Services/DirectoryService.cs @@ -5,11 +5,9 @@ using System.Collections.Immutable; using System.Diagnostics; using System.IO; using System.Linq; -using System.Security; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; -using API.DTOs; using API.Entities; using API.Interfaces; using API.Parser; @@ -149,6 +147,7 @@ namespace API.Services // BUG: This is creating new volume entries and not resetting each run. IList existingVolumes = _seriesRepository.GetVolumes(series.Id).ToList(); + //IList existingVolumes = Task.Run(() => _seriesRepository.GetVolumesAsync(series.Id)).Result.ToList(); foreach (var info in infos) { var existingVolume = existingVolumes.SingleOrDefault(v => v.Name == info.Volumes); @@ -189,46 +188,45 @@ namespace API.Services return series; } - public void ScanLibrary(LibraryDto library) + public void ScanLibrary(int libraryId) { + var library = Task.Run(() => _libraryRepository.GetLibraryForIdAsync(libraryId)).Result; _scannedSeries = new ConcurrentDictionary>(); _logger.LogInformation($"Beginning scan on {library.Name}"); foreach (var folderPath in library.Folders) { try { - TraverseTreeParallelForEach(folderPath, (f) => + TraverseTreeParallelForEach(folderPath.Path, (f) => { - // Exceptions are no-ops. try { Process(f); } - catch (FileNotFoundException) {} - catch (IOException) {} - catch (UnauthorizedAccessException) {} - catch (SecurityException) {} + catch (FileNotFoundException exception) + { + _logger.LogError(exception, "The file could not be found"); + } }); } catch (ArgumentException ex) { - _logger.LogError(ex, "The directory '{folderPath}' does not exist"); + _logger.LogError(ex, $"The directory '{folderPath}' does not exist"); } } var filtered = _scannedSeries.Where(kvp => !kvp.Value.IsEmpty); var series = filtered.ToImmutableDictionary(v => v.Key, v => v.Value); - // Perform DB activities on ImmutableDictionary - var libraryEntity = _libraryRepository.GetLibraryForName(library.Name); - libraryEntity.Series = new List(); // Temp delete everything for testing + // Perform DB activities + library.Series = new List(); // Temp delete everything until we can mark items Unavailable foreach (var seriesKey in series.Keys) { var s = UpdateSeries(seriesKey, series[seriesKey].ToArray()); _logger.LogInformation($"Created/Updated series {s.Name}"); - libraryEntity.Series.Add(s); + library.Series.Add(s); } - _libraryRepository.Update(libraryEntity); + _libraryRepository.Update(library); if (_libraryRepository.SaveAll()) {