From fa76a14252b11c81c15bf629511a4afd2923271a Mon Sep 17 00:00:00 2001 From: Veirt Date: Wed, 13 Nov 2024 03:28:30 +0800 Subject: [PATCH] feat(subject): get all subjects endpoint --- src/controllers/subject.controller.ts | 18 ++++++++++++++++++ src/routes/v1/index.ts | 2 ++ src/routes/v1/subject.route.ts | 9 +++++++++ src/types.ts | 5 +++++ 4 files changed, 34 insertions(+) create mode 100644 src/controllers/subject.controller.ts create mode 100644 src/routes/v1/subject.route.ts diff --git a/src/controllers/subject.controller.ts b/src/controllers/subject.controller.ts new file mode 100644 index 0000000..c7fd407 --- /dev/null +++ b/src/controllers/subject.controller.ts @@ -0,0 +1,18 @@ +import { firestore } from "../config"; +import { logger } from "../middleware/logging.middleware"; +import { Controller, Subject } from "../types"; + +export const getAllSubjects: Controller = async (_req, res) => { + try { + const subjects = await firestore.collection("subjects").get(); + const data = subjects.docs.map((doc) => doc.data() as Subject); + + res.json({ status: "success", data }); + } catch (error) { + logger.error(`Error when getting all subjects: ${error}`); + res.status(400).json({ + status: "fail", + data: { message: "Failed to get all subjects." }, + }); + } +}; diff --git a/src/routes/v1/index.ts b/src/routes/v1/index.ts index c5069d1..c990ae3 100644 --- a/src/routes/v1/index.ts +++ b/src/routes/v1/index.ts @@ -1,8 +1,10 @@ import { Router } from "express"; import authRouter from "./auth.route"; +import subjectRouter from "./subject.route"; const v1Router = Router(); v1Router.use("/auth", authRouter); +v1Router.use("/subjects", subjectRouter); export default v1Router; diff --git a/src/routes/v1/subject.route.ts b/src/routes/v1/subject.route.ts new file mode 100644 index 0000000..347a718 --- /dev/null +++ b/src/routes/v1/subject.route.ts @@ -0,0 +1,9 @@ +import { Router } from "express"; +import * as subjectController from "@controllers/subject.controller"; + +// /api/v1/subjects +const subjectRouter = Router(); + +subjectRouter.get("/", subjectController.getAllSubjects); + +export default subjectRouter; diff --git a/src/types.ts b/src/types.ts index fd5f9b0..44ed52c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -25,3 +25,8 @@ interface User { updatedAt: Date; lastSeen?: Date; } + +export interface Subject { + name: string; + iconUrl: string; +}