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/lisasiliu/edit-programs-form-backend-route #68

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 25 additions & 1 deletion backend/src/controllers/program.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import { RequestHandler } from "express";
import { validationResult } from "express-validator";
//import { error } from "firebase-functions/logger";

import ProgramFormModel from "../models/program-form";
import validationErrorParser from "../util/validationErrorParser";

export type Program = {
_id: string;
name: string;
abbreviation: string;
type: string;
Expand All @@ -15,7 +17,7 @@ export type Program = {
color: string; //colorValueHex;
};

export const createForm: RequestHandler = async (req, res, next) => {
export const createProgram: RequestHandler = async (req, res, next) => {
const errors = validationResult(req);

try {
Expand All @@ -28,3 +30,25 @@ export const createForm: RequestHandler = async (req, res, next) => {
next(error);
}
};

export const updateProgram: RequestHandler = async (req, res, next) => {
const errors = validationResult(req);
try {
validationErrorParser(errors);

const programId = req.params.id;
const programData = req.body as Program;

const editedProgram = await ProgramFormModel.findOneAndUpdate({ _id: programId }, programData, {
new: true,
});

if (!editedProgram) {
return res.status(404).json({ message: "No object in database with provided ID" });
}

res.status(200).json(editedProgram);
} catch (error) {
next(error);
}
};
3 changes: 2 additions & 1 deletion backend/src/routes/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as ProgramValidator from "../validators/program";

const router = express.Router();

router.post("/", ProgramValidator.createForm, ProgramController.createForm);
router.post("/", ProgramValidator.createProgram, ProgramController.createProgram);
router.patch("/:id", ProgramValidator.updateProgram, ProgramController.updateProgram);

export default router;
13 changes: 11 additions & 2 deletions backend/src/validators/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,18 @@ const makeColorValidator = () =>
}
return true;
});
// check for first chara being # and others being 1-F

export const createProgram = [
makeNameValidator(),
makeAbbreviationValidator(),
makeTypeValidator(),
makeDaysOfWeekValidator(),
makeStartDateValidator(),
makeEndDateValidator(),
makeColorValidator(),
];

export const createForm = [
export const updateProgram = [
makeNameValidator(),
makeAbbreviationValidator(),
makeTypeValidator(),
Expand Down
Loading
Loading