Skip to content

Commit

Permalink
Added series sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
umerfaruk committed Dec 15, 2024
1 parent 2245558 commit c5e4ddf
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ public async Task DeleteSeries(int libraryId, int seriesId, CancellationToken ca
}
}

public async Task<Page<SeriesModel>> GetSeries(int libraryId, int pageNumber, int pageSize, CancellationToken cancellationToken)
public async Task<Page<SeriesModel>> GetSeries(int libraryId, int pageNumber, int pageSize, SeriesSortByType sortBy, SortDirection direction, CancellationToken cancellationToken)
{
using (var connection = _connectionProvider.GetLibraryConnection())
{
var sortByQuery = $"{GetSortByQuery(sortBy)}";
var sortDirection = direction == SortDirection.Descending ? "DESC" : "ASC";

var sql = @"SELECT s.Id, s.Name, s.Description, s.ImageId, f.FilePath AS ImageUrl,
(SELECT COUNT(*) FROM Book b WHERE b.SeriesId = s.Id) AS BookCount
FROM Series AS s
LEFT OUTER JOIN `File` f ON f.Id = s.ImageId
WHERE s.LibraryId = @LibraryId
ORDER BY s.Name
LIMIT @PageSize OFFSET @Offset";
WHERE s.LibraryId = @LibraryId " +
$" ORDER BY {sortByQuery} {sortDirection} " +
"LIMIT @PageSize OFFSET @Offset";
var command = new CommandDefinition(sql,
new
{
Expand All @@ -101,18 +104,21 @@ ORDER BY s.Name
}
}

public async Task<Page<SeriesModel>> FindSeries(int libraryId, string query, int pageNumber, int pageSize, CancellationToken cancellationToken)
public async Task<Page<SeriesModel>> FindSeries(int libraryId, string query, int pageNumber, int pageSize, SeriesSortByType sortBy, SortDirection direction, CancellationToken cancellationToken)
{
using (var connection = _connectionProvider.GetLibraryConnection())
{
var sortByQuery = $"{GetSortByQuery(sortBy)}";
var sortDirection = direction == SortDirection.Descending ? "DESC" : "ASC";

var sql = @"SELECT s.Id, s.Name, s.Description, s.ImageId, f.FilePath AS ImageUrl,
(SELECT COUNT(*) FROM Book b WHERE b.SeriesId = s.Id) AS BookCount
FROM Series AS s
LEFT OUTER JOIN `File` f ON f.Id = s.ImageId
WHERE s.LibraryId = @LibraryId
AND s.Name LIKE @Query
ORDER BY s.Name
LIMIT @PageSize OFFSET @Offset";
AND s.Name LIKE @Query " +
$" ORDER BY {sortByQuery} {sortDirection} " +
"LIMIT @PageSize OFFSET @Offset";
var command = new CommandDefinition(sql,
new
{
Expand Down Expand Up @@ -161,4 +167,17 @@ public async Task UpdateSeriesImage(int libraryId, int seriesId, long imageId, C
await connection.ExecuteScalarAsync<int>(command);
}
}

private static string GetSortByQuery(SeriesSortByType sortBy)
{
switch (sortBy)
{
case SeriesSortByType.Name:
return "s.`Name`";
case SeriesSortByType.BookCount:
return "BookCount";
default:
return "s.`Name`";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ public async Task DeleteSeries(int libraryId, int seriesId, CancellationToken ca
}
}

public async Task<Page<SeriesModel>> GetSeries(int libraryId, int pageNumber, int pageSize, CancellationToken cancellationToken)
public async Task<Page<SeriesModel>> GetSeries(int libraryId, int pageNumber, int pageSize, SeriesSortByType sortBy, SortDirection direction, CancellationToken cancellationToken)
{
using (var connection = _connectionProvider.GetLibraryConnection())
{
var sortByQuery = $"{GetSortByQuery(sortBy)}";
var sortDirection = direction == SortDirection.Descending ? "DESC" : "ASC";

var sql = @"SELECT s.Id, s.Name, s.[Description], s.ImageId, f.FilePath AS ImageUrl, (Select Count(*) From Book b Where b.SeriesId = s.Id) AS BookCount
FROM Series AS s
LEFT OUTER JOIN [File] f ON f.Id = s.ImageId
Where s.LibraryId = @LibraryId
Order By s.Name
OFFSET @PageSize * (@PageNumber - 1) ROWS
Where s.LibraryId = @LibraryId " +
$" ORDER BY {sortByQuery} {sortDirection} " +
@"OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY";
var command = new CommandDefinition(sql,
new { LibraryId = libraryId, PageSize = pageSize, PageNumber = pageNumber },
Expand All @@ -85,17 +88,20 @@ Order By s.Name
}
}

public async Task<Page<SeriesModel>> FindSeries(int libraryId, string query, int pageNumber, int pageSize, CancellationToken cancellationToken)
public async Task<Page<SeriesModel>> FindSeries(int libraryId, string query, int pageNumber, int pageSize, SeriesSortByType sortBy, SortDirection direction, CancellationToken cancellationToken)
{
using (var connection = _connectionProvider.GetLibraryConnection())
{
var sortByQuery = $"{GetSortByQuery(sortBy)}";
var sortDirection = direction == SortDirection.Descending ? "DESC" : "ASC";

var sql = @"SELECT s.Id, s.Name, s.[Description], s.ImageId, f.FilePath AS ImageUrl, (Select Count(*) From Book b Where b.SeriesId = s.Id) AS BookCount
FROM Series AS s
LEFT OUTER JOIN [File] f ON f.Id = s.ImageId
Where s.LibraryId = @LibraryId AND
s.Name LIKE @Query
Order By s.Name
OFFSET @PageSize * (@PageNumber - 1) ROWS
s.Name LIKE @Query " +
$" ORDER BY {sortByQuery} {sortDirection} " +
@"OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY";
var command = new CommandDefinition(sql,
new { LibraryId = libraryId, Query = $"%{query}%", PageSize = pageSize, PageNumber = pageNumber },
Expand Down Expand Up @@ -139,4 +145,17 @@ public async Task UpdateSeriesImage(int libraryId, int seriesId, long imageId, C
await connection.ExecuteScalarAsync<int>(command);
}
}

private static string GetSortByQuery(SeriesSortByType sortBy)
{
switch (sortBy)
{
case SeriesSortByType.Name:
return "s.`Name`";
case SeriesSortByType.BookCount:
return "BookCount";
default:
return "s.`Name`";
}
}
}
10 changes: 8 additions & 2 deletions src/Inshapardaz.Api/Controllers/SeriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Inshapardaz.Api.Extensions;
using Inshapardaz.Api.Mappings;
using Inshapardaz.Api.Views.Library;
using Inshapardaz.Domain.Models;
using Inshapardaz.Domain.Models.Library;
using Inshapardaz.Domain.Ports.Command.Library.Series;
using Inshapardaz.Domain.Ports.Query.Library.Series;
Expand Down Expand Up @@ -30,9 +31,14 @@ public SeriesController(IAmACommandProcessor commandProcessor,
}

[HttpGet("libraries/{libraryId}/series", Name = nameof(SeriesController.GetSeries))]
public async Task<IActionResult> GetSeries(int libraryId, string query, int pageNumber = 1, int pageSize = 10, CancellationToken token = default(CancellationToken))
public async Task<IActionResult> GetSeries(int libraryId, string query, int pageNumber = 1, int pageSize = 10, SeriesSortByType sortby = SeriesSortByType.Name, SortDirection sortDirection = SortDirection.Ascending, CancellationToken token = default(CancellationToken))
{
var seriesQuery = new GetSeriesQuery(libraryId, pageNumber, pageSize) { Query = query };
var seriesQuery = new GetSeriesQuery(libraryId, pageNumber, pageSize)
{
Query = query,
SortBy = sortby,
SortDirection = sortDirection
};
var series = await _queryProcessor.ExecuteAsync(seriesQuery, cancellationToken: token);

var args = new PageRendererArgs<SeriesModel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public interface ISeriesRepository

Task DeleteSeries(int libraryId, int seriesId, CancellationToken cancellationToken);

Task<Page<SeriesModel>> GetSeries(int libraryId, int pageNumber, int pageSize, CancellationToken cancellationToken);
Task<Page<SeriesModel>> GetSeries(int libraryId, int pageNumber, int pageSize, SeriesSortByType sortBy, SortDirection direction, CancellationToken cancellationToken);

Task<Page<SeriesModel>> FindSeries(int libraryId, string query, int pageNumber, int pageSize, CancellationToken cancellationToken);
Task<Page<SeriesModel>> FindSeries(int libraryId, string query, int pageNumber, int pageSize, SeriesSortByType sortBy, SortDirection direction, CancellationToken cancellationToken);

Task<SeriesModel> GetSeriesById(int libraryId, int seriesId, CancellationToken cancellationToken);

Expand Down
7 changes: 7 additions & 0 deletions src/Inshapardaz.Domain/Models/SeriesSortByType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Inshapardaz.Domain.Models;

public enum SeriesSortByType
{
Name,
BookCount
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public GetSeriesQuery(int libraryId, int pageNumber, int pageSize)
public int PageSize { get; private set; }

public string Query { get; set; }
public SeriesSortByType SortBy { get; set; }
public SortDirection SortDirection { get; set; }
}

public class GetSeriesQueryHandler : QueryHandlerAsync<GetSeriesQuery, Page<SeriesModel>>
Expand All @@ -40,8 +42,8 @@ public GetSeriesQueryHandler(ISeriesRepository seriesRepository, IFileRepository
public override async Task<Page<SeriesModel>> ExecuteAsync(GetSeriesQuery query, CancellationToken cancellationToken = new CancellationToken())
{
var series = string.IsNullOrWhiteSpace(query.Query)
? await _seriesRepository.GetSeries(query.LibraryId, query.PageNumber, query.PageSize, cancellationToken)
: await _seriesRepository.FindSeries(query.LibraryId, query.Query, query.PageNumber, query.PageSize, cancellationToken);
? await _seriesRepository.GetSeries(query.LibraryId, query.PageNumber, query.PageSize, query.SortBy, query.SortDirection, cancellationToken)
: await _seriesRepository.FindSeries(query.LibraryId, query.Query, query.PageNumber, query.PageSize, query.SortBy, query.SortDirection, cancellationToken);

foreach (var author in series.Data)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Inshapardaz.LibraryMigrator/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private async Task<Dictionary<int, int>> MigrateSeries(int libraryId, int newLib
var destinationDb = DestinationRepositoryFactory.SeriesRepository;

Dictionary<int, int> seriesMap = new Dictionary<int, int>();
var series = await sourceDb.GetSeries(libraryId, 1, int.MaxValue, cancellationToken);
var series = await sourceDb.GetSeries(libraryId, 1, int.MaxValue, SeriesSortByType.Name, SortDirection.Ascending, cancellationToken);
Console.WriteLine($"Started migration of {series.TotalCount} Series.");
int i = 0;

Expand Down

0 comments on commit c5e4ddf

Please sign in to comment.