diff --git a/backend/src/album/album.controller.spec.ts b/backend/src/album/album.controller.spec.ts index d4a1317..cbde833 100644 --- a/backend/src/album/album.controller.spec.ts +++ b/backend/src/album/album.controller.spec.ts @@ -59,15 +59,15 @@ describe('AlbumController', () => { }) describe('create', () => { - it('should return the Album ID', async () => { - expect(await controller.create(mockCreateAlbumDto)).toStrictEqual(mockAlbum); + it('should return the Album', async () => { + expect(await controller.create(mockCreateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }); }) }) describe('update', () => { - it('should return the success message', async () => { + it('should return the Album', async () => { mockUpdateAlbumDto.id = 1; - expect(await controller.update(1, mockUpdateAlbumDto)).toStrictEqual(mockAlbum); + expect(await controller.update(1, mockUpdateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }); }) }) diff --git a/backend/src/album/album.service.spec.ts b/backend/src/album/album.service.spec.ts index 7d464be..729ab33 100644 --- a/backend/src/album/album.service.spec.ts +++ b/backend/src/album/album.service.spec.ts @@ -27,7 +27,7 @@ describe('AlbumService', () => { findOneBy: jest.fn().mockResolvedValue({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }), create: jest.fn().mockResolvedValue(mockAlbum), save: jest.fn().mockResolvedValue({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }), - update: jest.fn().mockResolvedValue(mockAlbum), + update: jest.fn().mockResolvedValue({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }), delete: jest.fn().mockResolvedValue(mockDeleteResult), }, }, @@ -55,15 +55,15 @@ describe('AlbumService', () => { }) describe('create', () => { - it('should return the Album ID', async () => { - expect(await service.create(mockCreateAlbumDto)).toStrictEqual(mockAlbum); + it('should return the Album', async () => { + expect(await service.create(mockCreateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }); }) }) describe('update', () => { - it('should return the success message', async () => { + it('should return the Album', async () => { mockUpdateAlbumDto.id = 1; - expect(await service.update(1, mockUpdateAlbumDto)).toStrictEqual(mockAlbum); + expect(await service.update(1, mockUpdateAlbumDto)).toStrictEqual({ id: 1, title: 'some album', artist: 'some artist', genre: 'some genre' }); }) }) diff --git a/backend/src/album/album.service.ts b/backend/src/album/album.service.ts index d707df6..e7ea8a2 100644 --- a/backend/src/album/album.service.ts +++ b/backend/src/album/album.service.ts @@ -44,15 +44,14 @@ export class AlbumService { async update(id: number, updateAlbumDto: UpdateAlbumDto): Promise { if (id === updateAlbumDto.id) { - const album = this.albumRepository.findOneBy({ id: updateAlbumDto.id }); - if (!album) + const albumToUpdate = await this.albumRepository.findOneBy({ id: updateAlbumDto.id }); + if (!albumToUpdate) return null; - const savedAlbum = await this.albumRepository.update({ id: updateAlbumDto.id }, { - title: updateAlbumDto.title, - artist: updateAlbumDto.artist, - genre: updateAlbumDto.genre - }); + albumToUpdate.title = updateAlbumDto.title; + albumToUpdate.artist = updateAlbumDto.artist; + albumToUpdate.genre = updateAlbumDto.genre; + const savedAlbum = await this.albumRepository.save(albumToUpdate); return savedAlbum; } else { return null; diff --git a/backend/src/song/song.controller.ts b/backend/src/song/song.controller.ts index a9e76b9..811ddb5 100644 --- a/backend/src/song/song.controller.ts +++ b/backend/src/song/song.controller.ts @@ -22,13 +22,13 @@ export class SongController { @Post() @UsePipes(new ValidationPipe({ transform: true })) - async create(@Body() createSongDto: CreateSongDto): Promise { + async create(@Body() createSongDto: CreateSongDto): Promise { return this.songService.create(createSongDto); } @Put(':id') @UsePipes(new ValidationPipe({ transform: true })) - async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise { + async update(@Param('id') id: number, @Body() updateSongDto: UpdateSongDto): Promise { return this.songService.update(id, updateSongDto); } diff --git a/backend/src/song/song.service.ts b/backend/src/song/song.service.ts index 0c433f0..5bb9b35 100644 --- a/backend/src/song/song.service.ts +++ b/backend/src/song/song.service.ts @@ -27,7 +27,7 @@ export class SongService { return this.songRepository.findOneBy({ id: id }); } - async create(createSongDto: CreateSongDto): Promise { + async create(createSongDto: CreateSongDto): Promise { const album = await this.albumRepository.findOneBy({id: createSongDto.albumId}); if (album) { const song = new Song(); @@ -36,28 +36,28 @@ export class SongService { song.trackNumber = createSongDto.trackNumber; song.album = album; const savedSong = await this.songRepository.save(song); - return savedSong.id; + return savedSong; } else { throw new Error(`Unable to find Album with ID ${createSongDto.albumId}`); } } - async update(id: number, updateSongDto: UpdateSongDto): Promise { + async update(id: number, updateSongDto: UpdateSongDto): Promise { if (id === updateSongDto.id) { const album = await this.albumRepository.findOneBy({id: updateSongDto.albumId}); const songToUpdate = await this.songRepository.findOneBy({ id: updateSongDto.id }); if (!songToUpdate || !album) - return "Song not found"; + return null; songToUpdate.id = updateSongDto.id; songToUpdate.title = updateSongDto.title; songToUpdate.duration = updateSongDto.duration; songToUpdate.trackNumber = updateSongDto.trackNumber; songToUpdate.album = album; - await this.songRepository.update({ id: updateSongDto.id }, songToUpdate); - return "Song updated successfully"; + const song = await this.songRepository.save(songToUpdate); + return song; } else { - return "Song ID does not match posted data" + return null; } }