import { Router , Request , Response } from 'express' ; import { FeedItem } from '../models/FeedItem' ; import { requireAuth } from '../../users/routes/auth.router' ; import * as AWS from '../../../../aws' ; const router = Router ( ) ; router . get ( '/' , async ( req , res ) => { const items = await FeedItem . findAndCountAll ( { order : [ [ 'id' , 'DESC' ] ] } ) ; items . rows . map ( ( item ) => { if ( item . url ) { item . url = AWS . getGetSignedUrl ( item . url ) ; } } ) ; res . send ( items ) ; } ) ; router . patch ( '/:id' , requireAuth , async ( req , res ) => { res . send ( 500 ) . send ( "not implemented" ) ; } ) ; router . get ( '/signed-url/:fileName' , requireAuth , async ( req , res ) => { let { fileName } = req . params ; const url = AWS . getPutSignedUrl ( fileName ) ; res . status ( 201 ) . send ( { url : url } ) ; } ) ; router . post ( '/' , requireAuth , async ( req , res ) => { const caption = req . body . caption ; const fileName = req . body . url ; if ( ! caption ) { return res . status ( 400 ) . send ( { message : 'Caption is required or malformed' } ) ; } if ( ! fileName ) { return res . status ( 400 ) . send ( { message : 'File url is required' } ) ; } const item = await new FeedItem ( { caption : caption , url : fileName } ) ; const saved_item = await item . save ( ) ; saved_item . url = AWS . getGetSignedUrl ( saved_item . url ) ; res . status ( 201 ) . send ( saved_item ) ; } ) ; export const FeedRouter = router ;