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) {}
@Get()
findAll(): Promise<Album[]> {
findAll(): Promise<Album[] | string | null> {
return this.albumService.findAll();
}
@Get(':id')
findOneById(@Param('id') id: number): Promise<Album | null> {
findOneById(@Param('id') id: number): Promise<Album | string | null> {
return this.albumService.findOneById(id);
}
@Post()
@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);
}
@ -40,12 +40,12 @@ export class AlbumController {
async update(
@Param('id') id: number,
@Body() updateAlbumDto: UpdateAlbumDto,
): Promise<Album | null> {
): Promise<Album | string | null> {
return this.albumService.update(id, updateAlbumDto);
}
@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);
}
}

View File

@ -12,16 +12,21 @@ export class AlbumService {
private albumRepository: Repository<Album>,
) {}
findAll(): Promise<Album[]> {
findAll(): Promise<Album[] | string | null> {
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<Album | null> {
findOneById(id: number): Promise<Album | string | null> {
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<Album | null> {
async create(createAlbumDto: CreateAlbumDto): Promise<Album | string | null> {
const album = this.albumRepository.create({
title: createAlbumDto.title,
artist: createAlbumDto.artist,
genre: createAlbumDto.genre,
});
const savedAlbum = await this.albumRepository.save(album);
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<Album | null> {
): Promise<Album | string | null> {
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<DeleteResult> {
return await this.albumRepository.delete(id);
async remove(id: number): Promise<DeleteResult | string | null> {
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) {}
@Get()
findAll(): Promise<Song[]> {
findAll(): Promise<Song[] | string | null> {
return this.songService.findAll();
}
@Get(':id')
findOneById(@Param('id') id: number): Promise<Song | null> {
findOneById(@Param('id') id: number): Promise<Song | string | null> {
return this.songService.findOneById(id);
}
@Post()
@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);
}
@ -40,13 +40,13 @@ export class SongController {
async update(
@Param('id') id: number,
@Body() updateSongDto: UpdateSongDto,
): Promise<Song | null> {
): Promise<Song | string | null> {
console.log(updateSongDto);
return this.songService.update(id, updateSongDto);
}
@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);
}
}

View File

@ -15,19 +15,29 @@ export class SongService {
private albumRepository: Repository<Album>,
) {}
findAll(): Promise<Song[]> {
findAll(): Promise<Song[] | string | null> {
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<Song | null> {
return this.songRepository.findOneBy({ id: id });
findOneById(id: number): Promise<Song | string | null> {
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({
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<Song | null> {
async update(id: number, updateSongDto: UpdateSongDto): Promise<Song | string | null> {
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<DeleteResult> {
return await this.songRepository.delete(id);
async remove(id: number): Promise<DeleteResult | string | null> {
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}`
});
}
}