import logger from "../../utilities/Logger" ; import { Application , Request , Response } from "express" ; import { PostData , GetPostCallBack } from "../../../../cshub-shared/src/api-calls" ; import { getRepository } from "typeorm" ; import { Post } from "../../db/entities/post" ; export function registerPostDataEndpoint ( app ) { app . get ( PostData . getURL , ( req , res ) => { const hash = Number ( req . params . hash ) ; getPostData ( hash ) . then ( ( data ) => { if ( data === null ) { res . status ( 404 ) . send ( ) ; } else { res . json ( data ) ; } } ) ; } ) ; } export const getPostData = ( postHash ) : Promise < GetPostCallBack | null > => { const postRepository = getRepository ( Post ) ; return postRepository . findOne ( { relations : [ "topic" ] , where : { hash : postHash , } , } ) . then ( ( post ) => { if ( ! post ) { return null ; } return new GetPostCallBack ( post ) ; } ) . catch ( ( err ) => { logger . error ( `Retreiving post data failed` ) ; logger . error ( err ) ; return null ; } ) ; } ;