You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB
JavaScript

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