UNPKG

open-music-api-node

Version:
85 lines (72 loc) 2.1 kB
class SongHandler { constructor(service, validator) { this._service = service; this._validator = validator; this.addSongHandler = this.addSongHandler.bind(this); this.getSongHandler = this.getSongHandler.bind(this); this.getSongByIdHandler = this.getSongByIdHandler.bind(this); this.updateSongByIdHandler = this.updateSongByIdHandler.bind(this); this.deleteSongByIdHandler = this.deleteSongByIdHandler.bind(this); } async addSongHandler(request, h) { this._validator.validateSongPayload(request.payload); const { title, year, genre, performer, duration, albumId, } = request.payload; const song = await this._service.addSong({ title, year, genre, performer, duration, albumId, }); const response = h.response({ status: 'success', message: 'Lagu baru berhasil ditambahkan', data: { songId: song, }, }); response.code(201); return response; } async getSongHandler(request) { const { title, performer } = request.query; const song = await this._service.getAllSong({ title, performer }); return { status: 'success', data: { songs: song, }, }; } async getSongByIdHandler(request) { const { id } = request.params; const song = await this._service.getSongById(id); return { status: 'success', data: { song, }, }; } async updateSongByIdHandler(request) { this._validator.validateSongPayload(request.payload); const { title, year, genre, performer, duration, albumId, } = request.payload; const { id } = request.params; await this._service.updateSongById(id, { title, year, genre, performer, duration, albumId, }); return { status: 'success', message: 'Lagu berhasil diperbarui', }; } async deleteSongByIdHandler(request) { const { id } = request.params; await this._service.deleteSongById(id); return { status: 'success', message: 'Lagu berhasil dihapus', }; } } module.exports = SongHandler;