import {Response, Request} from 'express'
import UserService from '../service/UserService'

export default class CardController {

    constructor(
        private userService : UserService,
    ) {}

    async getAll(req: Request, res: Response) {

        try{
            res.send(await this.userService.getAll())
        } catch(error){
            return res.status(501).json({ error: error.toString() });
        }
        res.end()
    }

    async getById(req: Request, res: Response) {
        try{
            res.send(await this.userService.get(req.params.id))
        } catch(error){
            return res.status(501).json({ error: error.toString() });
        }
        res.end()
    }

    async create(req: Request, res: Response) {
        try{
            res.send(await this.userService.create(req.body))
        } catch(error){
            return res.status(501).json({ error: error.toString() });
        }
        res.end()
    }

    async update(req: Request, res: Response) {
        try{
            res.send(await this.userService.update(req.body))
        } catch(error){
            return res.status(501).json({ error: error.toString() });
        }
        res.end()
    }

    async delete(req: Request, res: Response) {
        try{
            let file = await this.userService.get(req.params.id)
            res.send(await this.userService.delete(req.params.id))
        } catch(error){
            return res.status(501).json({ error: error.toString() });
        }
        res.end()
    }
}
