Add a private route to get items to auto refresh

This commit is contained in:
Zoe Roux 2024-04-23 22:06:20 +02:00
parent 58f0f3d74b
commit 9ee35534bd
No known key found for this signature in database
2 changed files with 48 additions and 0 deletions

View File

@ -28,6 +28,7 @@ using Kyoo.Abstractions.Models;
using Kyoo.Postgresql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using static System.Text.Json.JsonNamingPolicy;
namespace Kyoo.Core.Controllers;
@ -82,4 +83,38 @@ public class MiscRepository(
.Concat(context.Movies.Select(x => x.Path))
.ToListAsync();
}
public async Task<ICollection<RefreshableItem>> GetRefreshableItems(DateTime end)
{
IQueryable<RefreshableItem> GetItems<T>()
where T : class, IResource, IRefreshable
{
return context
.Set<T>()
.Select(x => new RefreshableItem
{
Kind = CamelCase.ConvertName(typeof(T).Name),
Id = x.Id,
RefreshDate = x.NextMetadataRefresh!.Value
});
}
return await GetItems<Show>()
.Concat(GetItems<Movie>())
.Concat(GetItems<Season>())
.Concat(GetItems<Episode>())
.Concat(GetItems<Collection>())
.Where(x => x.RefreshDate <= end)
.OrderBy(x => x.RefreshDate)
.ToListAsync();
}
}
public class RefreshableItem
{
public string Kind { get; set; }
public Guid Id { get; set; }
public DateTime RefreshDate { get; set; }
}

View File

@ -16,6 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Kyoo.Abstractions.Models.Permissions;
@ -42,4 +43,16 @@ public class Misc(MiscRepository repo) : BaseApi
{
return repo.GetRegisteredPaths();
}
/// <summary>
/// List items to refresh.
/// </summary>
/// <param name="date">The upper limit for the refresh date.</param>
/// <returns>The items that should be refreshed before the given date</returns>
[HttpGet("/refreshables")]
[ProducesResponseType(StatusCodes.Status200OK)]
public Task<ICollection<RefreshableItem>> GetAllPaths([FromQuery] DateTime? date)
{
return repo.GetRefreshableItems(date ?? DateTime.UtcNow);
}
}