import * as express from 'express' ; import { TodosRepo , toObjectId } from './todos.repository' ; import { CONFIG } from './config' ; import { extractToken } from './utils' ; export class TodosRouter { repo : TodosRepo ; router : express . Router = express . Router ( ) ; serverConfig : CONFIG . IServerConfig ; middlewares : { ( req , res , next ) ; } [ ] = [ ] ; constructor ( datas ) { Object . assign ( this , datas ) ; } async getRoutes ( ) { const MIDDLEWARES = [ ... this . middlewares , ] ; return this . router . get ( '/test' , async ( req , res , next ) => { return res . status ( 200 ) . json ( { code : 200 , message : 'work!' } ) ; } ) . get ( '/' , MIDDLEWARES , async ( req , res , next ) => { const token = extractToken ( req ) ; const { _id = null } = ( ( < any > req ) . decoded || { } ) ; if ( ! _id ) return next ( { code : 401 , message : 'No user ID in Token' , stack : ( < any > req ) . decoded } ) ; const findItems = await this . repo . getAll ( _id ) . catch ( err => err ) ; if ( ! findItems . todos ) return next ( { code : 400 , message : '{TodosRouter} Failed to find toods' , stack : findItems } ) ; const { todos = [ ] } = findItems ; return res . status ( 200 ) . json ( { todos , token } ) ; } ) . get ( '/:id' , MIDDLEWARES , async ( req , res , next ) => { const token = extractToken ( req ) ; const { _id = null } = ( ( < any > req ) . decoded || { } ) ; if ( ! _id ) return next ( { code : 401 , message : 'No user ID in Token' , stack : ( < any > req ) . decoded } ) ; const { id = null } = req . params ; if ( ! id ) return next ( { code : 401 , message : 'No todo ID' , stack : ( < any > req ) . decoded } ) ; const findItems = await this . repo . getById ( { userID : _id , todoID : id } ) . catch ( err => err ) ; if ( ! findItems . todos ) return next ( { code : 400 , message : '{TodosRouter} Failed to find todos' , stack : findItems } ) ; const { todos = [ ] } = findItems ; if ( todos . length <= 0 ) return next ( { code : 404 , message : '{TodosRouter} Failed to find todo by ID. Unexisting Item.' } ) ; return res . status ( 200 ) . json ( { todos , token } ) ; } ) . post ( '/' , MIDDLEWARES , async ( req , res , next ) => { const token = extractToken ( req ) || null ; const { _id = null } = ( ( < any > req ) . decoded || { } ) ; if ( ! _id ) return next ( { code : 401 , message : 'No user ID in Token' , stack : ( < any > req ) . decoded } ) ; const newItem = await this . repo . create ( { ... req . body , uid : _id } ) . catch ( err => err ) ; if ( ! newItem . todo ) return next ( { code : 400 , message : '{TodosRouter} Todo creation failed' , stack : newItem } ) ; const { todo = null } = newItem ; res . status ( 200 ) . json ( { todo , token } ) ; } ) . put ( '/:id' , MIDDLEWARES , async ( req , res , next ) => { const token = extractToken ( req ) || null ; const { _id : userId = null } = ( ( < any > req ) . decoded || { } ) ; if ( ! userId ) return next ( { code : 401 , message : 'No user ID in Token' , stack : ( < any > req ) . decoded } ) ; const { id : itemId = null } = ( ( < any > req ) . params || { } ) ; if ( ! itemId ) return next ( { code : 401 , message : 'No item ID in request params' , stack : ( < any > req ) . params } ) ; const updateItem = await this . repo . update ( { ... req . body , _id : itemId , uid : userId } ) . catch ( err => err ) ; if ( ! updateItem . todo ) return next ( { code : 400 , message : '{TodosRouter} Todo creation failed' , stack : updateItem } ) ; const { todo = null } = updateItem ; res . status ( 200 ) . json ( { todo , token } ) ; } ) ; } }