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

#3 connected two methods in one to get albums by artist id

parent da879f5b
......@@ -32,7 +32,7 @@ export class AlbumsController {
}
private getAlbums = async (req: Request, res: Response): Promise<void> => {
const response = req.query.artist ? await this.service.getAlbumsByArtistId(req.query.artist.toString()) : await this.service.getAlbums()
const response = await this.service.getAlbums(req)
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
} else{
......
......@@ -3,12 +3,15 @@ import IAlbum from "../interfaces/IAlbum";
import IResponse from "../interfaces/IResponse";
import IAlbumDto from "../interfaces/IAlbumDto";
import { Album } from "../models/Album";
import { Request } from "express";
export class AlbumsService {
public getAlbums = async(): Promise<IResponse<IAlbum[] | null>> => {
public getAlbums = async(req: Request): Promise<IResponse<IAlbum[] | null>> => {
try {
const data = await Album.find().populate('artist')
const data = req.query.artist ?
await Album.find({artist: req.query.artist}).populate('artist') :
await Album.find().populate('artist')
const response: IResponse<IAlbum[]> = {
status: EStatuses.SUCCESS,
result: data,
......@@ -26,26 +29,6 @@ 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>> => {
try {
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