Commit da879f5b authored by Цой Данил's avatar Цой Данил 💬

#2 added method to find by artist id. Planning to connect both gets together

parent 8e96069a
...@@ -32,7 +32,7 @@ export class AlbumsController { ...@@ -32,7 +32,7 @@ export class AlbumsController {
} }
private getAlbums = async (req: Request, res: Response): Promise<void> => { private getAlbums = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getAlbums() const response = req.query.artist ? await this.service.getAlbumsByArtistId(req.query.artist.toString()) : await this.service.getAlbums()
if (response.status === EStatuses.FAILURE){ if (response.status === EStatuses.FAILURE){
res.status(418).send(response) res.status(418).send(response)
} else{ } else{
......
...@@ -26,6 +26,26 @@ export class AlbumsService { ...@@ -26,6 +26,26 @@ export class AlbumsService {
} }
} }
public getAlbumsByArtistId = async(artistId: string): Promise<IResponse<IAlbum[] | null>> => {
try{
const data = await Album.find({artist: artistId}).populate('artist')
const response: IResponse<IAlbum[] | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Album found'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public addAlbum = async(albumDto: IAlbumDto): Promise<IResponse<IAlbum | null>> => { public addAlbum = async(albumDto: IAlbumDto): Promise<IResponse<IAlbum | null>> => {
try { try {
if (albumDto.title.trim() === '' || !albumDto.artist || !albumDto.releaseYear || !albumDto.coverImage) throw new Error('All fields should exist') if (albumDto.title.trim() === '' || !albumDto.artist || !albumDto.releaseYear || !albumDto.coverImage) throw new Error('All fields should exist')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment