Skip to content

Commit

Permalink
Get courses by language
Browse files Browse the repository at this point in the history
  • Loading branch information
laurogripa committed Oct 10, 2024
1 parent b8cfdbe commit a32f63b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "dist/index.js",
"scripts": {
"dev": "nodemon",
"test": "bun run test:mongodb:setup; NODE_ENV=test jest tests/lessons.test.ts; bun run test:mongodb:rm",
"test": "bun run test:mongodb:setup; NODE_ENV=test jest; bun run test:mongodb:rm",
"test:mongodb:setup": "bun run test:mongodb:rm; bun run test:mongodb:run",
"test:mongodb:rm": "docker rm -f doteducation-mongodb-test",
"test:mongodb:run": "docker run --name doteducation-mongodb-test -p 27777:27017 -dti mongo:7.0.5-jammy",
Expand Down
27 changes: 27 additions & 0 deletions src/controllers/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,33 @@ export const getCourse = async (req: Request, res: Response) => {
});
};

export const getCoursesByLanguage = async (req: Request, res: Response) => {
try {
const { language } = req.query;
if (!language) {
return res.status(400).send({ error: { message: "Missing language" } });
}

const courses = await CourseModel.find({ language }).populate("modules");
if (courses.length > 0) {
return res.status(200).send(courses);
} else {
return res.status(404).send({
error: {
message: "No courses found for this language",
},
});
}
} catch (e) {
console.error(`[ERROR][getCoursesByLanguage] ${JSON.stringify(e)}`);
return res.status(500).send({
error: {
message: JSON.stringify(e),
},
});
}
};

export const deleteCourse = async (req: Request, res: Response) => {
try {
const { courseId } = req.body;
Expand Down
10 changes: 9 additions & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import {
} from "@/controllers/users";
import { createLesson, deleteLesson, getLesson, getLessonsByLanguage, updateLesson } from "@/controllers/lessons";
import { createModule, deleteModule, getModule, updateModule } from "@/controllers/modules";
import { createCourse, deleteCourse, getCourse, updateCourse, duplicateCourse } from "@/controllers/courses";
import {
createCourse,
deleteCourse,
getCourse,
updateCourse,
duplicateCourse,
getCoursesByLanguage,
} from "@/controllers/courses";

import authMiddleware from "./middlewares/auth";

Expand Down Expand Up @@ -42,6 +49,7 @@ const router = (app: Express) => {
// Courses
app.post("/course", [authMiddleware], createCourse);
app.get("/course", [authMiddleware], getCourse);
app.get("/courses", [authMiddleware], getCoursesByLanguage);
app.delete("/course", [authMiddleware], deleteCourse);
app.put("/course/:id", [authMiddleware], updateCourse);
app.post("/course/duplicate", [authMiddleware], duplicateCourse);
Expand Down
80 changes: 80 additions & 0 deletions tests/courses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,86 @@ describe("Setting API Server up...", () => {
.catch((e) => expect(e).toBeUndefined());
});

it("Get courses by language (GET /courses?language=english)", async () => {
await CourseModel.deleteMany({});

const lesson1 = await LessonModel.create({
title: "Lesson in English #1",
language: "english",
body: loadFixture("example.md"),
difficulty: "easy",
challenge: {
question: "What is the capital of the USA?",
choices: ["Washington D.C.", "New York", "Los Angeles", "Chicago"],
correctChoice: 0,
},
});

const lesson2 = await LessonModel.create({
title: "Aula em Português",
language: "portuguese",
body: loadFixture("example.md"),
difficulty: "medium",
challenge: {
question: "Qual é a capital do Brasil?",
choices: ["Brasília", "Rio de Janeiro", "São Paulo"],
correctChoice: 0,
},
});

const moduleEnglish = await ModuleModel.create({
title: "Module in English",
lessons: [lesson1._id],
});

const modulePortuguese = await ModuleModel.create({
title: "Módulo em Português",
lessons: [lesson2._id],
});

await CourseModel.create({
title: "Course in English",
language: "english",
summary: "This is an English course",
modules: [moduleEnglish._id],
});

await CourseModel.create({
title: "Curso em Português",
language: "portuguese",
summary: "Este é um curso em Português",
modules: [modulePortuguese._id],
});

await axios
.get(`${API_URL}/courses?language=english`)
.then((r) => {
expect(r.data.length).toBe(1);
expect(r.data[0].title).toEqual("Course in English");
})
.catch((e) => expect(e).toBeUndefined());
});

it("Get courses by language with no results (GET /courses?language=french)", async () => {
await axios
.get(`${API_URL}/courses?language=french`)
.then(() => {})
.catch((e) => {
expect(e.response.status).toEqual(404);
expect(e.response.data.error.message).toEqual("No courses found for this language");
});
});

it("Get courses by language without specifying language (GET /courses)", async () => {
await axios
.get(`${API_URL}/courses`)
.then(() => {})
.catch((e) => {
expect(e.response.status).toEqual(400);
expect(e.response.data.error.message).toEqual("Missing language");
});
});

it("Delete a Course (DELETE /course)", async () => {
const lesson = await LessonModel.create({
title: "Lesson #4",
Expand Down

0 comments on commit a32f63b

Please sign in to comment.