-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
Ticket 4: design doc -- judging algo refresh
This ticket, unlike its predecessors for the judging refresh, has a LOT to do (we are both writing code and testing it). Please be sure to start early.
-
a. Before proceeding, read up on RESTful API characteristics and CRUD operations.
-
b. In the internal-tools/judging-algorithm/data repository, create the following 3 folders:
- models - convert each schema in to a model using mongoose
- dao - build a mongo data access object to control collections using their mongoose models
- controllers - do CRUD operations as requested by client code (IE the UI written in the mono-repo)
- c. Write
judges-model.tsusing the following example
import monogoose from "mongoose";
import groupsSchema from "./groups-schema";
const groupsModel = monogoose.model("groups", groupsSchema);
export default groupsModel;
- d. Write
judges-dao.tsfile using the following example. Implement:
- Create: making new entries to each collection
- Read: all entries in each collection, by ID, by name
- Delete: removing all entries in each collection, removing by ID
import groupsModel from "./groups-model";
type groupType = {
name: string;
description: string;
image: string;
}
export const getGroups = async () =>
await groupsModel.find();
export const getGroupById = async (groupId: string) =>
await groupsModel.find({_id: groupId});
export const getGroupByName =async (groupName: string) =>
await groupsModel.findOne({name: groupName});
export const updateGroup = async (groupId: string, group: groupType) =>
await groupsModel.updateOne({_id: groupId}, group);
- e. Write a
judges-controller.tsfile that builds an interface of actions through the DAO, and constructs endpointsapi/xxxto complete required CRUD operations. Example:
import * as groupsDao from "./groups-dao";
const GroupsController = (app) => {
const getGroups = async (req, res) => {
const groups = await groupsDao.getGroups();
res.json(groups);
};
const getGroupById = async (req, res) => {
const groupId = req.params.groupId;
const group = await groupsDao.getGroupById(groupId);
res.json(group);
};
const updateGroup = async (req, res) => {
const group = req.body;
const update = await groupsDao.updateGroup(group._id, group);
res.json(update);
return update;
};
app.get("/api/groups", getGroups);
app.get("/api/groups/:groupId", getGroupById);
app.put("/api/groups", updateGroup);
};
export default GroupsController;
- f. Test all endpoints by constructing test files in the judging-algorithm folder that go through the following cycle:
- Set up a Mongo connection
- Populate the DB with mock data and assert it is all present
- Read the mock data (by a particular ID, by a particular name)
- Delete the mock data and assert it can be deleted individually and collectively
- Close your Mongo connection to tear down your instance