import { Request , Response } from 'express' ; import CreatePostService from '@modules/posts/services/CreatePostService' ; import ShowPostService from '@modules/posts/services/ShowPostService' ; import UpdatePostService from '@modules/posts/services/UpdatePostService' ; import RemovePostService from '@modules/posts/services/RemovePostService' ; import { classToClass } from 'class-transformer' ; import { container } from 'tsyringe' ; class PostsController { public async show ( request , response ) { const { id } = request . params ; const showPost = container . resolve ( ShowPostService ) ; const post = await showPost . execute ( id ) ; return response . json ( classToClass ( post ) ) ; } public async create ( request , response ) { const { content , media } = request . body ; const userId = request . user . id ; const createPost = container . resolve ( CreatePostService ) ; const post = await createPost . execute ( { content , user_id : userId , ... ( request . file && { image : request . file . filename } ) , ... ( media && { media } ) , } ) ; return response . json ( classToClass ( post ) ) ; } public async update ( request , response ) { const updatePost = container . resolve ( UpdatePostService ) ; const post = await updatePost . execute ( { user_id : request . user . id , post_id : request . params . id , content : request . body . content , } ) ; return response . json ( classToClass ( post ) ) ; } public async remove ( request , response ) { const removePost = container . resolve ( RemovePostService ) ; await removePost . execute ( { user_id : request . user . id , post_id : request . params . id , } ) ; return response . status ( 204 ) . json ( { } ) ; } } export default PostsController ;