import Artist from '../models/Artist.js'; import ApiError from '../controllers/ErrorController.js'; class ArtistService { async create({name, soundcloud, facebook, spotify}) { const artist = await Artist.create({name, soundcloud, facebook, spotify, date: new Date()}); return {artist}; } async getAll({page, search}) { const limit = page ? 10 : 0 const artists = await Artist.find({name: {$regex: search || '', $options: 'i'}}).skip(page * limit).limit(limit).sort({_id: -1}); const count = await Artist.countDocuments({name: {$regex: search || '', $options: 'i'}}); return {artists, count: Math.ceil(count/limit)}; } async getOne({id}) { const artist = await Artist.findById(id); if(!artist) { throw ApiError.BadRequest('Исполнителя не существует'); } return {artist}; } async edit({id, name, soundcloud, facebook, spotify}) { const artist = await Artist.findByIdAndUpdate(id, {name, soundcloud, facebook, spotify}, {returnOriginal: false}); if(!artist) { throw ApiError.BadRequest('Исполнителя не существует'); } return {artist}; } async delete({id}) { const artist = await Artist.findByIdAndDelete(id).select(['login']); if(!artist) { throw ApiError.BadRequest('Исполнителя не существует'); } return {artist}; } } export default new ArtistService();