mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-13 16:52:56 -07:00
refactor(request-limits): ♻️ Refactored the request limits to sit in their own services
This commit is contained in:
parent
364b9f11af
commit
6adf75d0ec
13 changed files with 983 additions and 403 deletions
|
@ -753,98 +753,5 @@ namespace Ombi.Core.Engine
|
|||
|
||||
return new RequestEngineResult { Result = true, Message = $"{movieName} has been successfully added!", RequestId = model.Id };
|
||||
}
|
||||
|
||||
public async Task<RequestQuotaCountModel> GetRemainingRequests(OmbiUser user)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
user = await GetUser();
|
||||
|
||||
// If user is still null after attempting to get the logged in user, return null.
|
||||
if (user == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
int limit = user.MovieRequestLimit ?? 0;
|
||||
|
||||
if (limit <= 0)
|
||||
{
|
||||
return new RequestQuotaCountModel()
|
||||
{
|
||||
HasLimit = false,
|
||||
Limit = 0,
|
||||
Remaining = 0,
|
||||
NextRequest = DateTime.Now,
|
||||
};
|
||||
}
|
||||
|
||||
IQueryable<RequestLog> log = _requestLog.GetAll().Where(x => x.UserId == user.Id && x.RequestType == RequestType.Movie);
|
||||
|
||||
|
||||
int count = 0;
|
||||
DateTime oldestRequestedAt = DateTime.Now;
|
||||
DateTime nextRequest = DateTime.Now;
|
||||
|
||||
if (!user.MovieRequestLimitType.HasValue)
|
||||
{
|
||||
count = limit - await log.CountAsync(x => x.RequestDate >= DateTime.UtcNow.AddDays(-7));
|
||||
|
||||
oldestRequestedAt = await log.Where(x => x.RequestDate >= DateTime.UtcNow.AddDays(-7))
|
||||
.OrderBy(x => x.RequestDate)
|
||||
.Select(x => x.RequestDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return new RequestQuotaCountModel()
|
||||
{
|
||||
HasLimit = true,
|
||||
Limit = limit,
|
||||
Remaining = count < 0 ? 0 : count,
|
||||
NextRequest = DateTime.SpecifyKind(oldestRequestedAt.AddDays(7), DateTimeKind.Utc),
|
||||
};
|
||||
}
|
||||
|
||||
switch (user.MovieRequestLimitType)
|
||||
{
|
||||
case RequestLimitType.Day:
|
||||
count = limit - await log.CountAsync(x => x.RequestDate >= DateTime.UtcNow.Date);
|
||||
oldestRequestedAt = await log.Where(x => x.RequestDate >= DateTime.UtcNow.Date)
|
||||
.OrderBy(x => x.RequestDate)
|
||||
.Select(x => x.RequestDate)
|
||||
.FirstOrDefaultAsync();
|
||||
nextRequest = oldestRequestedAt.AddDays(1).Date;
|
||||
break;
|
||||
case RequestLimitType.Week:
|
||||
var fdow = DateTime.UtcNow.FirstDateInWeek();
|
||||
count = limit - await log.CountAsync(x => x.RequestDate >= fdow);
|
||||
oldestRequestedAt = await log.Where(x => x.RequestDate >= fdow)
|
||||
.OrderBy(x => x.RequestDate)
|
||||
.Select(x => x.RequestDate)
|
||||
.FirstOrDefaultAsync();
|
||||
nextRequest = fdow.AddDays(7).Date;
|
||||
break;
|
||||
case RequestLimitType.Month:
|
||||
var now = DateTime.UtcNow;
|
||||
var firstDayOfMonth = new DateTime(now.Year, now.Month, 1);
|
||||
count = limit - await log.CountAsync(x => x.RequestDate >= firstDayOfMonth);
|
||||
oldestRequestedAt = await log.Where(x => x.RequestDate >= firstDayOfMonth)
|
||||
.OrderBy(x => x.RequestDate)
|
||||
.Select(x => x.RequestDate)
|
||||
.FirstOrDefaultAsync();
|
||||
nextRequest = firstDayOfMonth.AddMonths(1).Date;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return new RequestQuotaCountModel()
|
||||
{
|
||||
HasLimit = true,
|
||||
Limit = limit,
|
||||
Remaining = count < 0 ? 0 : count,
|
||||
NextRequest = DateTime.SpecifyKind(nextRequest, DateTimeKind.Utc),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue