UNPKG

open-music-api-node

Version:
77 lines (58 loc) 2.06 kB
const { nanoid } = require('nanoid'); const { Pool } = require('pg'); const InvariantError = require('../../exceptions/InvariantError'); const NotFoundError = require('../../exceptions/NotFoundError'); class CollaborationsService { constructor() { this._pool = new Pool(); } async addCollaboration(playlistId, userId) { const id = `collab-${nanoid(16)}`; const queryUser = { text: 'SELECT * FROM users WHERE id = $1', values: [userId], }; const resultUser = await this._pool.query(queryUser); if (!resultUser.rows.length) { throw new NotFoundError('Pengguna yang akan ditambahkan tidak ditemukan'); } const queryPlaylist = { text: 'SELECT * FROM playlist WHERE id = $1', values: [playlistId], }; const resultPlaylist = await this._pool.query(queryPlaylist); if (!resultPlaylist.rows.length) { throw new NotFoundError('Playlist yang akan ditambahkan tidak ditemukan'); } const query = { text: 'INSERT INTO collaborations VALUES($1, $2, $3) RETURNING id', values: [id, playlistId, userId], }; const result = await this._pool.query(query); if (!result.rows.length) { throw new InvariantError('Kolaborasi gagal ditambahkan'); } return result.rows[0].id; } async deleteCollaboration(playlistId, userId) { const query = { text: 'DELETE FROM collaborations WHERE playlist_id = $1 AND user_id = $2 RETURNING id', values: [playlistId, userId], }; const result = await this._pool.query(query); if (!result.rows.length) { throw new InvariantError('Kolaborasi gagal dihapus'); } } async verifyCollaborator(playlistId, userId) { const query = { text: 'SELECT * FROM collaborations WHERE playlist_id = $1 AND user_id = $2', values: [playlistId, userId], }; const result = await this._pool.query(query); if (!result.rows.length) { throw new InvariantError('Kolaborasi gagal diverifikasi'); } } } module.exports = CollaborationsService;