SHARE
TWEET

Untitled

a guest Sep 7th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { NextFunction, Request, Response }   from 'express-serve-static-core';
  2. import { DocumentQuery, Model, SaveOptions } from 'mongoose';
  3. import { InstanceType }                      from 'typegoose';
  4. import { Category, CategoryModel }           from '../models/MinnichSite/category.model';
  5. import { ApiRoutes }                         from './api.routes';
  6.  
  7.  
  8. export class MinnichSiteCrudRouter<T> extends ApiRoutes {
  9.  
  10.   constructor( public routerParam: string = null, public mongooseModel: Model<InstanceType<T>> = null, public dataModel: string = null ) {
  11.     super();
  12.  
  13.     this._router.route( '/' )
  14.       .get( this.getCategory.bind( this ) )
  15.       .get( this.where.bind( this ) )
  16.       .get( this.getData.bind( this ) )
  17.       .get( this.populate.bind( this ) )
  18.       .get( this.get.bind( this ) )
  19.  
  20.       .post( this.post.bind( this ) )
  21.       .post( this.populateSave.bind( this ) )
  22.       .post( this.get.bind( this ) );
  23.  
  24.  
  25.     this._router.route( `/:${this.routerParam}` )
  26.       .get( this.populate.bind( this ) )
  27.       .get( this.get.bind( this ) )
  28.       .put( this.put.bind( this ) )
  29.       .put( this.populateSave.bind( this ) )
  30.       .put( this.get.bind( this ) )
  31.       .delete( this.delete.bind( this ) );
  32.  
  33.  
  34.     this._router.param( this.routerParam, ( req, res, next ) => {
  35.       res.locals[ this.dataModel ] = this.mongooseModel.findById( req.params[ this.routerParam ] );
  36.       return next();
  37.     } );
  38.  
  39.   }
  40.  
  41.  
  42.   public getCategory( req: Request, res: Response, next: NextFunction ) {
  43.     res.locals.searchParams = res.locals.searchParams || [];
  44.     if ( req.query.category === undefined ) {
  45.       res.locals.searchParams.push( {} );
  46.       return next();
  47.     }
  48.  
  49.     CategoryModel.findOne( {
  50.       categoryType: 'video',
  51.       name        : new RegExp( [ '^', req.query.category.trim().toLowerCase(), '$' ].join( '' ), 'i' ),
  52.     } ).then( ( category: InstanceType<Category> ) => {
  53.       res.locals.searchParams.push( { category: category } );
  54.       return next();
  55.     } );
  56.  
  57.   }
  58.  
  59.   public where( req: Request, res: Response, next: NextFunction ) {
  60.     res.locals.searchParams = res.locals.searchParams || [];
  61.     if ( req.query.where !== undefined ) {
  62.       Object.keys( req.query.where ).some( item => {
  63.  
  64.         try {
  65.           if ( JSON.parse( req.query.where[ item ] ) ) {
  66.             req.query.where[ item ] = JSON.parse( req.query.where[ item ] );
  67.             return;
  68.           }
  69.         } catch ( e ) { }
  70.  
  71.         if ( req.query.where[ item ] === 'null' ) {
  72.           req.query.where[ item ] = null;
  73.         }
  74.  
  75.       } );
  76.       res.locals.searchParams.push( req.query.where );
  77.     }
  78.     next();
  79.   }
  80.  
  81.   public getData( req: Request, res: Response, next: NextFunction ) {
  82.     let newSearchParams = {};
  83.     ( res.locals.searchParams || [] ).forEach( ( data: object ) => {
  84.       newSearchParams = { ...newSearchParams, ...data };
  85.     } );
  86.     console.log( newSearchParams );
  87.     res.locals[ this.dataModel ] = this.mongooseModel.find( newSearchParams );
  88.     next();
  89.   }
  90.  
  91.  
  92.   public populate( req: Request, res: Response, next: NextFunction ) {
  93.     const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
  94.     ( req.query.pop || req.query.populate || [] ).forEach( ( dot: string ) => object.populate( dot ) );
  95.     res.locals[ this.dataModel ] = object;
  96.     return next();
  97.   }
  98.  
  99.   public populateSave( req: Request, res: Response, next: NextFunction ) {
  100.     let object: InstanceType<T> = res.locals[ this.dataModel ];
  101.     ( req.query.pop || req.query.populate || [] ).forEach( ( dot: string ) => object.populate( dot ) );
  102.     res.locals[ this.dataModel ] = object.execPopulate();
  103.     return next();
  104.   }
  105.  
  106.   public get( req: Request, res: Response, next: NextFunction ) {
  107.     const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
  108.     object.then( obj => res.json( obj ) )
  109.       .catch( error => res.status( 400 ).json( error ) );
  110.   }
  111.  
  112.  
  113.   public post( req: Request, res: Response, next: NextFunction ) {
  114.     const newObject = new this.mongooseModel( req.body );
  115.     newObject.save( <SaveOptions>{ validateBeforeSave: false } )
  116.       .then( object => {
  117.         res.locals[ this.dataModel ] = object;
  118.         return next();
  119.       } )
  120.       .catch( error => {
  121.         res.status( 400 ).json( error );
  122.       } );
  123.   }
  124.  
  125.   public put( req: Request, res: Response, next: NextFunction ) {
  126.     const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
  127.  
  128.     object.then( returnedObject => {
  129.       returnedObject
  130.         .set( req.body )
  131.         .save()
  132.         .then( savedObject => {
  133.           console.log( savedObject );
  134.           res.locals[ this.dataModel ] = savedObject;
  135.           next();
  136.  
  137.         } );
  138.  
  139.     } ).catch( error => {
  140.       console.log( error );
  141.       next();
  142.     } );
  143.  
  144.   }
  145.  
  146.   public delete( req: Request, res: Response, next: NextFunction ) {
  147.     const object: DocumentQuery<InstanceType<T>, InstanceType<T>> = res.locals[ this.dataModel ];
  148.     object.then( returnedObject => {
  149.       returnedObject.remove()
  150.         .then( obj => {
  151.           res.json( obj );
  152.           next();
  153.         } )
  154.         .catch( error => res.status( 400 ).json( error ) );
  155.     } )
  156.       .catch( error => res.status( 400 ).json( error ) );
  157.   }
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top