Skip to content

Commit

Permalink
backend routes (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineychen8 authored Nov 10, 2024
1 parent b7efcd5 commit a06f466
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
48 changes: 48 additions & 0 deletions server/src/controllers/chapter.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable import/prefer-default-export */
import express from 'express';
import ApiError from '../util/apiError.ts';
import StatusCode from '../util/statusCode.ts';
import {
toggleRequestByID,
getAllChaptersFromDB,
} from '../services/chapter.service.ts';

const getAllChapters = async (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
return (
getAllChaptersFromDB()
.then((userList) => {
res.status(StatusCode.OK).send(userList);
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch((e) => {
next(ApiError.internal('Unable to retrieve all users'));
})
);
};

const toggleRequest = async (
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
const { id } = req.params;
if (!id) {
next(ApiError.missingFields(['id']));
return;
}

toggleRequestByID(id)
.then(() => {
res.sendStatus(StatusCode.OK);
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch((e) => {
next(ApiError.internal('Unable to toggle status.'));
});
};

export { toggleRequest, getAllChapters };
17 changes: 17 additions & 0 deletions server/src/routes/chapter.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import express from 'express';
import { isAdmin } from '../controllers/admin.middleware.ts';
import {
toggleRequest,
getAllChapters,
} from '../controllers/chapter.controller.ts';
import { isAuthenticated } from '../controllers/auth.middleware.ts';
import 'dotenv/config';

const router = express.Router();

router.put('/toggleRequests/:id', isAuthenticated, isAdmin, toggleRequest);

router.get('/all', isAuthenticated, isAdmin, getAllChapters);

export default router;
5 changes: 5 additions & 0 deletions server/src/routes/routers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The prefix should be of the form '/api/ROUTERNAME'
import { Router } from 'express';
import adminRouter from './admin.route.ts';
import authRouter from './auth.route.ts';
import chapterRouter from './chapter.route.ts';

const prefixToRouterMap: { prefix: string; router: Router }[] = [
{
Expand All @@ -19,6 +20,10 @@ const prefixToRouterMap: { prefix: string; router: Router }[] = [
prefix: '/api/admin',
router: adminRouter,
},
{
prefix: '/api/chapter',
router: chapterRouter,
},
];

export default prefixToRouterMap;
25 changes: 25 additions & 0 deletions server/src/services/chapter.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable import/prefer-default-export */
import { Chapter } from '../models/chapter.model.ts';

const removeSensitiveDataQuery = [
'-password',
'-verificationToken',
'-resetPasswordToken',
'-resetPasswordTokenExpiryDate',
];

const toggleRequestByID = async (id: string) => {
const chapter = await Chapter.findByIdAndUpdate(id, [
{ $set: { isAcceptingRequests: { $not: '$isAcceptingRequests' } } },
]).exec();
return chapter;
};

const getAllChaptersFromDB = async () => {
const userList = await Chapter.find({})
.select(removeSensitiveDataQuery)
.exec();
return userList;
};

export { toggleRequestByID, getAllChaptersFromDB };

0 comments on commit a06f466

Please sign in to comment.