Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoints for Solar Eclipse 2024 story #102

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/stories/minids/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function isValidEclipseMiniData(data: any): data is EclipseMiniData {

export async function submitEclipseMiniResponse(data: EclipseMiniData): Promise<EclipseMiniResponse | null> {

logger.verbose(`Attempting to submit measurement for user ${data.user_uuid}`);
logger.verbose(`Attempting to submit annular eclipse 2023 measurement for user ${data.user_uuid}`);

const dataWithCounts = {
...data,
Expand Down
2 changes: 1 addition & 1 deletion src/stories/minids/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ router.put("/annular-eclipse-2023/response", async (req, res) => {
const response = await submitEclipseMiniResponse(data);
if (!response) {
res.status(400);
res.json({ error: "Error creating eclipse mini response" });
res.json({ error: "Error creating annular eclipse 2023 mini response" });
return;
}

Expand Down
46 changes: 46 additions & 0 deletions src/stories/solar-eclipse-2024/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { cosmicdsDB } from "../../database";
import { logger } from "../../logger";
import {
isArrayThatSatisfies,
isNumberArray,
} from "../../utils";

import { initializeModels, SolarEclipse2024Response } from "./models";

initializeModels(cosmicdsDB);

export interface SolarEclipse2024Data {
user_uuid: string;
user_selected_locations: [number, number][],
timestamp: Date
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isValidSolarEclipseData(data: any): data is SolarEclipse2024Response {
return typeof data.user_uuid === "string" &&
isArrayThatSatisfies(data.user_selected_locations, (arr) => {
return arr.every(x => isNumberArray(x) && x.length === 2);
});
}

export async function submitSolarEclipse2024Response(data: SolarEclipse2024Response): Promise<SolarEclipse2024Response | null> {

logger.verbose(`Attempting to submit solar eclipse 2024 measurement for user ${data.user_uuid}`);

const dataWithCounts = {
...data,
user_selected_locations_count: data.user_selected_locations.length
};

return SolarEclipse2024Response.upsert(dataWithCounts).then(([item, _]) => item);
}

export async function getAllSolarEclipse2024Responses(): Promise<SolarEclipse2024Response[]> {
return SolarEclipse2024Response.findAll();
}

export async function getSolarEclipse2024Response(userUUID: string): Promise<SolarEclipse2024Response | null> {
return SolarEclipse2024Response.findOne({
where: { user_uuid: userUUID }
});
}
6 changes: 6 additions & 0 deletions src/stories/solar-eclipse-2024/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import router from "./router";

module.exports = {
path: "/solar-eclipse-2024",
router
};
41 changes: 41 additions & 0 deletions src/stories/solar-eclipse-2024/models/eclipse_response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Sequelize, DataTypes, Model, InferAttributes, InferCreationAttributes, CreationOptional } from "sequelize";

export class SolarEclipse2024Response extends Model<InferAttributes<SolarEclipse2024Response>, InferCreationAttributes<SolarEclipse2024Response>> {
declare id: CreationOptional<number>;
declare user_uuid: string;
declare user_selected_locations: [number, number][];
declare user_selected_locations_count: number;
declare timestamp: CreationOptional<Date>;
}

export function initializeSolarEclipse2024ResponseModel(sequelize: Sequelize) {
SolarEclipse2024Response.init({
id: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
user_uuid: {
type: DataTypes.STRING,
unique: true,
allowNull: false
},
user_selected_locations: {
type: DataTypes.JSON,
allowNull: false
},
user_selected_locations_count: {
type: DataTypes.INTEGER,
allowNull: false
},
timestamp: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP")
}
}, {
sequelize,
engine: "InnoDB"
});
}
10 changes: 10 additions & 0 deletions src/stories/solar-eclipse-2024/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Sequelize } from "sequelize";
import { SolarEclipse2024Response, initializeSolarEclipse2024ResponseModel } from "./eclipse_response";

export {
SolarEclipse2024Response
};

export function initializeModels(db: Sequelize) {
initializeSolarEclipse2024ResponseModel(db);
}
37 changes: 37 additions & 0 deletions src/stories/solar-eclipse-2024/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Router } from "express";
import { getAllSolarEclipse2024Responses, getSolarEclipse2024Response, isValidSolarEclipseData, submitSolarEclipse2024Response } from "./database";

const router = Router();

router.put("/response", async (req, res) => {
const data = req.body;
const valid = isValidSolarEclipseData(data);

if (!valid) {
res.status(400);
res.json({ error: "Malformed response submission" });
return;
}

const response = await submitSolarEclipse2024Response(data);
if (response === null) {
res.status(400);
res.json({ error: "Error creating solar eclipse 2024 response" });
return;
}

res.json({ response });
});

router.get("/responses", async (_req, res) => {
const responses = await getAllSolarEclipse2024Responses();
res.json({ responses });
});

router.get("/response/:userUUID", async (req, res) => {
const uuid = req.params.userUUID as string;
const response = await getSolarEclipse2024Response(uuid);
res.json({ response });
});

export default router;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE SolarEclipse2024Responses (
id int(11) UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT,
user_uuid varchar(36) NOT NULL UNIQUE,
user_selected_locations JSON NOT NULL,
user_selected_locations_count INT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

PRIMARY KEY(id),
INDEX(user_uuid)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PACK_KEYS=0;
Loading