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

#3 connected all search album methods in one service method

parent 5ed90227
......@@ -23,7 +23,8 @@ export class AlbumsController {
constructor() {
this.router = express.Router()
this.service = albumsService
this.router.get('/', this.getAlbums)
this.router.get('/', this.getAlbumsHandler)
this.router.get('/:id', this.getAlbumsHandler)
this.router.post('/', upload.single('coverImage'), this.addAlbum)
}
......@@ -31,7 +32,7 @@ export class AlbumsController {
return this.router;
}
private getAlbums = async (req: Request, res: Response): Promise<void> => {
private getAlbumsHandler = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.getAlbums(req)
if (response.status === EStatuses.FAILURE){
res.status(418).send(response)
......
......@@ -7,17 +7,18 @@ import { Request } from "express";
export class AlbumsService {
public getAlbums = async(req: Request): Promise<IResponse<IAlbum[] | null>> => {
public getAlbums = async(req: Request): Promise<IResponse<IAlbum[] | IAlbum | null>> => {
try {
let data
if (req.query.artist){
data = await Album.find({artist: req.query.artist}).populate('artist')
} else if (req.query.album){
data = await Album.find({_id: req.query.album}).populate('artist')
} else if (req.params.id){
data = await Album.findById(req.params.id).populate('artist')
} else{
data = await Album.find().populate('artist')
}
const response: IResponse<IAlbum[]> = {
if (data === null || data === undefined) throw new Error('No album found')
const response: IResponse<IAlbum[] | IAlbum | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Albums found'
......
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