-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from bpatrik/feature/saved-search
Creating saved search as Albums #45
- Loading branch information
Showing
44 changed files
with
1,153 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {NextFunction, Request, Response} from 'express'; | ||
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error'; | ||
import {ObjectManagers} from '../model/ObjectManagers'; | ||
import {Utils} from '../../common/Utils'; | ||
import {Config} from '../../common/config/private/Config'; | ||
|
||
|
||
export class AlbumMWs { | ||
|
||
|
||
public static async listAlbums(req: Request, res: Response, next: NextFunction): Promise<void> { | ||
if (Config.Client.Album.enabled === false) { | ||
return next(); | ||
} | ||
try { | ||
req.resultPipe = await ObjectManagers.getInstance() | ||
.AlbumManager.getAlbums(); | ||
return next(); | ||
|
||
} catch (err) { | ||
return next(new ErrorDTO(ErrorCodes.ALBUM_ERROR, 'Error during listing albums', err)); | ||
} | ||
} | ||
|
||
|
||
public static async deleteAlbum(req: Request, res: Response, next: NextFunction): Promise<void> { | ||
if (Config.Client.Album.enabled === false) { | ||
return next(); | ||
} | ||
if (!req.params.id || !Utils.isUInt32(parseInt(req.params.id, 10))) { | ||
return next(); | ||
} | ||
try { | ||
await ObjectManagers.getInstance().AlbumManager.deleteAlbum(parseInt(req.params.id, 10)); | ||
req.resultPipe = 'ok'; | ||
return next(); | ||
|
||
} catch (err) { | ||
return next(new ErrorDTO(ErrorCodes.ALBUM_ERROR, 'Error during deleting albums', err)); | ||
} | ||
} | ||
|
||
public static async createSavedSearch(req: Request, res: Response, next: NextFunction): Promise<void> { | ||
if (Config.Client.Album.enabled === false) { | ||
return next(); | ||
} | ||
if ((typeof req.body === 'undefined') || (typeof req.body.name !== 'string') || (typeof req.body.searchQuery !== 'object')) { | ||
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'updateSharing filed is missing')); | ||
} | ||
try { | ||
await ObjectManagers.getInstance().AlbumManager.addSavedSearch(req.body.name, req.body.searchQuery); | ||
req.resultPipe = 'ok'; | ||
return next(); | ||
|
||
} catch (err) { | ||
return next(new ErrorDTO(ErrorCodes.ALBUM_ERROR, 'Error during creating saved search albums', err)); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {SearchQueryDTO} from '../../../../common/entities/SearchQueryDTO'; | ||
import {AlbumBaseDTO} from '../../../../common/entities/album/AlbumBaseDTO'; | ||
|
||
export interface IAlbumManager { | ||
/** | ||
* Creates a saved search type of album | ||
*/ | ||
addSavedSearch(name: string, searchQuery: SearchQueryDTO): Promise<void>; | ||
|
||
/** | ||
* Deletes an album | ||
*/ | ||
deleteAlbum(id: number): Promise<void>; | ||
|
||
/** | ||
* Returns with all albums | ||
*/ | ||
getAlbums(): Promise<AlbumBaseDTO[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {AlbumBaseDTO} from '../../../../common/entities/album/AlbumBaseDTO'; | ||
import {SearchQueryDTO} from '../../../../common/entities/SearchQueryDTO'; | ||
import {IAlbumManager} from '../interfaces/IAlbumManager'; | ||
|
||
export class AlbumManager implements IAlbumManager { | ||
|
||
public async addSavedSearch(name: string, searchQuery: SearchQueryDTO): Promise<void> { | ||
throw new Error('not supported by memory DB'); | ||
|
||
} | ||
|
||
public async deleteAlbum(id: number): Promise<void> { | ||
throw new Error('not supported by memory DB'); | ||
} | ||
|
||
public async getAlbums(): Promise<AlbumBaseDTO[]> { | ||
throw new Error('not supported by memory DB'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {SQLConnection} from './SQLConnection'; | ||
import {AlbumBaseEntity} from './enitites/album/AlbumBaseEntity'; | ||
import {AlbumBaseDTO} from '../../../../common/entities/album/AlbumBaseDTO'; | ||
import {SavedSearchDTO} from '../../../../common/entities/album/SavedSearchDTO'; | ||
import {ObjectManagers} from '../../ObjectManagers'; | ||
import {ISQLSearchManager} from './ISearchManager'; | ||
import {SearchQueryDTO} from '../../../../common/entities/SearchQueryDTO'; | ||
import {SavedSearchEntity} from './enitites/album/SavedSearchEntity'; | ||
import { IAlbumManager } from '../interfaces/IAlbumManager'; | ||
|
||
export class AlbumManager implements IAlbumManager{ | ||
private static async fillPreviewToAlbum(album: AlbumBaseDTO): Promise<void> { | ||
if (!(album as SavedSearchDTO).searchQuery) { | ||
throw new Error('no search query present'); | ||
} | ||
album.preview = await (ObjectManagers.getInstance().SearchManager as ISQLSearchManager) | ||
.getPreview((album as SavedSearchDTO).searchQuery); | ||
} | ||
|
||
public async addSavedSearch(name: string, searchQuery: SearchQueryDTO): Promise<void> { | ||
const connection = await SQLConnection.getConnection(); | ||
await connection.getRepository(SavedSearchEntity).insert({name, searchQuery}); | ||
|
||
} | ||
|
||
public async deleteAlbum(id: number): Promise<void> { | ||
const connection = await SQLConnection.getConnection(); | ||
await connection.getRepository(AlbumBaseEntity).delete({id}); | ||
} | ||
|
||
public async getAlbums(): Promise<AlbumBaseDTO[]> { | ||
const connection = await SQLConnection.getConnection(); | ||
const albums = await connection.getRepository(AlbumBaseEntity).find(); | ||
for (const a of albums) { | ||
await AlbumManager.fillPreviewToAlbum(a); | ||
} | ||
|
||
return albums; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {SearchQueryDTO, SearchQueryTypes} from '../../../../common/entities/SearchQueryDTO'; | ||
import {MediaDTO} from '../../../../common/entities/MediaDTO'; | ||
import {ISearchManager} from '../interfaces/ISearchManager'; | ||
import {AutoCompleteItem} from '../../../../common/entities/AutoCompleteItem'; | ||
import {SearchResultDTO} from '../../../../common/entities/SearchResultDTO'; | ||
import {PhotoDTO} from '../../../../common/entities/PhotoDTO'; | ||
|
||
export interface ISQLSearchManager extends ISearchManager { | ||
autocomplete(text: string, type: SearchQueryTypes): Promise<AutoCompleteItem[]>; | ||
|
||
search(query: SearchQueryDTO): Promise<SearchResultDTO>; | ||
|
||
getRandomPhoto(queryFilter: SearchQueryDTO): Promise<PhotoDTO>; | ||
|
||
// "Protected" functions. only called from other Managers, not from middlewares | ||
getPreview(query: SearchQueryDTO): Promise<MediaDTO>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/backend/model/database/sql/enitites/album/AlbumBaseEntity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {Column, Entity, Index, PrimaryGeneratedColumn, TableInheritance} from 'typeorm'; | ||
import {MediaEntity} from '../MediaEntity'; | ||
import {columnCharsetCS} from '../EntityUtils'; | ||
import {AlbumBaseDTO} from '../../../../../../common/entities/album/AlbumBaseDTO'; | ||
|
||
@Entity() | ||
@TableInheritance({column: {type: 'varchar', name: 'type', length: 24}}) | ||
export class AlbumBaseEntity implements AlbumBaseDTO { | ||
|
||
@Index() | ||
@PrimaryGeneratedColumn({unsigned: true}) | ||
id: number; | ||
|
||
@Index() | ||
@Column(columnCharsetCS) | ||
name: string; | ||
|
||
// not saving to database, it is only assigned when querying the DB | ||
public preview: MediaEntity; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/backend/model/database/sql/enitites/album/SavedSearchEntity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {ChildEntity, Column} from 'typeorm'; | ||
import {AlbumBaseEntity} from './AlbumBaseEntity'; | ||
import {SavedSearchDTO} from '../../../../../../common/entities/album/SavedSearchDTO'; | ||
import {SearchQueryDTO} from '../../../../../../common/entities/SearchQueryDTO'; | ||
|
||
@ChildEntity() | ||
export class SavedSearchEntity extends AlbumBaseEntity implements SavedSearchDTO { | ||
@Column({ | ||
type: 'text', | ||
nullable: false, | ||
transformer: { | ||
// used to deserialize your data from db field value | ||
from: (val: string) => { | ||
return JSON.parse(val); | ||
}, | ||
// used to serialize your data to db field | ||
to: (val: object) => { | ||
return JSON.stringify(val); | ||
} | ||
} | ||
}) | ||
searchQuery: SearchQueryDTO; | ||
} |
Oops, something went wrong.