Kavita/API/Data/AppUserProgressRepository.cs
Joseph Milazzo 2b99c8abfa
Scan Bugfixes (#177)
* Added way  more logging for debugging issue #163.

Fixed #175

* Removed some comment that isn't needed

* Fixed a enumeration issue due to removing while enumerating
2021-04-16 12:21:16 -05:00

32 lines
1008 B
C#

using System.Linq;
using System.Threading.Tasks;
using API.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace API.Data
{
public class AppUserProgressRepository : IAppUserProgressRepository
{
private readonly DataContext _context;
public AppUserProgressRepository(DataContext context)
{
_context = context;
}
/// <summary>
/// This will remove any entries that have chapterIds that no longer exists. This will execute the save as well.
/// </summary>
public async Task<int> CleanupAbandonedChapters()
{
var chapterIds = _context.Chapter.Select(c => c.Id);
var rowsToRemove = await _context.AppUserProgresses
.Where(progress => !chapterIds.Contains(progress.ChapterId))
.ToListAsync();
_context.RemoveRange(rowsToRemove);
return await _context.SaveChangesAsync() > 0 ? rowsToRemove.Count : 0;
}
}
}