Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { NextFunction, Request, Response } from 'express-serve-static-core';
- import { DocumentQuery, Model, SaveOptions } from 'mongoose';
- import { InstanceType } from 'typegoose';
- import { Category, CategoryModel } from '../models/MinnichSite/category.model';
- import { ApiRoutes } from './api.routes';
- export class MinnichSiteCrudRouter<T> extends ApiRoutes {
- constructor( public routerParam: string = null, public mongooseModel: Model<InstanceType<T>> = null, public dataModel: string = null ) {
- super();
- this._router.route( '/' )
- .get( this.getCategory.bind( this ) )
- .get( this.where.bind( this ) )
- .get( this.getData.bind( this ) )
- .get( this.populate.bind( this ) )
- .get( this.get.bind( this ) )
- .post( this.post.bind( this ) )
- .post( this.populateSave.bind( this ) )
- .post( this.get.bind( this ) );
- this._router.route( `/:${this.routerParam}` )
- .get( this.populate.bind( this ) )
- .get( this.get.bind( this ) )
- .put( this.put.bind( this ) )
- .put( this.populateSave.bind( this ) )
- .put( this.get.bind( this ) )
- .delete( this.delete.bind( this ) );
- this._router.param( this.routerParam, ( req, res, next ) => {
- res.locals[ this.dataModel ] = this.mongooseModel.findById( req.params[ this.routerParam ] );
- return next();
- } );
- }
- public getCategory( req: Request, res: Response, next: NextFunction ) {
- res.locals.searchParams = res.locals.searchParams || [];
- if ( req.query.category === undefined ) {
- res.locals.searchParams.push( {} );
- return next();
- }
- CategoryModel.findOne( {
- categoryType: 'video',
- name : new RegExp( [ '^', req.query.category.trim().toLowerCase(), '$' ].join( '' ), 'i' ),
- } ).then( ( category: InstanceType<Category> ) => {
- res.locals.searchParams.push( { category: category } );
- return next();
- } );
- }
- public where( req: Request, res: Response, next: NextFunction ) {
- res.locals.searchParams = res.locals.searchParams || [];
- if ( req.query.where !== undefined ) {
- Object.keys( req.query.where ).some( item => {
- try {
- if ( JSON.parse( req.query.where[ item ] ) ) {
- req.query.where[ item ] = JSON.parse( req.query.where[ item ] );
- return;
- }
- } catch ( e ) { }
- if ( req.query.where[ item ] === 'null' ) {
- req.query.where[ item ] = null;
- }
- } );
- res.locals.searchParams.push( req.query.where );
- }
- next();
- }
- public getData( req: Request, res: Response, next: NextFunction ) {
- let newSearchParams = {};
- ( res.locals.searchParams || [] ).forEach( ( data: object ) => {
- newSearchParams = { ...newSearchParams, ...data };
- } );
- console.log( newSearchParams );
- res.locals[ this.dataModel ] = this.mongooseModel.find( newSearchParams );
- next();
- }
- public populate( req: Request, res: Response, next: NextFunction ) {
- const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
- ( req.query.pop || req.query.populate || [] ).forEach( ( dot: string ) => object.populate( dot ) );
- res.locals[ this.dataModel ] = object;
- return next();
- }
- public populateSave( req: Request, res: Response, next: NextFunction ) {
- let object: InstanceType<T> = res.locals[ this.dataModel ];
- ( req.query.pop || req.query.populate || [] ).forEach( ( dot: string ) => object.populate( dot ) );
- res.locals[ this.dataModel ] = object.execPopulate();
- return next();
- }
- public get( req: Request, res: Response, next: NextFunction ) {
- const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
- object.then( obj => res.json( obj ) )
- .catch( error => res.status( 400 ).json( error ) );
- }
- public post( req: Request, res: Response, next: NextFunction ) {
- const newObject = new this.mongooseModel( req.body );
- newObject.save( <SaveOptions>{ validateBeforeSave: false } )
- .then( object => {
- res.locals[ this.dataModel ] = object;
- return next();
- } )
- .catch( error => {
- res.status( 400 ).json( error );
- } );
- }
- public put( req: Request, res: Response, next: NextFunction ) {
- const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
- object.then( returnedObject => {
- returnedObject
- .set( req.body )
- .save()
- .then( savedObject => {
- console.log( savedObject );
- res.locals[ this.dataModel ] = savedObject;
- next();
- } );
- } ).catch( error => {
- console.log( error );
- next();
- } );
- }
- public delete( req: Request, res: Response, next: NextFunction ) {
- const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
- object.then( returnedObject => {
- returnedObject.remove()
- .then( obj => {
- res.json( obj );
- next();
- } )
- .catch( error => res.status( 400 ).json( error ) );
- } )
- .catch( error => res.status( 400 ).json( error ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement