Skip to content

Commit c4e5b0b

Browse files
authored
Merge branch 'main' into feature/arohanrachel/our-impact
2 parents 81ebc14 + f5e0fc3 commit c4e5b0b

36 files changed

+1227
-489
lines changed

.husky/lint-config.sh

Lines changed: 0 additions & 23 deletions
This file was deleted.

.husky/pre-commit

Lines changed: 0 additions & 216 deletions
This file was deleted.

.husky/pre-push

Lines changed: 0 additions & 3 deletions
This file was deleted.

backend/src/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import "dotenv/config";
66
import cors from "cors";
77
import express, { NextFunction, Request, Response } from "express";
88
import { isHttpError } from "http-errors";
9+
import subscriberRoutes from "src/routes/subscriber";
10+
import memberRoutes from "src/routes/members";
911

1012
const app = express();
1113

@@ -24,6 +26,8 @@ app.use(
2426
);
2527

2628
// Routes ( e.g. app.use("/api/task", taskRoutes); )
29+
app.use("/api/subscribers", subscriberRoutes);
30+
app.use("/api/member", memberRoutes);
2731

2832
/**
2933
* Error handler; all errors thrown by server are handled here.

backend/src/controllers/member.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { RequestHandler } from "express";
2+
import MemberModel from "src/models/member";
3+
import { Types } from "mongoose";
4+
5+
export const createMember: RequestHandler = async (req, res, next) => {
6+
const { name, role, profilePictureURL } = req.body;
7+
try {
8+
const member = await MemberModel.create({
9+
name: name,
10+
role: role,
11+
profilePictureURL: profilePictureURL,
12+
});
13+
res.status(201).json(member);
14+
} catch (error) {
15+
next(error);
16+
}
17+
};
18+
19+
export const getMember: RequestHandler = async (req, res, next) => {
20+
const { id } = req.params;
21+
try {
22+
const member = Types.ObjectId.isValid(id) ? await MemberModel.findById(id) : null;
23+
res.status(200).json(member);
24+
} catch (error) {
25+
next(error);
26+
}
27+
};
28+
29+
export const getAllMembers: RequestHandler = async (req, res, next) => {
30+
try {
31+
const members = await MemberModel.find({});
32+
res.status(200).json(members);
33+
} catch (error) {
34+
next(error);
35+
}
36+
};

backend/src/controllers/subscriber.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Controller for the newsletter subscriber route, /api/subscribers.
3+
* passes error handling off to /src/util/validationErrorParser.ts
4+
*/
5+
6+
import { RequestHandler } from "express";
7+
import { validationResult } from "express-validator";
8+
import Subscriber from "src/models/subscriber";
9+
import validationErrorParser from "src/util/validationErrorParser";
10+
11+
export const createSubscriber: RequestHandler = async (req, res, next) => {
12+
const errors = validationResult(req);
13+
const { email } = req.body;
14+
15+
try {
16+
// validationErrorParser is a helper that throws 400 if there are errors
17+
validationErrorParser(errors);
18+
const subscriber = await Subscriber.create({
19+
email: email,
20+
});
21+
22+
/*
23+
* TODO: Handle adding the newsletter subscriber
24+
* to a mailing list or however this will be handled.
25+
*/
26+
27+
// successfully created subscriber in db
28+
res.status(201).json(subscriber);
29+
} catch (error) {
30+
next(error);
31+
}
32+
};

backend/src/models/member.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { InferSchemaType, Schema, model } from "mongoose";
2+
3+
const memberSchema = new Schema({
4+
name: { type: String, required: true },
5+
role: { type: String, required: true },
6+
profilePictureURL: { type: String },
7+
});
8+
9+
type Member = InferSchemaType<typeof memberSchema>;
10+
11+
export default model<Member>("Member", memberSchema);

backend/src/models/subscriber.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Defines the schema for a newsletter subscriber
3+
*/
4+
5+
import { InferSchemaType, Schema, model } from "mongoose";
6+
7+
const subscriberSchema = new Schema({
8+
email: { type: String, required: true },
9+
});
10+
11+
type Subscriber = InferSchemaType<typeof subscriberSchema>;
12+
13+
export default model<Subscriber>("Subscriber", subscriberSchema);

backend/src/routes/members.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import express from "express";
2+
import * as MembersController from "src/controllers/member";
3+
4+
const router = express.Router();
5+
6+
router.get("/get", MembersController.getAllMembers);
7+
router.post("/post", MembersController.createMember);
8+
9+
export default router;

backend/src/routes/subscriber.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Newsletter subscription route requests.
3+
*/
4+
5+
import express from "express";
6+
import * as SubscriberController from "src/controllers/subscriber";
7+
import * as SubscriberValidator from "src/validators/subscriber";
8+
9+
const router = express.Router();
10+
11+
router.post("/", SubscriberValidator.createSubscriber, SubscriberController.createSubscriber);
12+
13+
export default router;

0 commit comments

Comments
 (0)