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