-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from TritonSE/feature/arnav-jacob/page-footer
Feature/arnav jacob/page footer
- Loading branch information
Showing
26 changed files
with
951 additions
and
484 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Controller for the newsletter subscriber route, /api/subscribers. | ||
* passes error handling off to /src/util/validationErrorParser.ts | ||
*/ | ||
|
||
import { RequestHandler } from "express"; | ||
import { validationResult } from "express-validator"; | ||
import Subscriber from "src/models/subscriber"; | ||
import validationErrorParser from "src/util/validationErrorParser"; | ||
|
||
export const createSubscriber: RequestHandler = async (req, res, next) => { | ||
const errors = validationResult(req); | ||
const { email } = req.body; | ||
|
||
try { | ||
// validationErrorParser is a helper that throws 400 if there are errors | ||
validationErrorParser(errors); | ||
const subscriber = await Subscriber.create({ | ||
email: email, | ||
}); | ||
|
||
/* | ||
* TODO: Handle adding the newsletter subscriber | ||
* to a mailing list or however this will be handled. | ||
*/ | ||
|
||
// successfully created subscriber in db | ||
res.status(201).json(subscriber); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Defines the schema for a newsletter subscriber | ||
*/ | ||
|
||
import { InferSchemaType, Schema, model } from "mongoose"; | ||
|
||
const subscriberSchema = new Schema({ | ||
email: { type: String, required: true }, | ||
}); | ||
|
||
type Subscriber = InferSchemaType<typeof subscriberSchema>; | ||
|
||
export default model<Subscriber>("Subscriber", subscriberSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Newsletter subscription route requests. | ||
*/ | ||
|
||
import express from "express"; | ||
import * as SubscriberController from "src/controllers/subscriber"; | ||
import * as SubscriberValidator from "src/validators/subscriber"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post("/", SubscriberValidator.createSubscriber, SubscriberController.createSubscriber); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Result, ValidationError } from "express-validator"; | ||
import createHttpError from "http-errors"; | ||
|
||
/** | ||
* Parses through errors thrown by validator (if any exist). Error messages are | ||
* added to a string and that string is used as the error message for the HTTP | ||
* error. | ||
* | ||
* @param errors the validation result provided by express validator middleware | ||
*/ | ||
const validationErrorParser = (errors: Result<ValidationError>) => { | ||
if (!errors.isEmpty()) { | ||
let errorString = ""; | ||
|
||
// parse through errors returned by the validator and append them to the error string | ||
for (const error of errors.array()) { | ||
errorString += error.msg + " "; | ||
} | ||
|
||
// trim removes the trailing space created in the for loop | ||
throw createHttpError(400, errorString.trim()); | ||
} | ||
}; | ||
|
||
export default validationErrorParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Normalize and validate emails for newsletter subscribers | ||
* in the route handler. | ||
*/ | ||
|
||
import { body } from "express-validator"; | ||
import Subscriber from "src/models/subscriber"; | ||
|
||
/* | ||
* 1. Trim whitespace then | ||
* 2. check if empty then | ||
* 3. check if valid email then | ||
* 4. normalize email then | ||
* 5. check if email already exists in db | ||
*/ | ||
const makeEmailValidator = () => | ||
body("email") | ||
.trim() | ||
.exists() | ||
.withMessage("email is required") | ||
.bail() | ||
.isEmail() | ||
.withMessage("email must be a valid email address") | ||
.bail() | ||
.normalizeEmail() | ||
.custom(async (value) => { | ||
// check if email already exists in db | ||
const subscriber = await Subscriber.findOne({ email: value }).exec(); | ||
if (subscriber !== null) { | ||
return Promise.reject(`email is already subscribed`); | ||
} | ||
}); | ||
|
||
export const createSubscriber = [makeEmailValidator()]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"printWidth": 100 | ||
"printWidth": 100, | ||
"trailingComma": "all" | ||
} |
Oops, something went wrong.