-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
94 additions
and
41 deletions.
There are no files selected for viewing
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,20 +1,21 @@ | ||
import { RequestHandler } from "express"; | ||
import { DB } from "../repositories/repository"; | ||
import { BadRequestError, NotFoundError } from "../errors/HTTPErrors"; | ||
|
||
export const getAllNews: RequestHandler = async (_, res, next) => { | ||
try { | ||
res.status(200).json(await DB.getAllNews()) | ||
} catch (err) { | ||
console.error(err) | ||
next(err) | ||
export default class NewsController { | ||
static getAllNews: RequestHandler = async (req, res, next) => { | ||
res.json(await DB.getAllNews()) | ||
} | ||
} | ||
|
||
export const getNewsById: RequestHandler = async (req, res, next) => { | ||
try { | ||
res.status(200).json(await DB.getNewsById(Number(req.params.id))) | ||
} catch (err) { | ||
console.error(err) | ||
next(err) | ||
static getNewsById: RequestHandler = async (req, res, next) => { | ||
if (!("id" in req.params)) throw new BadRequestError() | ||
const id: number = Number(req.params.id) | ||
if (isNaN(id)) throw new BadRequestError() | ||
|
||
const n = await DB.getNewsById(id) | ||
if (!n) throw new NotFoundError(`News article with id ${req.params.id} does not exist.`) | ||
|
||
res.json(await DB.getNewsById(id)) | ||
next() | ||
} | ||
} |
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,20 +1,21 @@ | ||
import { RequestHandler } from "express"; | ||
import { DB } from "../repositories/repository"; | ||
import { BadRequestError, NotFoundError } from "../errors/HTTPErrors"; | ||
|
||
export const getAllResearch: RequestHandler = async (_, res, next) => { | ||
try { | ||
res.status(200).json(DB.getAllResearch()) | ||
} catch (err) { | ||
console.error(err) | ||
next(err) | ||
export default class ResearchController { | ||
static getAllResearch: RequestHandler = async (req, res, next) => { | ||
res.json(await DB.getAllResearch()) | ||
} | ||
} | ||
|
||
export const getResearchById: RequestHandler = async (req, res, next) => { | ||
try { | ||
res.status(200).json(DB.getResearchById(Number(req.params.id))) | ||
} catch (err) { | ||
console.error(err) | ||
next(err) | ||
static getResearchById: RequestHandler = async (req, res, next) => { | ||
if (!("id" in req.params)) throw new BadRequestError() | ||
const id: number = Number(req.params.id) | ||
if (isNaN(id)) throw new BadRequestError() | ||
|
||
const n = await DB.getResearchById(id) | ||
if (!n) throw new NotFoundError(`Research article with id ${req.params.id} does not exist.`) | ||
|
||
res.json(await DB.getResearchById(id)) | ||
next() | ||
} | ||
} |
Empty file.
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,33 @@ | ||
abstract class HTTPError extends Error { | ||
public status: number | ||
|
||
protected constructor (status: number) { | ||
super() | ||
this.status = status | ||
this.name = "HTTPError" | ||
} | ||
} | ||
|
||
export class NotFoundError extends HTTPError { | ||
constructor (message?: string) { | ||
super(404) | ||
this.message = message?? "Not Found" | ||
this.name = this.status.toString() | ||
} | ||
} | ||
|
||
export class UnauthorizedError extends HTTPError { | ||
constructor(message?: string) { | ||
super(401) | ||
this.message = message ?? "Unauthorized" | ||
this.name = this.status.toString() | ||
} | ||
} | ||
|
||
export class BadRequestError extends HTTPError { | ||
constructor(message?: string) { | ||
super(400) | ||
this.message = message ?? "Bad Request" | ||
this.name = this.status.toString() | ||
} | ||
} |
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
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,8 +1,15 @@ | ||
import express, { Router } from "express"; | ||
import { getAllNews, getNewsById } from "../controllers/news.controller"; | ||
import NewsController from "../controllers/news.controller"; | ||
|
||
export const newsRouter: Router = express.Router() | ||
export const newsURL = "news" | ||
export default class NewsRouter { | ||
static url = "/content/news" | ||
|
||
newsRouter.get("/", getAllNews) | ||
newsRouter.get("/:id", getNewsById) | ||
static router (): Router { | ||
const router = express.Router() | ||
|
||
router.get("/", NewsController.getAllNews) | ||
router.get("/:id", NewsController.getNewsById) | ||
|
||
return 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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import express, { Router } from "express"; | ||
import { getAllResearch, getResearchById } from "../controllers/research.controller"; | ||
import ResearchController from "../controllers/research.controller"; | ||
|
||
export const researchRouter: Router = express.Router() | ||
export const researchURL = "research" | ||
export default class ResearchRouter { | ||
static url = "/content/research" | ||
|
||
researchRouter.get("/", getAllResearch) | ||
researchRouter.get("/:id", getResearchById) | ||
static router(): Router { | ||
const router = express.Router() | ||
|
||
router.get("/", ResearchController.getAllResearch) | ||
router.get("/:id", ResearchController.getResearchById) | ||
|
||
return router | ||
} | ||
} |