import { ArticlesService } from './articles.service'; import { Router, Request, Response, NextFunction } from 'express'; import initLogger, { LoggerContext } from "jsm-logger"; import Container from 'typedi'; import { respond, getAuthData } from 'jsm-utilities'; /** * Logger instance */ const logger = initLogger(LoggerContext.CONTROLLER, 'articles'); /** * @module ArticlesController * @description Articles management service controller * @param {Router} router * @returns {void} */ export default (router: Router) => { logger.event('Loading module controller'); const service = Container.get(ArticlesService); // Loop through each API method and create the corresponding route handler /** * @function Create * @description Create a new article * @param {Request} req * @param {Response} res * @param {NextFunction} next */ router.post( '/', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.Create.Response; const body: Levelup.Core.Content.Api.Articles.Create.Request = req.body; const response = await service.create( body, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); /** * @function Update * @description Update an article * @param {Request} req * @param {Response} res * @param {NextFunction} next */ router.put( '/:id', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.Update.Response; const { id } = req.params as { id: string }; const body: Levelup.Core.Content.Api.Articles.Update.Request = req.body; const response = await service.update( id, body, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); /** * @function Delete * @description Delete an article * @param {Request} req * @param {Response} res * @param {NextFunction} next */ router.delete( '/:id', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.Delete.Response; const { id } = req.params as { id: string }; const query: Levelup.Core.Content.Api.Articles.Delete.Request = req.query; const response = await service.delete( id, query, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); /** * @function GetOne * @description Get an article * @param {Request} req * @param {Response} res * @param {NextFunction} next */ router.get( '/:id', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.GetOne.Response; const { id } = req.params as { id: string }; const query: Levelup.Core.Content.Api.Articles.GetOne.Request = req.query; const response = await service.getById( id, query, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); router.get( '/by-slug/:slug', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.GetOne.Response; const { slug } = req.params as { slug: string }; const query: Levelup.Core.Content.Api.Articles.GetOne.Request = req.query; const response = await service.getBySlug( slug, query, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); /** * @function List * @description List articles * @param {Request} req * @param {Response} res * @param {NextFunction} next */ router.get( '/', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.List.Response; const query: Levelup.Core.Content.Api.Articles.List.Request = req.query; const response = await service.list( query, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); /** * @function AggregateByTypes * @description Aggregate articles by types * @param {Request} req * @param {Response} res * @param {NextFunction} next */ router.get( '/aggregate-by-types', async (req: Request, res: Response, next: NextFunction) => { try { /** * Always get the auth data at the beginning of the function */ const AUTH_DATA = await getAuthData(req); type R = Levelup.Core.Content.Api.Articles.AggregateByTypes.Response; const query: Levelup.Core.Content.Api.Articles.AggregateByTypes.Request = req.query; const response = await service.aggregateByTypes( query, AUTH_DATA ); respond(res, response); } catch (error) { /** * Pass the error to the next middleware * the error logging logic is handled on the service layer */ next(error); } } ); };