Commit 96e3e882 authored by tidusjar's avatar tidusjar

Fixed the issues where the DB was being disposed too early

parent 47f323fc
......@@ -188,11 +188,7 @@ namespace Ombi.Core.Engine
var requests = await (OrderMovies(allRequests, orderFilter.OrderType)).Skip(position).Take(count)
.ToListAsync();
requests.ForEach(async x =>
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
await CheckForSubscription(shouldHide, requests);
return new RequestsViewModel<MovieRequests>
{
Collection = requests,
......@@ -251,26 +247,30 @@ namespace Ombi.Core.Engine
allRequests = await MovieRepository.GetWithUser().ToListAsync();
}
allRequests.ForEach(async x =>
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
await CheckForSubscription(shouldHide, allRequests);
return allRequests;
}
private async Task CheckForSubscription(HideResult shouldHide, MovieRequests x)
private async Task CheckForSubscription(HideResult shouldHide, List<MovieRequests> movieRequests)
{
if (shouldHide.UserId == x.RequestedUserId)
{
x.ShowSubscribe = false;
}
else
var requestIds = movieRequests.Select(x => x.Id);
var sub = await _subscriptionRepository.GetAll().Where(s =>
s.UserId == shouldHide.UserId && requestIds.Contains(s.RequestId) && s.RequestType == RequestType.Movie)
.ToListAsync();
foreach (var x in movieRequests)
{
x.ShowSubscribe = true;
var sub = await _subscriptionRepository.GetAll().FirstOrDefaultAsync(s =>
s.UserId == shouldHide.UserId && s.RequestId == x.Id && s.RequestType == RequestType.Movie);
x.Subscribed = sub != null;
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
if (shouldHide.UserId == x.RequestedUserId)
{
x.ShowSubscribe = false;
}
else
{
x.ShowSubscribe = true;
var hasSub = sub.FirstOrDefault(r => r.RequestId == x.Id);
x.Subscribed = hasSub != null;
}
}
}
......@@ -293,11 +293,8 @@ namespace Ombi.Core.Engine
}
var results = allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToList();
results.ForEach(async x =>
{
x.PosterPath = PosterPathHelper.FixPosterPath(x.PosterPath);
await CheckForSubscription(shouldHide, x);
});
await CheckForSubscription(shouldHide, results);
return results;
}
......@@ -493,7 +490,7 @@ namespace Ombi.Core.Engine
RequestType = RequestType.Movie,
});
return new RequestEngineResult {Result = true, Message = $"{movieName} has been successfully added!", RequestId = model.Id};
return new RequestEngineResult { Result = true, Message = $"{movieName} has been successfully added!", RequestId = model.Id };
}
public async Task<RequestQuotaCountModel> GetRemainingRequests(OmbiUser user)
......@@ -533,7 +530,7 @@ namespace Ombi.Core.Engine
return new RequestQuotaCountModel()
{
HasLimit = true,
HasLimit = true,
Limit = limit,
Remaining = count,
NextRequest = DateTime.SpecifyKind(oldestRequestedAt.AddDays(7), DateTimeKind.Utc),
......
......@@ -156,10 +156,10 @@ namespace Ombi.Core.Engine
.ThenInclude(x => x.Episodes)
.OrderByDescending(x => x.ChildRequests.Select(y => y.RequestedDate).FirstOrDefault())
.Skip(position).Take(count).ToListAsync();
}
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
}
await CheckForSubscription(shouldHide, allRequests);
allRequests.ForEach(async r => { });
return new RequestsViewModel<TvRequests>
{
......@@ -194,7 +194,8 @@ namespace Ombi.Core.Engine
{
return new RequestsViewModel<TvRequests>();
}
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return new RequestsViewModel<TvRequests>
{
......@@ -216,7 +217,7 @@ namespace Ombi.Core.Engine
allRequests = await TvRepository.Get().ToListAsync();
}
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return allRequests;
}
......@@ -236,7 +237,7 @@ namespace Ombi.Core.Engine
allRequests = await TvRepository.GetLite().ToListAsync();
}
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return allRequests;
}
......@@ -255,7 +256,7 @@ namespace Ombi.Core.Engine
request = await TvRepository.Get().Where(x => x.Id == requestId).FirstOrDefaultAsync();
}
await CheckForSubscription(shouldHide, request);
await CheckForSubscription(shouldHide, new List<TvRequests>{request});
return request;
}
......@@ -304,7 +305,7 @@ namespace Ombi.Core.Engine
allRequests = await TvRepository.GetChild().Include(x => x.SeasonRequests).Where(x => x.ParentRequestId == tvId).ToListAsync();
}
allRequests.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, allRequests);
return allRequests;
}
......@@ -323,7 +324,7 @@ namespace Ombi.Core.Engine
}
var results = await allRequests.Where(x => x.Title.Contains(search, CompareOptions.IgnoreCase)).ToListAsync();
results.ForEach(async r => { await CheckForSubscription(shouldHide, r); });
await CheckForSubscription(shouldHide, results);
return results;
}
......@@ -420,7 +421,7 @@ namespace Ombi.Core.Engine
public async Task<ChildRequests> UpdateChildRequest(ChildRequests request)
{
await TvRepository.UpdateChild(request);
await TvRepository.UpdateChild(request);
return request;
}
......@@ -438,14 +439,14 @@ namespace Ombi.Core.Engine
// Delete the parent
TvRepository.Db.TvRequests.Remove(parent);
}
await TvRepository.Db.SaveChangesAsync();
}
public async Task RemoveTvRequest(int requestId)
{
var request = await TvRepository.Get().FirstOrDefaultAsync(x => x.Id == requestId);
await TvRepository.Delete(request);
await TvRepository.Delete(request);
}
public async Task<bool> UserHasRequest(string userId)
......@@ -520,26 +521,32 @@ namespace Ombi.Core.Engine
}
}
private async Task CheckForSubscription(HideResult shouldHide, TvRequests x)
private async Task CheckForSubscription(HideResult shouldHide, List<TvRequests> x)
{
foreach (var tv in x.ChildRequests)
foreach (var tvRequest in x)
{
await CheckForSubscription(shouldHide, tv);
await CheckForSubscription(shouldHide, tvRequest.ChildRequests);
}
}
private async Task CheckForSubscription(HideResult shouldHide, ChildRequests x)
private async Task CheckForSubscription(HideResult shouldHide, List<ChildRequests> childRequests)
{
if (shouldHide.UserId == x.RequestedUserId)
var sub = _subscriptionRepository.GetAll();
var childIds = childRequests.Select(x => x.Id);
var relevantSubs = await sub.Where(s =>
s.UserId == shouldHide.UserId && childIds.Contains(s.Id) && s.RequestType == RequestType.TvShow).ToListAsync();
foreach (var x in childRequests)
{
x.ShowSubscribe = false;
}
else
{
x.ShowSubscribe = true;
var sub = await _subscriptionRepository.GetAll().FirstOrDefaultAsync(s =>
s.UserId == shouldHide.UserId && s.RequestId == x.Id && s.RequestType == RequestType.TvShow);
x.Subscribed = sub != null;
if (shouldHide.UserId == x.RequestedUserId)
{
x.ShowSubscribe = false;
}
else
{
x.ShowSubscribe = true;
var result = relevantSubs.FirstOrDefault(s => s.RequestId == x.Id);
x.Subscribed = result != null;
}
}
}
......@@ -560,7 +567,7 @@ namespace Ombi.Core.Engine
return await AfterRequest(model.ChildRequests.FirstOrDefault());
}
private static List<ChildRequests> SortEpisodes(List<ChildRequests> items)
private static List<ChildRequests> SortEpisodes(List<ChildRequests> items)
{
foreach (var value in items)
{
......@@ -597,7 +604,7 @@ namespace Ombi.Core.Engine
var result = await TvSender.Send(model);
if (result.Success)
{
return new RequestEngineResult { Result = true, RequestId = model.Id};
return new RequestEngineResult { Result = true, RequestId = model.Id };
}
return new RequestEngineResult
{
......@@ -650,10 +657,10 @@ namespace Ombi.Core.Engine
DateTime oldestRequestedAt = await log.OrderBy(x => x.RequestDate)
.Select(x => x.RequestDate)
.FirstOrDefaultAsync();
return new RequestQuotaCountModel()
{
HasLimit = true,
HasLimit = true,
Limit = limit,
Remaining = count,
NextRequest = DateTime.SpecifyKind(oldestRequestedAt.AddDays(7), DateTimeKind.Utc),
......
......@@ -213,7 +213,6 @@ namespace Ombi.Schedule.Jobs.Emby
if (disposing)
{
_movieRepo?.Dispose();
}
_disposed = true;
}
......
......@@ -206,7 +206,6 @@ namespace Ombi.Schedule.Jobs.Emby
if (disposing)
{
_settings?.Dispose();
_repo?.Dispose();
}
_disposed = true;
}
......
......@@ -156,7 +156,6 @@ namespace Ombi.Schedule.Jobs.Emby
if (disposing)
{
_settings?.Dispose();
_repo?.Dispose();
}
_disposed = true;
}
......
......@@ -49,7 +49,6 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing)
{
_issuesRepository?.Dispose();
_settings?.Dispose();
}
_disposed = true;
......
......@@ -99,7 +99,6 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing)
{
_plexRepo?.Dispose();
_settings?.Dispose();
}
_disposed = true;
......
......@@ -66,7 +66,7 @@ namespace Ombi.Schedule.Jobs.Ombi
private readonly IPlexContentRepository _plex;
private readonly IEmbyContentRepository _emby;
private readonly IExternalRepository<RecentlyAddedLog> _recentlyAddedLog;
private readonly IRepository<RecentlyAddedLog> _recentlyAddedLog;
private readonly IMovieDbApi _movieApi;
private readonly ITvMazeApi _tvApi;
private readonly IEmailProvider _email;
......@@ -78,7 +78,7 @@ namespace Ombi.Schedule.Jobs.Ombi
private readonly UserManager<OmbiUser> _userManager;
private readonly ILogger _log;
private readonly ILidarrApi _lidarrApi;
private readonly IRepository<LidarrAlbumCache> _lidarrAlbumRepository;
private readonly IExternalRepository<LidarrAlbumCache> _lidarrAlbumRepository;
private readonly ISettingsService<LidarrSettings> _lidarrSettings;
private readonly ISettingsService<PlexSettings> _plexSettings;
private readonly ISettingsService<EmbySettings> _embySettings;
......@@ -931,12 +931,9 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing)
{
_plex?.Dispose();
_emby?.Dispose();
_newsletterSettings?.Dispose();
_customizationSettings?.Dispose();
_emailSettings.Dispose();
_recentlyAddedLog.Dispose();
_templateRepo?.Dispose();
_userManager?.Dispose();
}
......
......@@ -352,8 +352,6 @@ namespace Ombi.Schedule.Jobs.Ombi
if (disposing)
{
_plexRepo?.Dispose();
_embyRepo?.Dispose();
_plexSettings?.Dispose();
}
_disposed = true;
......
......@@ -193,8 +193,6 @@ namespace Ombi.Schedule.Jobs.Plex
if (disposing)
{
_movieRepo?.Dispose();
_repo?.Dispose();
}
_disposed = true;
}
......
......@@ -650,7 +650,6 @@ namespace Ombi.Schedule.Jobs.Plex
if (disposing)
{
Plex?.Dispose();
Repo?.Dispose();
EpisodeSync?.Dispose();
}
_disposed = true;
......
......@@ -206,7 +206,6 @@ namespace Ombi.Schedule.Jobs.Plex
if (disposing)
{
_repo?.Dispose();
_settings?.Dispose();
}
_disposed = true;
......
......@@ -12,5 +12,10 @@ namespace Ombi.Store.Context.MySql
Database.Migrate();
}
public override void Dispose()
{
base.Dispose();
}
}
}
\ No newline at end of file
......@@ -107,25 +107,25 @@ namespace Ombi.Store.Repository
}
private bool _disposed;
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_ctx?.Dispose();
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
//private bool _disposed;
//// Protected implementation of Dispose pattern.
//protected virtual void Dispose(bool disposing)
//{
// if (_disposed)
// return;
// if (disposing)
// {
// _ctx?.Dispose();
// }
// _disposed = true;
//}
//public void Dispose()
//{
// Dispose(true);
// GC.SuppressFinalize(this);
//}
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public interface IExternalRepository<T> : IDisposable where T : Entity
public interface IExternalRepository<T> where T : Entity
{
Task<T> Find(object key);
IQueryable<T> GetAll();
......
......@@ -9,7 +9,7 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public interface IRepository<T> : IDisposable where T : Entity
public interface IRepository<T> where T : Entity
{
Task<T> Find(object key);
IQueryable<T> GetAll();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment