Skip to content

Commit

Permalink
reorganize project
Browse files Browse the repository at this point in the history
  • Loading branch information
HuzzNZ committed Apr 1, 2024
1 parent b3f17db commit cabd9fa
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 41 deletions.
27 changes: 14 additions & 13 deletions backend/src/controllers/news.controller.ts
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()
}
}
27 changes: 14 additions & 13 deletions backend/src/controllers/research.controller.ts
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.
33 changes: 33 additions & 0 deletions backend/src/errors/HTTPErrors.ts
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()
}
}
10 changes: 6 additions & 4 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv";
import { newsRouter, newsURL } from "./routes/news.router";
import { researchRouter, researchURL } from "./routes/research.router";
import NewsRouter from "./routes/news.router";
import ResearchRouter from "./routes/research.router";

dotenv.config()

const app: Express = express()
const port = process.env.PORT || 3000

app.use(express.json())

app.get("/", (req: Request, res: Response) => {
res.json({ message: "ok" })
})

app.use(`/content/${newsURL}`, newsRouter)
app.use(`/content/${researchURL}`, researchRouter)
app.use(NewsRouter.url, NewsRouter.router())
app.use(ResearchRouter.url, ResearchRouter.router())


app.listen(port, () => {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/repositories/memory/MemoryRepository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Article, User, IArticle } from "@aapc/types";
import { Article, IArticle, User } from "@aapc/types";
import IRepository from "../IRepository";
import users from "./data/users.json"
import news from "./data/news.json"
import researches from "./data/researches.json"
import { Nullable } from "../../util/types";

// interface IArticleInput extends Omit<IArticle, "id" | "lastEditedAt" | "publishedAt" | "publisher"> {}

export default class MemoryRepository implements IRepository {
private readonly users: User[]
private readonly news: Article[]
Expand Down
17 changes: 12 additions & 5 deletions backend/src/routes/news.router.ts
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
}
}
17 changes: 12 additions & 5 deletions backend/src/routes/research.router.ts
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
}
}

0 comments on commit cabd9fa

Please sign in to comment.