1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-02 09:12:07 +02:00

fixing preview manager ordering

This commit is contained in:
Patrik J. Braun 2022-02-02 00:24:16 +01:00
parent 0cd5e90866
commit 6c6d77ad34
2 changed files with 19 additions and 10 deletions

View File

@ -36,7 +36,7 @@ export class PreviewManager implements IPreviewManager {
query.addOrderBy('media.rating', 'ASC'); query.addOrderBy('media.rating', 'ASC');
break; break;
case SortingMethods.descName: case SortingMethods.descName:
query.addOrderBy('media.name', 'ASC'); query.addOrderBy('media.name', 'DESC');
break; break;
case SortingMethods.ascName: case SortingMethods.ascName:
query.addOrderBy('media.name', 'ASC'); query.addOrderBy('media.name', 'ASC');
@ -94,10 +94,10 @@ export class PreviewManager implements IPreviewManager {
public async getAlbumPreview(album: { searchQuery: SearchQueryDTO }): Promise<PreviewPhotoDTOWithID> { public async getAlbumPreview(album: { searchQuery: SearchQueryDTO }): Promise<PreviewPhotoDTOWithID> {
const albumQuery = await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager).prepareAndBuildWhereQuery(album.searchQuery); const albumQuery: Brackets = await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager).prepareAndBuildWhereQuery(album.searchQuery);
const connection = await SQLConnection.getConnection(); const connection = await SQLConnection.getConnection();
const previewQuery = async (): Promise<SelectQueryBuilder<MediaEntity>> => { const previewQuery = (): SelectQueryBuilder<MediaEntity> => {
const query = connection const query = connection
.getRepository(MediaEntity) .getRepository(MediaEntity)
.createQueryBuilder('media') .createQueryBuilder('media')
@ -111,7 +111,7 @@ export class PreviewManager implements IPreviewManager {
let previewMedia = null; let previewMedia = null;
if (Config.Server.Preview.SearchQuery && if (Config.Server.Preview.SearchQuery &&
!Utils.equalsFilter(Config.Server.Preview.SearchQuery, {type: SearchQueryTypes.any_text, text: ''} as TextSearch)) { !Utils.equalsFilter(Config.Server.Preview.SearchQuery, {type: SearchQueryTypes.any_text, text: ''} as TextSearch)) {
previewMedia = await (await previewQuery()) previewMedia = await previewQuery()
.andWhere(await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager) .andWhere(await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager)
.prepareAndBuildWhereQuery(Config.Server.Preview.SearchQuery)) .prepareAndBuildWhereQuery(Config.Server.Preview.SearchQuery))
.limit(1) .limit(1)
@ -119,7 +119,7 @@ export class PreviewManager implements IPreviewManager {
} }
if (!previewMedia) { if (!previewMedia) {
previewMedia = await (await previewQuery()) previewMedia = await previewQuery()
.limit(1) .limit(1)
.getOne(); .getOne();
} }

View File

@ -154,6 +154,9 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => {
after(async () => { after(async () => {
await sqlHelper.clearDB(); await sqlHelper.clearDB();
});
afterEach(() => {
Config.Server.Preview.SearchQuery = null; Config.Server.Preview.SearchQuery = null;
Config.Server.Preview.Sorting = [SortingMethods.descRating, SortingMethods.descDate]; Config.Server.Preview.Sorting = [SortingMethods.descRating, SortingMethods.descDate];
}); });
@ -218,10 +221,16 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => {
text: 'sw' text: 'sw'
} as TextSearch } as TextSearch
}))).to.deep.equalInAnyOrder(previewifyMedia(p2)); }))).to.deep.equalInAnyOrder(previewifyMedia(p2));
// Having a preview search query that does not return valid result
Config.Server.Preview.SearchQuery = {type: SearchQueryTypes.any_text, text: 'wont find it'} as TextSearch;
expect(Utils.clone(await pm.getAlbumPreview({
searchQuery: {
type: SearchQueryTypes.any_text,
text: 'Derem'
} as TextSearch
}))).to.deep.equalInAnyOrder(previewifyMedia(p2));
}); });
it('should invalidate and update preview', async () => { it('should invalidate and update preview', async () => {
const gm = new GalleryManagerTest(); const gm = new GalleryManagerTest();
const pm = new PreviewManager(); const pm = new PreviewManager();
@ -240,14 +249,14 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => {
let subdir = await selectDir(); let subdir = await selectDir();
expect(subdir.validPreview).to.equal(true); expect(subdir.validPreview).to.equal(true);
expect(subdir.preview.id).to.equal(2); expect(subdir.preview.id).to.equal(p2.id);
// new version should invalidate // new version should invalidate
await pm.onNewDataVersion(subDir as ParentDirectoryDTO); await pm.onNewDataVersion(subDir as ParentDirectoryDTO);
subdir = await selectDir(); subdir = await selectDir();
expect(subdir.validPreview).to.equal(false); expect(subdir.validPreview).to.equal(false);
// during invalidation, we do not remove the previous preview (it's good to show at least some photo) // during invalidation, we do not remove the previous preview (it's good to show at least some photo)
expect(subdir.preview.id).to.equal(2); expect(subdir.preview.id).to.equal(p2.id);
await conn.createQueryBuilder() await conn.createQueryBuilder()
.update(DirectoryEntity) .update(DirectoryEntity)
@ -258,7 +267,7 @@ describe('PreviewManager', (sqlHelper: DBTestHelper) => {
await gm.fillParentDir(conn, res); await gm.fillParentDir(conn, res);
subdir = await selectDir(); subdir = await selectDir();
expect(subdir.validPreview).to.equal(true); expect(subdir.validPreview).to.equal(true);
expect(subdir.preview.id).to.equal(2); expect(subdir.preview.id).to.equal(p2.id);
}); });