From 3c8e4b2240f50bb323e705dd7917b6387ccd533f Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Tue, 19 Jan 2021 14:41:50 -0600 Subject: [PATCH] Cleaned up some warnings and implemented re-occuring scan libraries task. Customization of task schedules is in v0.2. --- API/Controllers/LibraryController.cs | 4 +--- API/Controllers/SeriesController.cs | 7 +------ API/Data/DataContext.cs | 5 ----- API/Data/SeriesRepository.cs | 11 ----------- API/Data/UnitOfWork.cs | 2 +- API/Data/UserRepository.cs | 14 +------------- API/Entities/AppUserProgress.cs | 3 +-- API/Entities/Library.cs | 1 - API/Helpers/AutoMapperProfiles.cs | 7 ------- API/Interfaces/IDirectoryService.cs | 5 ++--- API/Interfaces/ISeriesRepository.cs | 2 +- API/Services/DirectoryService.cs | 11 ++++++++++- API/Services/TaskScheduler.cs | 6 ++---- 13 files changed, 20 insertions(+), 58 deletions(-) diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index f6e8afd9a..67990db2e 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -22,18 +22,16 @@ namespace API.Controllers private readonly IMapper _mapper; private readonly ITaskScheduler _taskScheduler; private readonly IUnitOfWork _unitOfWork; - private readonly ICacheService _cacheService; public LibraryController(IDirectoryService directoryService, ILogger logger, IMapper mapper, ITaskScheduler taskScheduler, - IUnitOfWork unitOfWork, ICacheService cacheService) + IUnitOfWork unitOfWork) { _directoryService = directoryService; _logger = logger; _mapper = mapper; _taskScheduler = taskScheduler; _unitOfWork = unitOfWork; - _cacheService = cacheService; } /// diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.cs index 589362a12..af2d36bb6 100644 --- a/API/Controllers/SeriesController.cs +++ b/API/Controllers/SeriesController.cs @@ -4,8 +4,6 @@ using System.Threading.Tasks; using API.DTOs; using API.Extensions; using API.Interfaces; -using AutoMapper; -using Hangfire; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -16,15 +14,12 @@ namespace API.Controllers { private readonly ILogger _logger; private readonly ITaskScheduler _taskScheduler; - private readonly ICacheService _cacheService; private readonly IUnitOfWork _unitOfWork; - public SeriesController(ILogger logger, ITaskScheduler taskScheduler, - ICacheService cacheService, IUnitOfWork unitOfWork) + public SeriesController(ILogger logger, ITaskScheduler taskScheduler, IUnitOfWork unitOfWork) { _logger = logger; _taskScheduler = taskScheduler; - _cacheService = cacheService; _unitOfWork = unitOfWork; } diff --git a/API/Data/DataContext.cs b/API/Data/DataContext.cs index 09a53f0cd..2bb9424c0 100644 --- a/API/Data/DataContext.cs +++ b/API/Data/DataContext.cs @@ -39,11 +39,6 @@ namespace API.Data .WithOne(u => u.Role) .HasForeignKey(ur => ur.RoleId) .IsRequired(); - // AppUsers have Libraries, not other way around - // builder.Entity() - // .HasMany(p => p.AppUsers) - // .WithMany(p => p.Libraries) - // .UsingEntity(j => j.ToTable("AppUserLibrary")); } void OnEntityTracked(object sender, EntityTrackedEventArgs e) diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.cs index d5106e15e..510a21388 100644 --- a/API/Data/SeriesRepository.cs +++ b/API/Data/SeriesRepository.cs @@ -63,17 +63,6 @@ namespace API.Data public async Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId) { - // if (userId > 0) - // { - // return await _context.AppUserProgresses - // .Include(p => p.Series) - // .Where(p => p.AppUserId == userId && p.Series.LibraryId == libraryId) - // .Select(p => p.Series) - // .ProjectTo(_mapper.ConfigurationProvider) - // //.Select(s => s.PagesRead = ) - // .ToListAsync(); - // } - var sw = Stopwatch.StartNew(); var series = await _context.Series .Where(s => s.LibraryId == libraryId) diff --git a/API/Data/UnitOfWork.cs b/API/Data/UnitOfWork.cs index 77f461f7b..25d0002c7 100644 --- a/API/Data/UnitOfWork.cs +++ b/API/Data/UnitOfWork.cs @@ -20,7 +20,7 @@ namespace API.Data } public ISeriesRepository SeriesRepository => new SeriesRepository(_context, _mapper); - public IUserRepository UserRepository => new UserRepository(_context, _mapper, _userManager); + public IUserRepository UserRepository => new UserRepository(_context, _userManager); public ILibraryRepository LibraryRepository => new LibraryRepository(_context, _mapper); public async Task Complete() diff --git a/API/Data/UserRepository.cs b/API/Data/UserRepository.cs index 72bcdcc63..2569e1604 100644 --- a/API/Data/UserRepository.cs +++ b/API/Data/UserRepository.cs @@ -5,8 +5,6 @@ using API.Constants; using API.DTOs; using API.Entities; using API.Interfaces; -using AutoMapper; -using AutoMapper.QueryableExtensions; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -15,13 +13,11 @@ namespace API.Data public class UserRepository : IUserRepository { private readonly DataContext _context; - private readonly IMapper _mapper; private readonly UserManager _userManager; - public UserRepository(DataContext context, IMapper mapper, UserManager userManager) + public UserRepository(DataContext context, UserManager userManager) { _context = context; - _mapper = mapper; _userManager = userManager; } @@ -77,13 +73,5 @@ namespace API.Data .AsNoTracking() .ToListAsync(); } - - // public async Task GetMemberAsync(string username) - // { - // return await _context.Users.Where(x => x.UserName == username) - // .Include(x => x.Libraries) - // .ProjectTo(_mapper.ConfigurationProvider) - // .SingleOrDefaultAsync(); - // } } } \ No newline at end of file diff --git a/API/Entities/AppUserProgress.cs b/API/Entities/AppUserProgress.cs index 8097c5abf..0f05f4dee 100644 --- a/API/Entities/AppUserProgress.cs +++ b/API/Entities/AppUserProgress.cs @@ -1,5 +1,4 @@ -using System.ComponentModel.DataAnnotations.Schema; - + namespace API.Entities { /// diff --git a/API/Entities/Library.cs b/API/Entities/Library.cs index 9c93fa337..3c2129c37 100644 --- a/API/Entities/Library.cs +++ b/API/Entities/Library.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; using API.Entities.Interfaces; namespace API.Entities diff --git a/API/Helpers/AutoMapperProfiles.cs b/API/Helpers/AutoMapperProfiles.cs index 586dc04f0..714871813 100644 --- a/API/Helpers/AutoMapperProfiles.cs +++ b/API/Helpers/AutoMapperProfiles.cs @@ -10,13 +10,6 @@ namespace API.Helpers public AutoMapperProfiles() { CreateMap(); - // .ForMember(dest => dest.Folders, - // opt => - // opt.MapFrom(src => src.Folders.Select(x => new FolderPath() - // { - // Path = x, - // //LibraryId = src.Id - // }).ToList())); CreateMap(); diff --git a/API/Interfaces/IDirectoryService.cs b/API/Interfaces/IDirectoryService.cs index cb007b1a5..93e7c1e7b 100644 --- a/API/Interfaces/IDirectoryService.cs +++ b/API/Interfaces/IDirectoryService.cs @@ -13,9 +13,6 @@ namespace API.Interfaces /// List of folder names IEnumerable ListDirectory(string rootPath); - - //IList ListFiles(string rootPath); - /// /// Given a library id, scans folders for said library. Parses files and generates DB updates. Will overwrite /// cover images if forceUpdate is true. @@ -24,6 +21,8 @@ namespace API.Interfaces /// Force overwriting for cover images void ScanLibrary(int libraryId, bool forceUpdate); + void ScanLibraries(); + /// /// Returns the path a volume would be extracted to. /// Deprecated. diff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.cs index 1e72cef3c..a33fc18aa 100644 --- a/API/Interfaces/ISeriesRepository.cs +++ b/API/Interfaces/ISeriesRepository.cs @@ -13,7 +13,7 @@ namespace API.Interfaces Series GetSeriesByName(string name); Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId); Task> GetSeriesForLibraryIdAsync(int libraryId); - Task> GetVolumesDtoAsync(int seriesId, int userId = 0); + Task> GetVolumesDtoAsync(int seriesId, int userId); IEnumerable GetVolumes(int seriesId); Task GetSeriesDtoByIdAsync(int seriesId); diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs index d652ee149..9c619408a 100644 --- a/API/Services/DirectoryService.cs +++ b/API/Services/DirectoryService.cs @@ -210,7 +210,16 @@ namespace API.Services return volumes; } - public void ScanLibrary(int libraryId, bool forceUpdate) + public void ScanLibraries() + { + var libraries = Task.Run(() => _unitOfWork.LibraryRepository.GetLibrariesAsync()).Result.ToList(); + foreach (var lib in libraries) + { + ScanLibrary(lib.Id, false); + } + } + + public void ScanLibrary(int libraryId, bool forceUpdate) { var sw = Stopwatch.StartNew(); Library library; diff --git a/API/Services/TaskScheduler.cs b/API/Services/TaskScheduler.cs index d33b82758..490f8b2b3 100644 --- a/API/Services/TaskScheduler.cs +++ b/API/Services/TaskScheduler.cs @@ -8,21 +8,19 @@ namespace API.Services { private readonly ICacheService _cacheService; private readonly ILogger _logger; - private readonly IUnitOfWork _unitOfWork; private readonly IDirectoryService _directoryService; public BackgroundJobServer Client => new BackgroundJobServer(); public TaskScheduler(ICacheService cacheService, ILogger logger, - IUnitOfWork unitOfWork, IDirectoryService directoryService) + IDirectoryService directoryService) { _cacheService = cacheService; _logger = logger; - _unitOfWork = unitOfWork; _directoryService = directoryService; _logger.LogInformation("Scheduling/Updating cache cleanup on a daily basis."); RecurringJob.AddOrUpdate(() => _cacheService.Cleanup(), Cron.Daily); - //RecurringJob.AddOrUpdate(() => scanService.ScanLibraries(), Cron.Daily); + RecurringJob.AddOrUpdate(() => directoryService.ScanLibraries(), Cron.Daily); } public void ScanLibrary(int libraryId, bool forceUpdate = false)