From 8b8fe29347e766acd650faab09872250ad8955bd Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Tue, 17 Oct 2023 17:46:13 -0400 Subject: [PATCH 1/3] Add SQL and model for dashboard class groups table. --- src/models/dashboard_class_group.ts | 36 +++++++++++++++++++ src/models/index.ts | 3 ++ .../create_dashboard_class_group_table.sql | 8 +++++ 3 files changed, 47 insertions(+) create mode 100644 src/models/dashboard_class_group.ts create mode 100644 src/sql/create_dashboard_class_group_table.sql diff --git a/src/models/dashboard_class_group.ts b/src/models/dashboard_class_group.ts new file mode 100644 index 0000000..42dd008 --- /dev/null +++ b/src/models/dashboard_class_group.ts @@ -0,0 +1,36 @@ +import { Sequelize, DataTypes, Model, InferAttributes, InferCreationAttributes, CreationOptional, DATE } from "sequelize"; + +export class DashboardClassGroup extends Model, InferCreationAttributes> { + declare id: CreationOptional; + declare name: string; + declare code: string; + declare class_ids: number[]; +} + +export function initializeDashboardClassGroupModel(sequelize: Sequelize) { + DashboardClassGroup.init({ + id: { + type: DataTypes.INTEGER.UNSIGNED, + allowNull: false, + autoIncrement: true, + primaryKey: true + }, + name: { + type: DataTypes.STRING, + allowNull: false, + unique: true + }, + code: { + type: DataTypes.STRING, + allowNull: false, + unique: true + }, + class_ids: { + type: DataTypes.JSON, + allowNull: false + } + }, { + sequelize, + engine: "InnoDB" + }); +} diff --git a/src/models/index.ts b/src/models/index.ts index 5bcab49..457a688 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,4 +1,5 @@ import { Class, initializeClassModel } from "./class"; +import { DashboardClassGroup, initializeDashboardClassGroupModel } from "./dashboard_class_group"; import { DummyClass, initializeDummyClassModel } from "./dummy_class"; import { Educator, initializeEducatorModel } from "./educator"; import { IgnoreStudent, initializeIgnoreStudentModel } from "./ignore_student"; @@ -17,6 +18,7 @@ export { Class, ClassStories, CosmicDSSession, + DashboardClassGroup, DummyClass, Educator, IgnoreStudent, @@ -39,4 +41,5 @@ export function initializeModels(db: Sequelize) { initializeStudentOptionsModel(db); initializeIgnoreStudentModel(db); initializeQuestionModel(db); + initializeDashboardClassGroupModel(db); } diff --git a/src/sql/create_dashboard_class_group_table.sql b/src/sql/create_dashboard_class_group_table.sql new file mode 100644 index 0000000..d811458 --- /dev/null +++ b/src/sql/create_dashboard_class_group_table.sql @@ -0,0 +1,8 @@ +CREATE TABLE DashboardClassGroups ( + id int(11) UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT, + name varchar(50) UNIQUE NOT NULL, + code varchar(36) UNIQUE NOT NULL, + class_ids JSON NOT NULL, + + PRIMARY KEY(id) +) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PACK_KEYS=0; From c7e5dec8e05c99c3d5f51e335c4b3ad55264f781 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Tue, 17 Oct 2023 17:54:52 -0400 Subject: [PATCH 2/3] Add endpoint for getting class IDs given a dashboard group code. --- src/database.ts | 14 +++++++++++++- src/server.ts | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/database.ts b/src/database.ts index 574aaa0..57ad0b6 100644 --- a/src/database.ts +++ b/src/database.ts @@ -9,13 +9,15 @@ import { Story, StudentsClasses, Student, - DummyClass + DummyClass, + DashboardClassGroup, } from "./models"; import { createClassCode, createVerificationCode, encryptPassword, + isNumberArray, } from "./utils"; @@ -661,3 +663,13 @@ export async function getQuestionsForStory(storyName: string, newestOnly=true): type: QueryTypes.SELECT }); } + + +export async function getDashboardGroupClasses(code: string): Promise { + const group = await DashboardClassGroup.findOne({ where: { code } }); + if (group === null) { + return []; + } + const classIDs = group.class_ids; + return isNumberArray(classIDs) ? classIDs : []; +} diff --git a/src/server.ts b/src/server.ts index 9030668..362e164 100644 --- a/src/server.ts +++ b/src/server.ts @@ -28,6 +28,7 @@ import { addQuestion, currentVersionForQuestion, getQuestionsForStory, + getDashboardGroupClasses, } from "./database"; import { getAPIKey, hasPermission } from "./authorization"; @@ -652,3 +653,11 @@ app.put("/options/:studentID", async (req, res) => { } res.json(updatedOptions); }); + +app.get("/dashboard-group-classes/:code", async (req, res) => { + const classIDs = await getDashboardGroupClasses(req.params.code); + res.statusCode = classIDs.length > 0 ? 200 : 404; + res.json({ + class_ids: classIDs + }); +}); From f5e0a382b06d1c089f942a599415d406c95e3717 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Wed, 18 Oct 2023 09:39:14 -0400 Subject: [PATCH 3/3] Tweak endpoint response format. --- src/database.ts | 4 ++-- src/server.ts | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/database.ts b/src/database.ts index 57ad0b6..6eb85a1 100644 --- a/src/database.ts +++ b/src/database.ts @@ -665,10 +665,10 @@ export async function getQuestionsForStory(storyName: string, newestOnly=true): } -export async function getDashboardGroupClasses(code: string): Promise { +export async function getDashboardGroupClasses(code: string): Promise { const group = await DashboardClassGroup.findOne({ where: { code } }); if (group === null) { - return []; + return null; } const classIDs = group.class_ids; return isNumberArray(classIDs) ? classIDs : []; diff --git a/src/server.ts b/src/server.ts index 362e164..36d199f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -656,8 +656,14 @@ app.put("/options/:studentID", async (req, res) => { app.get("/dashboard-group-classes/:code", async (req, res) => { const classIDs = await getDashboardGroupClasses(req.params.code); - res.statusCode = classIDs.length > 0 ? 200 : 404; - res.json({ - class_ids: classIDs - }); + if (classIDs === null) { + res.statusCode = 404; + res.json({ + error: `Could not find a dashboard group for code ${req.params.code}` + }); + } else { + res.json({ + class_ids: classIDs + }); + } });