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