1- import { performBRCAction , performCoreAction , performJSRAction , performJSRFAction } from "../config/db.js" ;
2- import { Actions } from "../config/dbActions.js" ;
3- import { ObjectId } from "mongodb" ;
1+ import { ObjectId } from "mongodb" ;
2+ import Constants from "../constants/dbConstants.js" ;
3+ import { Actions } from "../config/dbActions.js" ;
4+ import { performDBAction } from "../config/db.js" ;
5+ import LOGGER from "../utils/logger.js" ;
46
5-
6- const Artist = 'Artist' ;
7- const Song = 'Song' ;
7+ const Artist = "Artist" ;
8+ const Song = "Song" ;
9+ const { CORE_DB , JSR_DB , JSRF_DB , BRC_DB } = Constants ;
810
911export const getArtists = async ( req , res ) => {
1012 try {
1113 const artists = await fetchArtists ( req ) ;
1214 if ( artists ) {
1315 return res . send ( artists . length === 1 ? artists [ 0 ] : artists ) ;
14- }
16+ }
1517 res . status ( 404 ) . send ( ) ;
16- } catch ( err ) {
17- res . status ( 500 ) . send ( `Could not fetch ALL Artists due to error: \n${ err } ` ) ;
18+ } catch ( err ) {
19+ LOGGER . error ( `Could not fetch ALL Artists` , err ) ;
20+ res . status ( 500 ) . send ( `Could not fetch ALL Artists due to error` , err ) ;
1821 }
19- }
22+ } ;
2023
2124export const getArtistById = async ( req , res ) => {
2225 try {
23- const artist = await performCoreAction ( Actions . fetchById , Artist , req ?. params ?. id ) ;
26+ const artist = await performDBAction (
27+ Actions . fetchById ,
28+ CORE_DB ,
29+ Artist ,
30+ req ?. params ?. id
31+ ) ;
2432 if ( artist ) {
2533 return res . send ( artist ) ;
2634 }
27- res . status ( 404 ) . send ( `Artist Resource could not be found at requested location` ) ;
28- } catch ( err ) {
29- res . status ( 500 ) . send ( `Could not fetch Artist with ID: ${ req . params . id } \n${ err } ` ) ;
35+ res
36+ . status ( 404 )
37+ . send ( `Artist Resource could not be found at requested location` ) ;
38+ } catch ( err ) {
39+ LOGGER . error ( `Could not fetch Artist by Id ${ req ?. params ?. id } ` , err ) ;
40+ res
41+ . status ( 500 )
42+ . send ( `Could not fetch Artist with ID: ${ req . params . id } ` , err ) ;
3043 }
31- }
44+ } ;
3245
3346export const getSongsByArtist = async ( req , res ) => {
3447 try {
3548 const artistId = req ?. params ?. id ;
3649 if ( ! artistId ) {
37- return res . status ( 400 ) . send ( ' Invalid ArtistId' ) ;
50+ return res . status ( 400 ) . send ( " Invalid ArtistId" ) ;
3851 }
3952 res . send ( await fetchSongsByArtistId ( artistId ) ) ;
40- } catch ( err ) {
41- res . status ( 500 ) . send ( `Could not fetch Songs by Artist with ID: ${ req . params . id } \n${ err } ` ) ;
53+ } catch ( err ) {
54+ LOGGER . error ( `Could not fetch Songs By Artist ${ req ?. params ?. id } ` , err ) ;
55+ res
56+ . status ( 500 )
57+ . send ( `Could not fetch Songs by Artist with ID: ${ req . params . id } ` , err ) ;
4258 }
43- }
44-
59+ } ;
4560
4661export const fetchArtists = async ( req ) => {
4762 if ( req ?. query ) {
48- return await performCoreAction ( Actions . fetchWithQuery , Artist , null , req ?. query ) ;
63+ return await performDBAction (
64+ Actions . fetchWithQuery ,
65+ CORE_DB ,
66+ Artist ,
67+ null ,
68+ req ?. query
69+ ) ;
4970 }
50- return await performCoreAction ( Actions . fetchAll , Artist , null ) ;
51- }
71+ return await performDBAction ( Actions . fetchAll , CORE_DB , Artist , null ) ;
72+ } ;
5273
5374export const fetchSongsByArtistId = async ( artistId ) => {
5475 const songs = [ ] ;
55- const jsrSongs = await performJSRAction ( Actions . fetchWithQuery , Song , null , { artistId : new ObjectId ( artistId ) } ) ;
56- const jsrfSongs = await performJSRFAction ( Actions . fetchWithQuery , Song , null , { artistId : new ObjectId ( artistId ) } ) ;
57- const brcSongs = await performBRCAction ( Actions . fetchWithQuery , Song , null , { artistId : new ObjectId ( artistId ) } ) ;
76+ const jsrSongs = await performDBAction (
77+ Actions . fetchWithQuery ,
78+ JSR_DB ,
79+ Song ,
80+ null ,
81+ {
82+ artistId : new ObjectId ( artistId ) ,
83+ }
84+ ) ;
85+ const jsrfSongs = await performDBAction (
86+ Actions . fetchWithQuery ,
87+ JSRF_DB ,
88+ Song ,
89+ null ,
90+ { artistId : new ObjectId ( artistId ) }
91+ ) ;
92+ const brcSongs = await performDBAction (
93+ Actions . fetchWithQuery ,
94+ BRC_DB ,
95+ Song ,
96+ null ,
97+ {
98+ artistId : new ObjectId ( artistId ) ,
99+ }
100+ ) ;
58101 if ( jsrSongs && jsrSongs . length > 0 ) {
59102 songs . push ( jsrSongs ) ;
60103 }
@@ -65,4 +108,4 @@ export const fetchSongsByArtistId = async (artistId) => {
65108 songs . push ( brcSongs ) ;
66109 }
67110 return songs . flat ( 1 ) ;
68- }
111+ } ;
0 commit comments