import PlaylistService from '../services/PlaylistService.js'; import {validationResult} from 'express-validator'; import ApiError from './ErrorController.js'; class PlaylistController { async create(req, res, next) { try { const errors = validationResult(req); if(!errors.isEmpty()) { return next(ApiError.BadRequest('Ошибка при валидации', errors.array())); } const { name } = req.body; const playlist = await PlaylistService.create({name}); return res.json(playlist); } catch (e) { next(e); } } async getAll(req, res, next) { try { const {page, search} = req.query; const playlists = await PlaylistService.getAll({page, search}); res.set('Access-Control-Expose-Headers', 'X-total-count'); res.set('X-total-count', playlists.count); return res.json({playlists: playlists.playlists}); } catch (e) { next(e); } } async getOne(req, res, next) { try { const { id } = req.params; const playlist = await PlaylistService.getOne({id}); return res.json(playlist); } catch (e) { next(e); } } async edit(req, res, next) { try { const { id } = req.params; const { name } = req.body; const playlist = await PlaylistService.edit({id, name}); return res.json(playlist); } catch (e) { next(e); } } async delete(req, res, next) { try { const { id } = req.params; const playlist = await PlaylistService.delete({id}); return res.json(playlist); } catch (e) { next(e); } } } export default new PlaylistController();