Skip to content

Commit

Permalink
feat: page result
Browse files Browse the repository at this point in the history
  • Loading branch information
Misaka-L committed Sep 16, 2024
1 parent bb83410 commit e88cc2f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 25 deletions.
22 changes: 22 additions & 0 deletions VPMReposSynchronizer.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;
using VPMReposSynchronizer.Core.Models.Types;

namespace VPMReposSynchronizer.Core.Extensions;

public static class EnumerableExtensions
{
public static PageResult<TSource> ToPageResult<TSource>(this IEnumerable<TSource> source, int page, int pageSize)
{
var items = source.Skip(page * pageSize).Take(pageSize).ToArray();

return new PageResult<TSource>(items, source.Count());
}

public static async ValueTask<PageResult<TSource>> ToPageResultAsync<TSource>(this IQueryable<TSource> source,
int page, int pageSize)
{
var items = await source.Skip(page * pageSize).Take(pageSize).ToArrayAsync();

return new PageResult<TSource>(items, source.Count());
}
}
3 changes: 3 additions & 0 deletions VPMReposSynchronizer.Core/Models/Types/PageResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace VPMReposSynchronizer.Core.Models.Types;

public record PageResult<T>(IEnumerable<T> Items, int TotalCount);
21 changes: 14 additions & 7 deletions VPMReposSynchronizer.Core/Services/RepoBrowserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Options;
using Semver;
using VPMReposSynchronizer.Core.Extensions;
using VPMReposSynchronizer.Core.Models.Entity;
using VPMReposSynchronizer.Core.Models.Types;
using VPMReposSynchronizer.Core.Models.Types.RepoBrowser;
Expand All @@ -16,9 +17,9 @@ public class RepoBrowserService(
IOptions<FileHostServiceOptions> fileHostOptions,
IMapper mapper)
{
public async ValueTask<BrowserRepo[]> GetAllReposAsync()
public async ValueTask<PageResult<BrowserRepo>> GetReposAsync(int page, int count)
{
var repoEntities = await repoMetaDataService.GetAllRepos();
var repoEntities = await repoMetaDataService.GetRepos();
var repoSyncStatuses = (await repoSyncStatusService.GetAllSyncStatusAsync())
.ToDictionary(status => status.RepoId, status => status);

Expand All @@ -31,21 +32,27 @@ public async ValueTask<BrowserRepo[]> GetAllReposAsync()
browserRepo.RepoUrl = GetRepoUrl(browserRepo.ApiId);
}

return browserRepos;
return browserRepos.ToPageResult(page, count);
}

public async ValueTask<BrowserPackage[]?> GetAllPackagesAsync(string repoId)
public async ValueTask<PageResult<BrowserPackage>> GetPackagesAsync(string repoId, int count, int page)
{
var packageEntities = await repoMetaDataService.GetVpmPackages(repoId);
var packages = packageEntities
var packageGroups = packageEntities
.Select(GetPackageWithUrl)
.GroupBy(package => package.Name)
.Select(package => package.OrderByDescending(pkg => pkg.Version, SemVersion.SortOrderComparer))
.ToArray();

var packages = packageGroups
.Skip(page * count)
.Take(count)
.Select(package => package.OrderByDescending(pkg => SemVersion.Parse(pkg.Version, SemVersionStyles.Any),
SemVersion.SortOrderComparer))
.Select(packagesGroup =>
new BrowserPackage(packagesGroup.First(), packagesGroup.ToArray(), repoId,
GetRepoUrl(repoId)));

return packages.ToArray();
return new PageResult<BrowserPackage>(packages.ToArray(), packageGroups.Length);
}

public async ValueTask<BrowserPackage[]> SearchVpmPackages(string keyword)
Expand Down
2 changes: 1 addition & 1 deletion VPMReposSynchronizer.Core/Services/RepoMetaDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task DeleteRepoAsync(string id)
await repoSyncTaskScheduleService.ScheduleAllTasks();
}

public async Task<VpmRepoEntity[]> GetAllRepos()
public async Task<VpmRepoEntity[]> GetRepos()
{
return await defaultDbContext.Repos.ToArrayAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private async Task ScheduleAllTasksInternal()
await using var scope = serviceScopeFactory.CreateAsyncScope();
var repoMetaDataService = scope.ServiceProvider.GetRequiredService<RepoMetaDataService>();

var repos = await repoMetaDataService.GetAllRepos();
var repos = await repoMetaDataService.GetRepos();

foreach (var repo in repos)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using VPMReposSynchronizer.Core.DbContexts;
using VPMReposSynchronizer.Core.Extensions;
using VPMReposSynchronizer.Core.Models.Entity;
using VPMReposSynchronizer.Core.Models.Types;

namespace VPMReposSynchronizer.Core.Services.RepoSync;

Expand Down Expand Up @@ -67,14 +69,12 @@ await defaultDbContext.SyncTasks
.SetProperty(task => task.EndTime, endTime => currentDateTime));
}

public async ValueTask<SyncTaskEntity[]> GetSyncTasksAsync(int offset = 0, int limit = 20, string? repoId = null)
public async ValueTask<PageResult<SyncTaskEntity>> GetSyncTasksAsync(int page = 0, int count = 20, string? repoId = null)
{
return await defaultDbContext.SyncTasks
.Where(task => repoId == null || task.RepoId == repoId)
.OrderByDescending(task => task.Id)
.Skip(offset)
.Take(limit)
.ToArrayAsync();
.ToPageResultAsync(page, count);
}

public async ValueTask<SyncTaskEntity?> GetSyncTaskAsync(long id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public async Task<SyncStatusPublic[]> GetAllSyncStatusAsync()
{
var latestSyncTasks = await repoSyncTaskService.GetAllLatestSyncTasksAsync();

var repoUpstreamUrls = (await repoMetaDataService.GetAllRepos())
var repoUpstreamUrls = (await repoMetaDataService.GetRepos())
.ToDictionary(repo => repo.Id, repo => repo.UpStreamUrl);

return latestSyncTasks
Expand Down
16 changes: 9 additions & 7 deletions VPMReposSynchronizer.Entry/Controllers/RepoController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using AutoMapper;
using System.ComponentModel.DataAnnotations;
using AutoMapper;
using Cronos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using VPMReposSynchronizer.Core.Models.Entity;
using VPMReposSynchronizer.Core.Models.Types;
using VPMReposSynchronizer.Core.Models.Types.RepoAdmin;
using VPMReposSynchronizer.Core.Models.Types.RepoBrowser;
using VPMReposSynchronizer.Core.Services;
Expand All @@ -19,10 +21,10 @@ public class RepoController(
{
[Route("")]
[HttpGet]
[ProducesResponseType<BrowserRepo[]>(StatusCodes.Status200OK)]
public async Task<BrowserRepo[]> GetAllRepos()
[ProducesResponseType<PageResult<BrowserRepo>>(StatusCodes.Status200OK)]
public async Task<PageResult<BrowserRepo>> GetRepos([Range(0, int.MaxValue)] int page = 0,[Range(0, 50)] int count = 10)
{
return await repoBrowserService.GetAllReposAsync();
return await repoBrowserService.GetReposAsync(page, count);
}

[HttpPost]
Expand Down Expand Up @@ -90,13 +92,13 @@ public async Task<IActionResult> GetRepo(string id)

[Route("{repoId}/packages")]
[HttpGet]
[ProducesResponseType<BrowserPackage[]>(StatusCodes.Status200OK)]
[ProducesResponseType<PageResult<BrowserPackage>>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetAllPackage(string repoId)
public async Task<ActionResult<PageResult<BrowserPackage>>> GetAllPackage(string repoId, [Range(0, int.MaxValue)] int page = 0, [Range(0, 50)] int count = 10)
{
if (await repoBrowserService.GetRepoAsync(repoId) is null) return NotFound();

var packages = await repoBrowserService.GetAllPackagesAsync(repoId);
var packages = await repoBrowserService.GetPackagesAsync(repoId, count, page);

return Ok(packages);
}
Expand Down
8 changes: 5 additions & 3 deletions VPMReposSynchronizer.Entry/Controllers/SyncTaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class SyncTaskController(
IMapper mapper) : ControllerBase
{
[HttpGet]
public async Task<SyncTaskPublic[]> Index([Range(0, int.MaxValue)] int offset = 0, [Range(1, 100)] int limit = 20, string? repoId = null)
public async Task<PageResult<SyncTaskPublic>> Index([Range(0, int.MaxValue)] int page = 0, [Range(1, 100)] int count = 20, string? repoId = null)
{
var syncTasks = await repoSyncTaskService.GetSyncTasksAsync(offset, limit, repoId);
var syncTasks = await repoSyncTaskService.GetSyncTasksAsync(page, count, repoId);

return mapper.Map<SyncTaskPublic[]>(syncTasks);
var tasks = mapper.Map<SyncTaskPublic[]>(syncTasks.Items);

return new PageResult<SyncTaskPublic>(tasks, syncTasks.TotalCount);
}

[HttpPost("{repoId}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class VpmRepoController(
[ProducesResponseType<Dictionary<string, string>>(StatusCodes.Status200OK)]
public async Task<Dictionary<string, string>> GetRepoLists()
{
var repos = await repoMetaDataService.GetAllRepos();
var repos = await repoMetaDataService.GetRepos();
return repos.Select(repo => new KeyValuePair<string, string>(repo.Id, repo.UpStreamUrl)).ToDictionary();
}

Expand Down

0 comments on commit e88cc2f

Please sign in to comment.