1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

Adding Album Middlewares and routers #45

This commit is contained in:
Patrik J. Braun 2021-05-28 19:46:22 +02:00
parent 512f5c18d6
commit 6a08cc1c1c
11 changed files with 206 additions and 44 deletions

View File

@ -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));
}
}
}

View File

@ -117,6 +117,7 @@ export class SharingMWs {
try {
req.resultPipe = await ObjectManagers.getInstance().SharingManager.deleteSharing(sharingKey);
req.resultPipe = 'ok';
return next();
} catch (err) {
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during deleting sharing', err));

View File

@ -9,6 +9,8 @@ import {IPersonManager} from './database/interfaces/IPersonManager';
import {IVersionManager} from './database/interfaces/IVersionManager';
import {IJobManager} from './database/interfaces/IJobManager';
import {LocationManager} from './database/LocationManager';
import {IAlbumManager} from './database/interfaces/IAlbumManager';
import {JobManager} from './jobs/JobManager';
const LOG_TAG = '[ObjectManagers]';
@ -25,6 +27,7 @@ export class ObjectManagers {
private versionManager: IVersionManager;
private jobManager: IJobManager;
private locationManager: LocationManager;
private albumManager: IAlbumManager;
get VersionManager(): IVersionManager {
@ -43,6 +46,14 @@ export class ObjectManagers {
this.locationManager = value;
}
get AlbumManager(): IAlbumManager {
return this.albumManager;
}
set AlbumManager(value: IAlbumManager) {
this.albumManager = value;
}
get PersonManager(): IPersonManager {
return this.personManager;
}
@ -121,51 +132,30 @@ export class ObjectManagers {
}
public static async InitCommonManagers(): Promise<void> {
const JobManager = require('./jobs/JobManager').JobManager;
ObjectManagers.getInstance().JobManager = new JobManager();
}
public static async InitMemoryManagers(): Promise<void> {
await ObjectManagers.reset();
const GalleryManager = require('./database/memory/GalleryManager').GalleryManager;
const UserManager = require('./database/memory/UserManager').UserManager;
const SearchManager = require('./database/memory/SearchManager').SearchManager;
const SharingManager = require('./database/memory/SharingManager').SharingManager;
const IndexingManager = require('./database/memory/IndexingManager').IndexingManager;
const PersonManager = require('./database/memory/PersonManager').PersonManager;
const VersionManager = require('./database/memory/VersionManager').VersionManager;
ObjectManagers.getInstance().GalleryManager = new GalleryManager();
ObjectManagers.getInstance().UserManager = new UserManager();
ObjectManagers.getInstance().SearchManager = new SearchManager();
ObjectManagers.getInstance().SharingManager = new SharingManager();
ObjectManagers.getInstance().IndexingManager = new IndexingManager();
ObjectManagers.getInstance().PersonManager = new PersonManager();
ObjectManagers.getInstance().VersionManager = new VersionManager();
ObjectManagers.getInstance().LocationManager = new LocationManager();
this.InitCommonManagers();
this.initManagers('memory');
Logger.debug(LOG_TAG, 'Memory DB inited');
}
public static async InitSQLManagers(): Promise<void> {
await ObjectManagers.reset();
await SQLConnection.init();
const GalleryManager = require('./database/sql/GalleryManager').GalleryManager;
const UserManager = require('./database/sql/UserManager').UserManager;
const SearchManager = require('./database/sql/SearchManager').SearchManager;
const SharingManager = require('./database/sql/SharingManager').SharingManager;
const IndexingManager = require('./database/sql/IndexingManager').IndexingManager;
const PersonManager = require('./database/sql/PersonManager').PersonManager;
const VersionManager = require('./database/sql/VersionManager').VersionManager;
ObjectManagers.getInstance().GalleryManager = new GalleryManager();
ObjectManagers.getInstance().UserManager = new UserManager();
ObjectManagers.getInstance().SearchManager = new SearchManager();
ObjectManagers.getInstance().SharingManager = new SharingManager();
ObjectManagers.getInstance().IndexingManager = new IndexingManager();
ObjectManagers.getInstance().PersonManager = new PersonManager();
ObjectManagers.getInstance().VersionManager = new VersionManager();
ObjectManagers.getInstance().LocationManager = new LocationManager();
this.InitCommonManagers();
this.initManagers('sql');
Logger.debug(LOG_TAG, 'SQL DB inited');
}
private static initManagers(type: 'memory' | 'sql'): void {
ObjectManagers.getInstance().AlbumManager = new (require(`./database/${type}/AlbumManager`).AlbumManager)();
ObjectManagers.getInstance().GalleryManager = new (require(`./database/${type}/GalleryManager`).GalleryManager)();
ObjectManagers.getInstance().IndexingManager = new (require(`./database/${type}/IndexingManager`).IndexingManager)();
ObjectManagers.getInstance().PersonManager = new (require(`./database/${type}/PersonManager`).PersonManager)();
ObjectManagers.getInstance().SearchManager = new (require(`./database/${type}/SearchManager`).SearchManager)();
ObjectManagers.getInstance().SharingManager = new (require(`./database/${type}/SharingManager`).SharingManager)();
ObjectManagers.getInstance().UserManager = new (require(`./database/${type}/UserManager`).UserManager)();
ObjectManagers.getInstance().VersionManager = new (require(`./database/${type}/VersionManager`).VersionManager)();
ObjectManagers.getInstance().JobManager = new JobManager();
ObjectManagers.getInstance().LocationManager = new LocationManager();
}
}

View File

@ -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[]>;
}

View File

@ -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');
}
}

View File

@ -6,8 +6,9 @@ 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 {
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');

View File

@ -0,0 +1,56 @@
import {AuthenticationMWs} from '../middlewares/user/AuthenticationMWs';
import {Express} from 'express';
import {RenderingMWs} from '../middlewares/RenderingMWs';
import {UserRoles} from '../../common/entities/UserDTO';
import {VersionMWs} from '../middlewares/VersionMWs';
import {AlbumMWs} from '../middlewares/AlbumMWs';
export class AlbumRouter {
public static route(app: Express): void {
this.addListAlbums(app);
this.addAddSavedSearch(app);
this.addDeleteAlbum(app);
}
private static addListAlbums(app: Express): void {
app.get(['/api/album'],
// common part
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.User),
VersionMWs.injectGalleryVersion,
// specific part
AlbumMWs.listAlbums,
RenderingMWs.renderResult
);
}
private static addDeleteAlbum(app: Express): void {
app.delete(['/api/album/:id'],
// common part
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
VersionMWs.injectGalleryVersion,
// specific part
AlbumMWs.deleteAlbum,
RenderingMWs.renderResult
);
}
private static addAddSavedSearch(app: Express): void {
app.put(['/api/album/saved-search'],
// common part
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
VersionMWs.injectGalleryVersion,
// specific part
AlbumMWs.createSavedSearch,
RenderingMWs.renderResult
);
}
}

View File

@ -8,6 +8,7 @@ import {AdminRouter} from './admin/AdminRouter';
import {SettingsRouter} from './admin/SettingsRouter';
import {NotificationRouter} from './NotificationRouter';
import {ErrorRouter} from './ErrorRouter';
import {AlbumRouter} from './AlbumRouter';
export class Router {
@ -15,13 +16,14 @@ export class Router {
PublicRouter.route(app);
UserRouter.route(app);
GalleryRouter.route(app);
PersonRouter.route(app);
SharingRouter.route(app);
AdminRouter.route(app);
SettingsRouter.route(app);
AlbumRouter.route(app);
GalleryRouter.route(app);
NotificationRouter.route(app);
PersonRouter.route(app);
SettingsRouter.route(app);
SharingRouter.route(app);
UserRouter.route(app);
ErrorRouter.route(app);
}

View File

@ -57,7 +57,7 @@ export class SharingRouter {
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
SharingMWs.deleteSharing,
RenderingMWs.renderOK
RenderingMWs.renderResult
);
}

View File

@ -35,6 +35,13 @@ export class ClientSearchConfig {
maxDirectoryResult: number = 200;
}
@SubConfigClass()
export class ClientAlbumConfig {
@ConfigProperty()
enabled: boolean = true;
}
@SubConfigClass()
export class ClientSharingConfig {
@ConfigProperty()
@ -172,6 +179,8 @@ export class ClientConfig {
@ConfigProperty()
Sharing: ClientSharingConfig = new ClientSharingConfig();
@ConfigProperty()
Album: ClientAlbumConfig = new ClientAlbumConfig();
@ConfigProperty()
Map: ClientMapConfig = new ClientMapConfig();
@ConfigProperty()
RandomPhoto: ClientRandomPhotoConfig = new ClientRandomPhotoConfig();

View File

@ -24,6 +24,8 @@ export enum ErrorCodes {
TASK_ERROR = 14,
JOB_ERROR = 15,
LocationLookUp_ERROR = 16,
ALBUM_ERROR = 17,
}
export class ErrorDTO {