import { Request, Response, NextFunction } from 'express';
import { userService } from '../services/userService';

export const userController = {

  getAll(_req: Request, res: Response): void {
    res.json(userService.getAll());
  },

  getById(req: Request, res: Response, next: NextFunction): void {
    try {
      const user = userService.getById(Number(req.params.id));
      res.json(user);
    } catch (err) { next(err); }
  },

  create(req: Request, res: Response, next: NextFunction): void {
    try {
      const user = userService.create(req.body);
      res.status(201).json(user);
    } catch (err) { next(err); }
  },

  update(req: Request, res: Response, next: NextFunction): void {
    try {
      const user = userService.update(Number(req.params.id), req.body);
      res.json(user);
    } catch (err) { next(err); }
  },

  delete(req: Request, res: Response, next: NextFunction): void {
    try {
      userService.delete(Number(req.params.id));
      res.status(204).send();
    } catch (err) { next(err); }
  },

};