From 9ae0b1dc2f7e4397791a98e3cdd17a471b648050 Mon Sep 17 00:00:00 2001 From: Phill Pover Date: Mon, 7 Apr 2025 10:17:17 +0100 Subject: [PATCH] Adding error handling --- backend/src/album/album.controller.ts | 10 +++---- backend/src/album/album.service.ts | 39 +++++++++++++++++++++------ backend/src/song/song.controller.ts | 10 +++---- backend/src/song/song.service.ts | 38 +++++++++++++++++++------- 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/backend/src/album/album.controller.ts b/backend/src/album/album.controller.ts index bb12857..5fecd04 100644 --- a/backend/src/album/album.controller.ts +++ b/backend/src/album/album.controller.ts @@ -20,18 +20,18 @@ export class AlbumController { constructor(private readonly albumService: AlbumService) {} @Get() - findAll(): Promise { + findAll(): Promise { return this.albumService.findAll(); } @Get(':id') - findOneById(@Param('id') id: number): Promise { + findOneById(@Param('id') id: number): Promise { return this.albumService.findOneById(id); } @Post() @UsePipes(new ValidationPipe({ transform: true })) - async create(@Body() createAlbumDto: CreateAlbumDto): Promise { + async create(@Body() createAlbumDto: CreateAlbumDto): Promise { return this.albumService.create(createAlbumDto); } @@ -40,12 +40,12 @@ export class AlbumController { async update( @Param('id') id: number, @Body() updateAlbumDto: UpdateAlbumDto, - ): Promise { + ): Promise { return this.albumService.update(id, updateAlbumDto); } @Delete(':id') - async remove(@Param('id') id: number): Promise { + async remove(@Param('id') id: number): Promise { return this.albumService.remove(id); } } diff --git a/backend/src/album/album.service.ts b/backend/src/album/album.service.ts index 52a2bd0..92e852e 100644 --- a/backend/src/album/album.service.ts +++ b/backend/src/album/album.service.ts @@ -12,16 +12,21 @@ export class AlbumService { private albumRepository: Repository, ) {} - findAll(): Promise { + findAll(): Promise { return this.albumRepository.find({ order: { artist: 'ASC', title: 'ASC', }, + }).then((albums) => { + return albums; + }) + .catch((error) => { + return `There was a problem getting the list of albums: ${error}` }); } - findOneById(id: number): Promise { + findOneById(id: number): Promise { return this.albumRepository.findOne({ where: { id: id, @@ -34,23 +39,35 @@ export class AlbumService { trackNumber: 'ASC', }, }, + }).then((album) => { + return album; + }) + .catch((error) => { + return `There was a problem creating the Album identified by ID ${id}: ${error}` + }).finally(() => { + return `There was a problem creating the Album identified by ID ${id}` }); } - async create(createAlbumDto: CreateAlbumDto): Promise { + async create(createAlbumDto: CreateAlbumDto): Promise { const album = this.albumRepository.create({ title: createAlbumDto.title, artist: createAlbumDto.artist, genre: createAlbumDto.genre, }); - const savedAlbum = await this.albumRepository.save(album); - return savedAlbum; + return await this.albumRepository.save(album) + .then((savedAlbum) => { + return savedAlbum; + }) + .catch((error) => { + return `There was a problem creating the Album (${createAlbumDto.title} by ${createAlbumDto.artist} (${createAlbumDto.genre})): ${error}` + }); } async update( id: number, updateAlbumDto: UpdateAlbumDto, - ): Promise { + ): Promise { if (id == updateAlbumDto.id) { const albumToUpdate = await this.albumRepository.findOneBy({ id: updateAlbumDto.id, @@ -82,7 +99,13 @@ export class AlbumService { } } - async remove(id: number): Promise { - return await this.albumRepository.delete(id); + async remove(id: number): Promise { + return await this.albumRepository.delete(id) + .then((deleteResult) => { + return deleteResult; + }) + .catch((error) => { + return `There was a problem deleting the Album identified by ID ${id}: ${error}` + }); } } diff --git a/backend/src/song/song.controller.ts b/backend/src/song/song.controller.ts index 662ca4a..cd3d43c 100644 --- a/backend/src/song/song.controller.ts +++ b/backend/src/song/song.controller.ts @@ -20,18 +20,18 @@ export class SongController { constructor(private readonly songService: SongService) {} @Get() - findAll(): Promise { + findAll(): Promise { return this.songService.findAll(); } @Get(':id') - findOneById(@Param('id') id: number): Promise { + findOneById(@Param('id') id: number): Promise { return this.songService.findOneById(id); } @Post() @UsePipes(new ValidationPipe({ transform: true })) - async create(@Body() createSongDto: CreateSongDto): Promise { + async create(@Body() createSongDto: CreateSongDto): Promise { return this.songService.create(createSongDto); } @@ -40,13 +40,13 @@ export class SongController { async update( @Param('id') id: number, @Body() updateSongDto: UpdateSongDto, - ): Promise { + ): Promise { console.log(updateSongDto); return this.songService.update(id, updateSongDto); } @Delete(':id') - async remove(@Param('id') id: number): Promise { + async remove(@Param('id') id: number): Promise { return this.songService.remove(id); } } diff --git a/backend/src/song/song.service.ts b/backend/src/song/song.service.ts index 9c9b417..3467f94 100644 --- a/backend/src/song/song.service.ts +++ b/backend/src/song/song.service.ts @@ -15,19 +15,29 @@ export class SongService { private albumRepository: Repository, ) {} - findAll(): Promise { + findAll(): Promise { return this.songRepository.find({ order: { trackNumber: 'ASC', }, + }).then((songs) => { + return songs; + }) + .catch((error) => { + return `There was a problem getting the list of songs: ${error}` }); } - findOneById(id: number): Promise { - return this.songRepository.findOneBy({ id: id }); + findOneById(id: number): Promise { + return this.songRepository.findOneBy({ id: id }).then((albums) => { + return albums; + }) + .catch((error) => { + return `There was a problem getting the song identified by ID ${id}: ${error}` + }); } - async create(createSongDto: CreateSongDto): Promise { + async create(createSongDto: CreateSongDto): Promise { const album = await this.albumRepository.findOneBy({ id: createSongDto.albumId, }); @@ -37,14 +47,18 @@ export class SongService { song.duration = createSongDto.duration; song.trackNumber = createSongDto.trackNumber; song.album = album; - const savedSong = await this.songRepository.save(song); - return savedSong; + return this.songRepository.save(song).then((albums) => { + return albums; + }) + .catch((error) => { + return `There was a problem creating the song (${createSongDto.trackNumber} ${createSongDto.title} (${createSongDto.duration}s) on the Album ${album.title} by ${album.artist}): ${error}` + }); } 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, @@ -76,7 +90,13 @@ export class SongService { } } - async remove(id: number): Promise { - return await this.songRepository.delete(id); + async remove(id: number): Promise { + return await this.songRepository.delete(id) + .then((deleteResult) => { + return deleteResult; + }) + .catch((error) => { + return `There was a problem deleting the Song identified by ID ${id}: ${error}` + }); } }