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

[Feature] Add RESTful endpoints for judge data in mongo #111

Open
surbhi-gulati opened this issue May 17, 2023 · 0 comments
Open

[Feature] Add RESTful endpoints for judge data in mongo #111

surbhi-gulati opened this issue May 17, 2023 · 0 comments
Assignees
Labels

Comments

@surbhi-gulati
Copy link
Contributor

surbhi-gulati commented May 17, 2023

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:

  1. models - convert each schema in to a model using mongoose
  2. dao - build a mongo data access object to control collections using their mongoose models
  3. controllers - do CRUD operations as requested by client code (IE the UI written in the mono-repo)
  • c. Write judges-model.ts using 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.ts file using the following example. Implement:
  1. Create: making new entries to each collection
  2. Read: all entries in each collection, by ID, by name
  3. 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.ts file that builds an interface of actions through the DAO, and constructs endpoints api/xxx to 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:
  1. Set up a Mongo connection
  2. Populate the DB with mock data and assert it is all present
  3. Read the mock data (by a particular ID, by a particular name)
  4. Delete the mock data and assert it can be deleted individually and collectively
  5. Close your Mongo connection to tear down your instance
@surbhi-gulati surbhi-gulati changed the title [Judging algo] Add RESTful endpoints for judge data in mongo [Feature] Add RESTful endpoints for judge data in mongo May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants