Adding error handling
All checks were successful
Music Collection CI Workflow / test (./backend) (push) Successful in 34s
Music Collection CI Workflow / test (./frontend) (push) Successful in 38s
Music Collection CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/music-collection-backend, ./backend) (push) Successful in 50s
Music Collection CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/music-collection-frontend, ./frontend) (push) Successful in 1m48s
Music Collection CI Workflow / deploy (push) Successful in 24s

This commit is contained in:
Phill Pover 2025-04-07 10:17:17 +01:00
parent 13f8cbb3ec
commit 9ae0b1dc2f
4 changed files with 70 additions and 27 deletions

View File

@ -20,18 +20,18 @@ export class AlbumController {
constructor(private readonly albumService: AlbumService) {} constructor(private readonly albumService: AlbumService) {}
@Get() @Get()
findAll(): Promise<Album[]> { findAll(): Promise<Album[] | string | null> {
return this.albumService.findAll(); return this.albumService.findAll();
} }
@Get(':id') @Get(':id')
findOneById(@Param('id') id: number): Promise<Album | null> { findOneById(@Param('id') id: number): Promise<Album | string | null> {
return this.albumService.findOneById(id); return this.albumService.findOneById(id);
} }
@Post() @Post()
@UsePipes(new ValidationPipe({ transform: true })) @UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createAlbumDto: CreateAlbumDto): Promise<Album | null> { async create(@Body() createAlbumDto: CreateAlbumDto): Promise<Album | string | null> {
return this.albumService.create(createAlbumDto); return this.albumService.create(createAlbumDto);
} }
@ -40,12 +40,12 @@ export class AlbumController {
async update( async update(
@Param('id') id: number, @Param('id') id: number,
@Body() updateAlbumDto: UpdateAlbumDto, @Body() updateAlbumDto: UpdateAlbumDto,
): Promise<Album | null> { ): Promise<Album | string | null> {
return this.albumService.update(id, updateAlbumDto); return this.albumService.update(id, updateAlbumDto);
} }
@Delete(':id') @Delete(':id')
async remove(@Param('id') id: number): Promise<DeleteResult> { async remove(@Param('id') id: number): Promise<DeleteResult | string | null> {
return this.albumService.remove(id); return this.albumService.remove(id);
} }
} }

View File

@ -12,16 +12,21 @@ export class AlbumService {
private albumRepository: Repository<Album>, private albumRepository: Repository<Album>,
) {} ) {}
findAll(): Promise<Album[]> { findAll(): Promise<Album[] | string | null> {
return this.albumRepository.find({ return this.albumRepository.find({
order: { order: {
artist: 'ASC', artist: 'ASC',
title: 'ASC', title: 'ASC',
}, },
}).then((albums) => {
return albums;
})
.catch((error) => {
return `There was a problem getting the list of albums: ${error}`
}); });
} }
findOneById(id: number): Promise<Album | null> { findOneById(id: number): Promise<Album | string | null> {
return this.albumRepository.findOne({ return this.albumRepository.findOne({
where: { where: {
id: id, id: id,
@ -34,23 +39,35 @@ export class AlbumService {
trackNumber: 'ASC', 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<Album | null> { async create(createAlbumDto: CreateAlbumDto): Promise<Album | string | null> {
const album = this.albumRepository.create({ const album = this.albumRepository.create({
title: createAlbumDto.title, title: createAlbumDto.title,
artist: createAlbumDto.artist, artist: createAlbumDto.artist,
genre: createAlbumDto.genre, genre: createAlbumDto.genre,
}); });
const savedAlbum = await this.albumRepository.save(album); return await this.albumRepository.save(album)
.then((savedAlbum) => {
return savedAlbum; return savedAlbum;
})
.catch((error) => {
return `There was a problem creating the Album (${createAlbumDto.title} by ${createAlbumDto.artist} (${createAlbumDto.genre})): ${error}`
});
} }
async update( async update(
id: number, id: number,
updateAlbumDto: UpdateAlbumDto, updateAlbumDto: UpdateAlbumDto,
): Promise<Album | null> { ): Promise<Album | string | null> {
if (id == updateAlbumDto.id) { if (id == updateAlbumDto.id) {
const albumToUpdate = await this.albumRepository.findOneBy({ const albumToUpdate = await this.albumRepository.findOneBy({
id: updateAlbumDto.id, id: updateAlbumDto.id,
@ -82,7 +99,13 @@ export class AlbumService {
} }
} }
async remove(id: number): Promise<DeleteResult> { async remove(id: number): Promise<DeleteResult | string | null> {
return await this.albumRepository.delete(id); 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}`
});
} }
} }

View File

@ -20,18 +20,18 @@ export class SongController {
constructor(private readonly songService: SongService) {} constructor(private readonly songService: SongService) {}
@Get() @Get()
findAll(): Promise<Song[]> { findAll(): Promise<Song[] | string | null> {
return this.songService.findAll(); return this.songService.findAll();
} }
@Get(':id') @Get(':id')
findOneById(@Param('id') id: number): Promise<Song | null> { findOneById(@Param('id') id: number): Promise<Song | string | null> {
return this.songService.findOneById(id); return this.songService.findOneById(id);
} }
@Post() @Post()
@UsePipes(new ValidationPipe({ transform: true })) @UsePipes(new ValidationPipe({ transform: true }))
async create(@Body() createSongDto: CreateSongDto): Promise<Song | null> { async create(@Body() createSongDto: CreateSongDto): Promise<Song | string | null> {
return this.songService.create(createSongDto); return this.songService.create(createSongDto);
} }
@ -40,13 +40,13 @@ export class SongController {
async update( async update(
@Param('id') id: number, @Param('id') id: number,
@Body() updateSongDto: UpdateSongDto, @Body() updateSongDto: UpdateSongDto,
): Promise<Song | null> { ): Promise<Song | string | null> {
console.log(updateSongDto); console.log(updateSongDto);
return this.songService.update(id, updateSongDto); return this.songService.update(id, updateSongDto);
} }
@Delete(':id') @Delete(':id')
async remove(@Param('id') id: number): Promise<DeleteResult> { async remove(@Param('id') id: number): Promise<DeleteResult | string | null> {
return this.songService.remove(id); return this.songService.remove(id);
} }
} }

View File

@ -15,19 +15,29 @@ export class SongService {
private albumRepository: Repository<Album>, private albumRepository: Repository<Album>,
) {} ) {}
findAll(): Promise<Song[]> { findAll(): Promise<Song[] | string | null> {
return this.songRepository.find({ return this.songRepository.find({
order: { order: {
trackNumber: 'ASC', trackNumber: 'ASC',
}, },
}).then((songs) => {
return songs;
})
.catch((error) => {
return `There was a problem getting the list of songs: ${error}`
}); });
} }
findOneById(id: number): Promise<Song | null> { findOneById(id: number): Promise<Song | string | null> {
return this.songRepository.findOneBy({ id: id }); 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<Song | null> { async create(createSongDto: CreateSongDto): Promise<Song | string | null> {
const album = await this.albumRepository.findOneBy({ const album = await this.albumRepository.findOneBy({
id: createSongDto.albumId, id: createSongDto.albumId,
}); });
@ -37,14 +47,18 @@ export class SongService {
song.duration = createSongDto.duration; song.duration = createSongDto.duration;
song.trackNumber = createSongDto.trackNumber; song.trackNumber = createSongDto.trackNumber;
song.album = album; song.album = album;
const savedSong = await this.songRepository.save(song); return this.songRepository.save(song).then((albums) => {
return savedSong; 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 { } else {
throw new Error(`Unable to find Album with ID ${createSongDto.albumId}`); throw new Error(`Unable to find Album with ID ${createSongDto.albumId}`);
} }
} }
async update(id: number, updateSongDto: UpdateSongDto): Promise<Song | null> { async update(id: number, updateSongDto: UpdateSongDto): Promise<Song | string | null> {
if (id == updateSongDto.id) { if (id == updateSongDto.id) {
const album = await this.albumRepository.findOneBy({ const album = await this.albumRepository.findOneBy({
id: updateSongDto.albumId, id: updateSongDto.albumId,
@ -76,7 +90,13 @@ export class SongService {
} }
} }
async remove(id: number): Promise<DeleteResult> { async remove(id: number): Promise<DeleteResult | string | null> {
return await this.songRepository.delete(id); 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}`
});
} }
} }