File tree Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ const errorHandler = require ( "../middlewares/errorMiddleware" ) ;
2+ const {
3+ randomQuoteBySong,
4+ randomQuoteByAlbum,
5+ getAllSongQuote,
6+ getAlbumQuotes,
7+ randomQuote,
8+ allQuotes,
9+ } = require ( "../utils/quotes" ) ;
10+
11+ const getRandomQuote = async ( req , res ) => {
12+ try {
13+ const { album, song } = req . query ;
14+ if ( song ) {
15+ return res . status ( 200 ) . json ( await randomQuoteBySong ( song ) ) ;
16+ }
17+ if ( album ) {
18+ return res . status ( 200 ) . json ( await randomQuoteByAlbum ( album ) ) ;
19+ }
20+
21+ res . status ( 200 ) . json ( await randomQuote ( ) ) ;
22+ } catch ( error ) {
23+ console . log ( ) ;
24+ errorHandler ( "Error: fetching all quotes" , res , error ) ;
25+ }
26+ } ;
27+
28+ const getAllQuotes = async ( req , res ) => {
29+ try {
30+ const { album, song } = req . query ;
31+ if ( song ) {
32+ return res . status ( 200 ) . json ( await getAllSongQuote ( song ) ) ;
33+ }
34+ if ( album ) {
35+ return res . status ( 200 ) . json ( await getAlbumQuotes ( album ) ) ;
36+ }
37+ res . status ( 200 ) . json ( await allQuotes ( ) ) ;
38+ } catch ( error ) {
39+ console . log ( ) ;
40+ errorHandler ( "Error: fetching all quotes" , res , error ) ;
41+ }
42+ } ;
43+
44+ module . exports = { getAllQuotes, getRandomQuote } ;
Original file line number Diff line number Diff line change 1+ const Quote = require ( "../models/Quote" ) ;
2+ const getAlbumQuotes = async ( albumName ) => {
3+ const quotes = await Quote . find ( { album : albumName } )
4+ . select ( "-__v" )
5+ . select ( "-_id" ) ;
6+ return quotes ;
7+ } ;
8+ const randomQuoteByAlbum = async ( albumName ) => {
9+ const quotes = await Quote . find ( { album : albumName } )
10+ . select ( "-__v" )
11+ . select ( "-_id" ) ;
12+ const quote = quotes [ Math . floor ( Math . random ( ) * quotes . length ) ] ;
13+ return quote ;
14+ } ;
15+
16+ const randomQuoteBySong = async ( songName ) => {
17+ const quotes = await Quote . find ( { song : songName } )
18+ . select ( "-__v" )
19+ . select ( "-_id" ) ;
20+ const quote = quotes [ Math . floor ( Math . random ( ) * quotes . length ) ] ;
21+ return quote ;
22+ } ;
23+ const getAllSongQuote = async ( songName ) => {
24+ const quotes = await Quote . find ( { song : songName } )
25+ . select ( "-__v" )
26+ . select ( "-_id" ) ;
27+
28+ return quotes ;
29+ } ;
30+ const randomQuote = async ( ) => {
31+ const quotes = await Quote . find ( ) . select ( "-__v" ) . select ( "-_id" ) ;
32+ const quote = quotes [ Math . floor ( Math . random ( ) * quotes . length ) ] ;
33+ return quote ;
34+ } ;
35+ const allQuotes = async ( ) => {
36+ const quotes = await Quote . find ( ) . select ( "-__v" ) . select ( "-_id" ) ;
37+ return quotes ;
38+ } ;
39+
40+ module . exports = {
41+ getAlbumQuotes,
42+ randomQuote,
43+ randomQuoteByAlbum,
44+ randomQuoteBySong,
45+ getAllSongQuote,
46+ allQuotes,
47+ } ;
You can’t perform that action at this time.
0 commit comments