|
| 1 | +import Constants from "../constants/dbConstants.js"; |
| 2 | +import {Actions} from "../config/dbActions.js"; |
| 3 | +import {performDBAction} from "../config/db.js"; |
| 4 | +import {sortObjects} from "../utils/utility.js"; |
| 5 | +import {fetchRandom} from "./utilController.js"; |
| 6 | +import LOGGER from "../utils/logger.js"; |
| 7 | + |
| 8 | +const Audio = "Audio"; |
| 9 | +const {JSR_DB, JSRF_DB} = Constants; |
| 10 | + |
| 11 | +export const getAudio = async (req, res) => { |
| 12 | + try { |
| 13 | + const sortByValue = req?.query?.sortBy ? req?.query?.sortBy : undefined; |
| 14 | + const sortOrder = req?.query?.orderBy ? req?.query?.orderBy : "asc"; |
| 15 | + const audio = await fetchAudio(req, "ALL"); |
| 16 | + if (sortByValue) { |
| 17 | + return res.send(audio.sort(sortObjects(sortByValue, sortOrder))); |
| 18 | + } |
| 19 | + res.send(audio); |
| 20 | + } catch (err) { |
| 21 | + LOGGER.error(`Could not fetch ALL ${Audio}`, err); |
| 22 | + res.status(500).send(`Could not fetch ALL ${Audio} due to error:`, err); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +export const getRandomAudio = async (req, res) => { |
| 27 | + try { |
| 28 | + res.send(await fetchRandom(req, Audio)); |
| 29 | + } catch (err) { |
| 30 | + LOGGER.error(`Could not fetch random ${Audio}`, err); |
| 31 | + res.status(500).json({error: `Failed to fetch random ${Audio}`}); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +export const getJSRAudio = async (req, res) => { |
| 36 | + try { |
| 37 | + const jsrAudio = await fetchAudio(req, JSR_DB); |
| 38 | + if (jsrAudio) { |
| 39 | + return res.send(jsrAudio); |
| 40 | + } |
| 41 | + res.status(404).send(); |
| 42 | + } catch (err) { |
| 43 | + LOGGER.error(`Could not fetch JSR ${Audio}`, err); |
| 44 | + res.status(500).send(`Could not fetch JSR ${Audio}`, err); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +export const getJSRAudioById = async (req, res) => { |
| 49 | + try { |
| 50 | + const id = req?.params?.id; |
| 51 | + const jsrAudio = await performDBAction( |
| 52 | + Actions.fetchById, |
| 53 | + JSR_DB, |
| 54 | + Audio, |
| 55 | + id |
| 56 | + ); |
| 57 | + if (jsrAudio) { |
| 58 | + return res.send(jsrAudio); |
| 59 | + } |
| 60 | + res |
| 61 | + .status(404) |
| 62 | + .send(`JSR ${Audio} Resource could not be found at requested location`); |
| 63 | + } catch (err) { |
| 64 | + LOGGER.error(`Could not fetch JSR ${Audio} by ID ${req?.params?.id}`, err); |
| 65 | + res |
| 66 | + .status(500) |
| 67 | + .send(`Could not fetch JSR ${Audio} with ID: ${req.params.id}`, err); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +export const getJSRFAudio = async (req, res) => { |
| 72 | + try { |
| 73 | + const jsrfAudio = await fetchAudio(req, JSRF_DB); |
| 74 | + if (jsrfAudio) { |
| 75 | + return res.send(jsrfAudio); |
| 76 | + } |
| 77 | + res.status(404).send(); |
| 78 | + } catch (err) { |
| 79 | + LOGGER.error(`Could not fetch JSRF ${Audio}`, err); |
| 80 | + res.status(500).send(`Could not fetch JSRF ${Audio}`, err); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +export const getJSRFAudioById = async (req, res) => { |
| 85 | + try { |
| 86 | + const id = req?.params?.id; |
| 87 | + const jsrfAudio = await performDBAction( |
| 88 | + Actions.fetchById, |
| 89 | + JSRF_DB, |
| 90 | + Audio, |
| 91 | + id |
| 92 | + ); |
| 93 | + if (jsrfAudio) { |
| 94 | + return res.send(jsrfAudio); |
| 95 | + } |
| 96 | + res |
| 97 | + .status(404) |
| 98 | + .send(`JSRF ${Audio} Resource could not be found at requested location`); |
| 99 | + } catch (err) { |
| 100 | + LOGGER.error(`Could not fetch JSRF ${Audio} by ID`, err); |
| 101 | + res |
| 102 | + .status(500) |
| 103 | + .send(`Could not fetch JSRF ${Audio} with ID: ${req.params.id}`, err); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +export const fetchAudio = async (req, dbName) => { |
| 108 | + if (dbName === "ALL") { |
| 109 | + const jsrAudio = await fetchAudio(req, JSR_DB); |
| 110 | + const jsrfAudio = await fetchAudio(req, JSRF_DB); |
| 111 | + const audio = [...jsrAudio, ...jsrfAudio]; |
| 112 | + return audio; |
| 113 | + } |
| 114 | + |
| 115 | + return await performDBAction( |
| 116 | + Actions.fetchWithQuery, |
| 117 | + dbName, |
| 118 | + Audio, |
| 119 | + null, |
| 120 | + req?.query |
| 121 | + ); |
| 122 | +}; |
0 commit comments