Skip to content

Commit

Permalink
Add endpoints and database functions for getting and creating/updatin…
Browse files Browse the repository at this point in the history
…g stage states.
  • Loading branch information
Carifio24 committed Jun 14, 2024
1 parent 8b1f162 commit 2af8823
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
53 changes: 46 additions & 7 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Student,
DummyClass,
DashboardClassGroup,
StageState,
} from "./models";

import {
Expand Down Expand Up @@ -347,35 +348,73 @@ export async function getStoryState(studentID: number, storyName: string): Promi
console.log(error);
return null;
});
return result?.story_state || null;
return result?.story_state ?? null;
}

export async function updateStoryState(studentID: number, storyName: string, newState: JSON): Promise<JSON | null> {
const query = {
student_id: studentID,
story_name: storyName,
};
let result = await StoryState.findOne({
where: query
})
.catch(error => {
console.log(error);
return null;
});

const storyData = { ...query, story_state: newState };
if (result !== null) {
result?.update(storyData);
} else {
result = await StoryState.create(storyData).catch(error => {
console.log(error);
return null;
});
}
return result?.story_state ?? null;
}

export async function getStageState(studentID: number, storyName: string, stageName: string): Promise<JSON | null> {
const result = await StageState.findOne({
where: {
student_id: studentID,
story_name: storyName
story_name: storyName,
stage_name: stageName,
}
})
.catch(error => {
console.log(error);
return null;
});
return result?.state ?? null;
}

const storyData = {
export async function updateStageState(studentID: number, storyName: string, stageName: string, newState: JSON): Promise<JSON | null> {
const query = {
student_id: studentID,
story_name: storyName,
story_state: newState
stage_name: stageName,
};
let result = await StageState.findOne({
where: query
})
.catch(error => {
console.log(error);
return null;
});

const data = { ...query, state: newState };
if (result !== null) {
result?.update(storyData);
result?.update(data);
} else {
result = await StoryState.create(storyData).catch(error => {
result = await StageState.create(data).catch(error => {
console.log(error);
return null;
});
}
return result?.story_state || null;
return result?.state ?? null;
}

export async function getClassesForEducator(educatorID: number): Promise<Class[]> {
Expand Down
3 changes: 3 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Educator, initializeEducatorModel } from "./educator";
import { IgnoreStudent, initializeIgnoreStudentModel } from "./ignore_student";
import { ClassStories, initializeClassStoryModel } from "./story_class";
import { CosmicDSSession, initializeSessionModel } from "./session";
import { StageState, initializeStageStateModel } from "./stage_state";
import { StoryState, initializeStoryStateModel } from "./story_state";
import { Story, initializeStoryModel } from "./story";
import { StudentsClasses, initializeStudentClassModel } from "./student_class";
Expand All @@ -22,6 +23,7 @@ export {
DummyClass,
Educator,
IgnoreStudent,
StageState,
Story,
StoryState,
Student,
Expand All @@ -36,6 +38,7 @@ export function initializeModels(db: Sequelize) {
initializeStoryModel(db);
initializeClassStoryModel(db);
initializeDummyClassModel(db);
initializeStageStateModel(db);
initializeStoryStateModel(db);
initializeStudentClassModel(db);
initializeStudentOptionsModel(db);
Expand Down
2 changes: 1 addition & 1 deletion src/models/stage_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class StageState extends Model<InferAttributes<StageState>, InferCreation
declare last_modified: CreationOptional<Date>;
}

export function initializeStoryStateModel(sequelize: Sequelize) {
export function initializeStageStateModel(sequelize: Sequelize) {
StageState.init({
student_id: {
type: DataTypes.INTEGER.UNSIGNED,
Expand Down
37 changes: 35 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
currentVersionForQuestion,
getQuestionsForStory,
getDashboardGroupClasses,
getStageState,
updateStageState,
} from "./database";

import { getAPIKey, hasPermission } from "./authorization";
Expand Down Expand Up @@ -423,7 +425,7 @@ app.get("/story-state/:studentID/:storyName", async (req, res) => {
res.status(status).json({
student_id: studentID,
story_name: storyName,
state: state
state
});
});

Expand All @@ -437,7 +439,38 @@ app.put("/story-state/:studentID/:storyName", async (req, res) => {
res.status(status).json({
student_id: studentID,
story_name: storyName,
state: state
state
});
});

app.get("/stage-state/:studentID/:storyName/:stageName", async (req, res) => {
const params = req.params;
const studentID = Number(params.studentID);
const storyName = params.storyName;
const stageName = params.stageName;
const state = await getStageState(studentID, storyName, stageName);
const status = state !== null ? 200 : 404;
res.status(status).json({
student_id: studentID,
story_name: storyName,
stage_name: stageName,
state
});
});

app.put("/stage-state/:studentID/:storyName/:stageName", async (req, res) => {
const params = req.params;
const studentID = Number(params.studentID);
const storyName = params.storyName;
const stageName = params.stageName;
const newState = req.body;
const state = await updateStageState(studentID, storyName, stageName, newState);
const status = state !== null ? 200 : 404;
res.status(status).json({
student_id: studentID,
story_name: storyName,
stage_name: stageName,
state
});
});

Expand Down

0 comments on commit 2af8823

Please sign in to comment.