From e46cc7aec7579b23639465189f167b71a6c41e84 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Thu, 18 Jan 2024 15:34:48 -0500 Subject: [PATCH] Add a query option for returning the internal database flags for galaxies. --- src/stories/hubbles_law/database.ts | 28 ++++++++++++++++++++-------- src/stories/hubbles_law/router.ts | 5 +++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/stories/hubbles_law/database.ts b/src/stories/hubbles_law/database.ts index 9dcb14d..ecd6712 100644 --- a/src/stories/hubbles_law/database.ts +++ b/src/stories/hubbles_law/database.ts @@ -1,4 +1,4 @@ -import { Op, QueryTypes, Sequelize, WhereOptions, col, fn, literal } from "sequelize"; +import { Attributes, FindOptions, Op, QueryTypes, Sequelize, WhereOptions, col, fn, literal } from "sequelize"; import { AsyncMergedHubbleStudentClasses, Galaxy, HubbleMeasurement, SampleHubbleMeasurement, initializeModels, SyncMergedHubbleClasses } from "./models"; import { classSize, cosmicdsDB, findClassById, findStudentById } from "../../database"; import { RemoveHubbleMeasurementResult, SubmitHubbleMeasurementResult } from "./request_results"; @@ -12,7 +12,7 @@ import { logger } from "../../logger"; initializeModels(cosmicdsDB); setUpHubbleAssociations(); -const galaxyAttributes = ["ra", "decl", "z", "type", "name", "element"]; +const galaxyAttributes = ["id", "ra", "decl", "z", "type", "name", "element"]; export async function submitHubbleMeasurement(data: { student_id: number, @@ -506,25 +506,37 @@ export async function removeSampleHubbleMeasurement(studentID: number, measureme return count > 0 ? RemoveHubbleMeasurementResult.MeasurementDeleted : RemoveHubbleMeasurementResult.NoSuchMeasurement; } -export async function getGalaxiesForTypes(types: string[]): Promise { - return Galaxy.findAll({ +export async function getGalaxiesForTypes(types: string[], flags=false): Promise { + const query: FindOptions> = { where: { is_bad: 0, spec_is_bad: 0, is_sample: 0, type: { [Op.in]: types } } - }); + }; + + if (!flags) { + query.attributes = galaxyAttributes; + } + + return Galaxy.findAll(query); } -export async function getAllGalaxies(): Promise { - return Galaxy.findAll({ +export async function getAllGalaxies(flags=false): Promise { + const query: FindOptions> = { where: { is_bad: 0, spec_is_bad: 0, is_sample: 0 } - }); + }; + + if (!flags) { + query.attributes = galaxyAttributes; + } + + return Galaxy.findAll(query); } export async function getGalaxyByName(name: string): Promise { diff --git a/src/stories/hubbles_law/router.ts b/src/stories/hubbles_law/router.ts index b19c01b..2954d33 100644 --- a/src/stories/hubbles_law/router.ts +++ b/src/stories/hubbles_law/router.ts @@ -352,9 +352,10 @@ router.put("/sync-merged-class/:classID", async(req, res) => { router.get("/galaxies", async (req, res) => { const types = req.query?.types ?? undefined; + const flags = (/true/i).test((req.query?.flags as string) ?? undefined); let galaxies: Galaxy[]; if (types === undefined) { - galaxies = await getAllGalaxies(); + galaxies = await getAllGalaxies(flags); } else { let galaxyTypes: string[]; if (Array.isArray(types)) { @@ -362,7 +363,7 @@ router.get("/galaxies", async (req, res) => { } else { galaxyTypes = (types as string).split(","); } - galaxies = await getGalaxiesForTypes(galaxyTypes); + galaxies = await getGalaxiesForTypes(galaxyTypes, flags); } res.json(galaxies); });