Skip to content

Commit

Permalink
feat(subject): get all subjects endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 12, 2024
1 parent 874d931 commit fa76a14
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/controllers/subject.controller.ts
Original file line number Diff line number Diff line change
@@ -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." },
});
}
};
2 changes: 2 additions & 0 deletions src/routes/v1/index.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions src/routes/v1/subject.route.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ interface User {
updatedAt: Date;
lastSeen?: Date;
}

export interface Subject {
name: string;
iconUrl: string;
}

0 comments on commit fa76a14

Please sign in to comment.