Skip to content

Commit

Permalink
Add Courses
Browse files Browse the repository at this point in the history
Courses organize related sentence lists together

Signed-off-by: Abhishek Kumar <[email protected]>
  • Loading branch information
abhi-kr-2100 committed Apr 7, 2024
1 parent ff26c9c commit 28294c1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
15 changes: 15 additions & 0 deletions express-backend/src/api/courses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Request, Response } from 'express';

import { Router } from 'express';
import Course from '../course';

const router = Router();

export async function getCourses(req: Request, res: Response) {
const courses = await Course.find({});
res.json(courses);
}

router.get('/', getCourses);

export default router;
11 changes: 11 additions & 0 deletions express-backend/src/course/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { InferSchemaType, Schema, model } from 'mongoose';
import { sentenceListSchema } from '../sentence-list';

export const courseSchema = new Schema({
title: { type: String, required: true },
sentenceLists: { type: [sentenceListSchema], default: [] },
});

export type CourseType = InferSchemaType<typeof courseSchema>;

export default model('Course', courseSchema);
2 changes: 2 additions & 0 deletions express-backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import createUserProfile from './middlewares/createUserProfile';
import SentenceListRoutes from './api/sentenceLists';
import SentenceRoutes from './api/sentences';
import WordRoutes from './api/words';
import CourseRoutes from './api/courses';

const PORT = process.env.PORT ?? 3000;

Expand All @@ -22,5 +23,6 @@ app.use(createUserProfile);
app.use('/api/sentenceLists', SentenceListRoutes);
app.use('/api/sentences', SentenceRoutes);
app.use('/api/words', WordRoutes);
app.use('/api/courses', CourseRoutes);

app.listen(PORT, () => console.log(`Listening on http://localhost:${PORT}`));
80 changes: 80 additions & 0 deletions express-backend/src/tests/api/courses/getCourses.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
} from '@jest/globals';

import { createRequest, createResponse } from 'node-mocks-http';

import mongoose, { HydratedDocument } from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';

import SentenceList from '../../../sentence-list';
import { UserProfile, UserProfileType } from '../../../user-profile';
import { getCourses } from '../../../api/courses';
import Course from '../../../course';

describe('GET courses', () => {
let mongoDB: MongoMemoryServer;
let alice: HydratedDocument<UserProfileType>;

beforeAll(async () => {
mongoDB = await MongoMemoryServer.create();
const uri = mongoDB.getUri();
await mongoose.connect(uri);

alice = await UserProfile.create({
userId: 'abc',
});
});

afterAll(async () => {
await mongoose.disconnect();
await mongoDB.stop();
});

afterEach(async () => {
await Promise.all([SentenceList.deleteMany(), Course.deleteMany()]);
});

it('should return empty array if there are not courses', async () => {
const req = createRequest();
const res = createResponse();

await getCourses(req, res);
const data = res._getJSONData();

expect(data.length).toBe(0);
});

it('should return all courses along with all lists', async () => {
const testLists = await SentenceList.insertMany([
{
title: 'Korean 1',
owner: alice,
},
{
title: 'Korean 2',
owner: alice,
},
]);

const testCourse = await Course.create({
title: 'Korean',
sentenceLists: testLists,
});

const req = createRequest();
const res = createResponse();

await getCourses(req, res);
const data = res._getJSONData();

expect(data.length).toBe(1);
expect(data[0].title).toBe(testCourse.title);
expect(data[0].sentenceLists.length).toBe(2);
});
});

0 comments on commit 28294c1

Please sign in to comment.