|
| 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 LOGGER from "../utils/logger.js"; |
| 6 | +import {fetchRandom} from "./utilController.js"; |
| 7 | + |
| 8 | +const GraffitiSoul = "GraffitiSoul"; |
| 9 | +const {JSR_DB, JSRF_DB} = Constants; |
| 10 | + |
| 11 | +export const getAllGraffitiSouls = 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 jsrSouls = await fetchJSRSouls(req); |
| 16 | + const jsrfSouls = await fetchJSRFSouls(req); |
| 17 | + if (sortByValue) { |
| 18 | + const Souls = [...jsrSouls, ...jsrfSouls]; |
| 19 | + return res.send(Souls.sort(sortObjects(sortByValue, sortOrder))); |
| 20 | + } |
| 21 | + res.send([...jsrSouls, ...jsrfSouls]); |
| 22 | + } catch (err) { |
| 23 | + LOGGER.error(`Could not fetch ALL GraffitiSouls`, err); |
| 24 | + res |
| 25 | + .status(500) |
| 26 | + .json({message: "Failed to fetch ALL GraffitiSouls", err: err}); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +export const getRandomGraffitiSoul = async (req, res) => { |
| 31 | + try { |
| 32 | + res.send(await fetchRandom(req, GraffitiSoul)); |
| 33 | + } catch (err) { |
| 34 | + LOGGER.error(`Could not fetch random GraffitiSoul`, err); |
| 35 | + res.status(500).json({error: "Failed to fetch random GraffitiSoul"}); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +export const getJSRGraffitiSouls = async (req, res) => { |
| 40 | + try { |
| 41 | + res.send(await fetchJSRSouls(req)); |
| 42 | + } catch (err) { |
| 43 | + LOGGER.error(`Could not fetch JSR GraffitiSouls`, err); |
| 44 | + res |
| 45 | + .status(500) |
| 46 | + .json({message: "Failed to fetch JSR GraffitiSouls", err: err}); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +export const getJSRFGraffitiSouls = async (req, res) => { |
| 51 | + try { |
| 52 | + res.send(await fetchJSRFSouls(req)); |
| 53 | + } catch (err) { |
| 54 | + LOGGER.error(`Could not fetch JSRF GraffitiSouls`, err); |
| 55 | + res |
| 56 | + .status(500) |
| 57 | + .json({message: "Failed to fetch JSRF GraffitiSouls", err: err}); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +export const getJSRGraffitiSoulById = async (req, res) => { |
| 62 | + try { |
| 63 | + const soulId = req?.params?.id; |
| 64 | + res.send( |
| 65 | + await performDBAction(Actions.fetchById, JSR_DB, GraffitiSoul, soulId) |
| 66 | + ); |
| 67 | + } catch (err) { |
| 68 | + LOGGER.error(`Could not fetch JSR GraffitiSoul With ID: ${soulId}`, err); |
| 69 | + res |
| 70 | + .status(500) |
| 71 | + .json({message: "Failed to fetch JSR GraffitiSoul By ID", err: err}); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +export const getJSRFGraffitiSoulById = async (req, res) => { |
| 76 | + try { |
| 77 | + const soulId = req?.params?.id; |
| 78 | + res.send( |
| 79 | + await performDBAction(Actions.fetchById, JSRF_DB, GraffitiSoul, soulId) |
| 80 | + ); |
| 81 | + } catch (err) { |
| 82 | + LOGGER.error(`Could not fetch JSRF GraffitiSoul With ID: ${soulId}`, err); |
| 83 | + res |
| 84 | + .status(500) |
| 85 | + .json({message: "Failed to fetch JSRF GraffitiSoul By ID", err: err}); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +export const fetchJSRSouls = async (req) => { |
| 90 | + if (req?.query) { |
| 91 | + return await performDBAction( |
| 92 | + Actions.fetchWithQuery, |
| 93 | + JSR_DB, |
| 94 | + GraffitiSoul, |
| 95 | + null, |
| 96 | + req?.query |
| 97 | + ); |
| 98 | + } |
| 99 | + return await performDBAction(Actions.fetchAll, JSR_DB, GraffitiSoul, null); |
| 100 | +}; |
| 101 | + |
| 102 | +export const fetchJSRFSouls = async (req) => { |
| 103 | + if (req?.query) { |
| 104 | + return await performDBAction( |
| 105 | + Actions.fetchWithQuery, |
| 106 | + JSRF_DB, |
| 107 | + GraffitiSoul, |
| 108 | + null, |
| 109 | + req?.query |
| 110 | + ); |
| 111 | + } |
| 112 | + return await performDBAction(Actions.fetchAll, JSRF_DB, GraffitiSoul, null); |
| 113 | +}; |
0 commit comments