Skip to content

Commit

Permalink
feat(subjects): get all subjects that are available for selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 28, 2024
1 parent 2d882dd commit 77107f7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/module/subject/subject.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,20 @@ export const getPopularSubjects: Controller = async (_req, res) => {
});
}
};

export const getAvailableSubjects: Controller = async (req, res) => {
try {
const subjects = await subjectService.getAvailableSubjects(req.tutor.id);

res.json({
status: "success",
data: subjects,
});
} catch (error) {
logger.error(`Error when getting available subjects: ${error}`);
res.status(400).json({
status: "fail",
message: "Failed to get available subjects",
});
}
};
27 changes: 26 additions & 1 deletion src/module/subject/subject.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { db as dbType } from "@/db/config";
import { desc, eq, inArray, sql } from "drizzle-orm";
import { subjects, tutories } from "@/db/schema";
import { and, desc, eq, inArray, notExists, sql } from "drizzle-orm";

export class SubjectRepository {
constructor(private readonly db: typeof dbType) {}
Expand Down Expand Up @@ -35,6 +35,31 @@ export class SubjectRepository {
}));
}

public async getAvailableSubjects(tutorId: string) {
const results = await this.db
.select()
.from(subjects)
.where(
notExists(
this.db
.select()
.from(tutories)
.where(
and(
eq(tutories.subjectId, subjects.id),
eq(tutories.tutorId, tutorId),
),
),
),
);

return results.map((subject) => ({
id: subject.id,
name: subject.name,
iconUrl: subject.iconUrl,
}));
}

public async createSubject(name: string, iconUrl: string) {
const [subjectId] = await this.db
.insert(subjects)
Expand Down
10 changes: 10 additions & 0 deletions src/module/subject/subject.route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as subjectController from "@/module/subject/subject.controller";
import { Router } from "express";
import { jwtAuthMiddleware, verifyTutor } from "../auth/auth.middleware";

// /api/v1/subjects
const subjectRouter = Router();
Expand All @@ -17,4 +18,13 @@ subjectRouter.get(
subjectController.getPopularSubjects,
);

subjectRouter.get(
"/available",
// #swagger.tags = ['subjects']
// #swagger.description = 'Get subjects that are available for selection, excluding subjects that are already selected by the tutor'
jwtAuthMiddleware,
verifyTutor,
subjectController.getAvailableSubjects,
);

export default subjectRouter;
8 changes: 8 additions & 0 deletions src/module/subject/subject.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export class SubjectService {
}
}

public async getAvailableSubjects(tutorId: string) {
try {
return await this.subjectRepository.getAvailableSubjects(tutorId);
} catch (error) {
throw new Error(`Error when getting available subjects: ${error}`);
}
}

public async checkSubjectExists(subjectId: string) {
return this.subjectRepository.checkSubjectExists(subjectId);
}
Expand Down

0 comments on commit 77107f7

Please sign in to comment.