Skip to content

Commit

Permalink
EW-994 provided a new API to get course metadata by ID (#5181)
Browse files Browse the repository at this point in the history
* EW-994 provided a get course by id API
  • Loading branch information
Fshmit authored Aug 16, 2024
1 parent 4d943c9 commit 4c04e4d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@shared/testing';
import { readFile } from 'node:fs/promises';
import { CourseMetadataListResponse } from '../dto';
import { CourseCommonCartridgeMetadataResponse } from '../dto/course-cc-metadata.response';

const createStudent = () => {
const { studentUser, studentAccount } = UserAndAccountTestFactory.buildStudent({}, [Permission.COURSE_VIEW]);
Expand Down Expand Up @@ -243,4 +244,30 @@ describe('Course Controller (API)', () => {
expect(data[teacher.user.id].length).toBeGreaterThan(0);
});
});

describe('[GET] /courses/:courseId', () => {
const setup = async () => {
const teacher = createTeacher();
const course = courseFactory.buildWithId({
teachers: [teacher.user],
students: [],
});

await em.persistAndFlush([teacher.account, teacher.user, course]);
em.clear();

return { course, teacher };
};

it('should return common cartridge metadata of a course', async () => {
const { course, teacher } = await setup();

const loggedInClient = await testApiClient.login(teacher.account);
const response = await loggedInClient.get(`${course.id}`);
const data = response.body as CourseCommonCartridgeMetadataResponse;

expect(response.statusCode).toBe(200);
expect(data.id).toBe(course.id);
});
});
});
11 changes: 11 additions & 0 deletions apps/server/src/modules/learnroom/controller/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { CourseExportUc, CourseImportUc, CourseSyncUc, CourseUc } from '../uc';
import { CommonCartridgeFileValidatorPipe } from '../utils';
import { CourseImportBodyParams, CourseMetadataListResponse, CourseQueryParams, CourseUrlParams } from './dto';
import { CourseExportBodyParams } from './dto/course-export.body.params';
import { CourseCommonCartridgeMetadataResponse } from './dto/course-cc-metadata.response';

@ApiTags('Courses')
@Authenticate('jwt')
Expand Down Expand Up @@ -129,4 +130,14 @@ export class CourseController {
[currentUser.userId]: permissions,
};
}

@Get(':courseId')
@ApiOperation({ summary: 'Get common cartridge metadata of a course by Id.' })
@ApiBadRequestResponse({ description: 'Request data has invalid format.' })
@ApiInternalServerErrorResponse({ description: 'Internal server error.' })
public async getCourseById(@Param() param: CourseUrlParams): Promise<CourseCommonCartridgeMetadataResponse> {
const course = await this.courseUc.findCourseById(param.courseId);

return CourseMapper.mapToCommonCartridgeMetadataResponse(course);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ApiProperty } from '@nestjs/swagger';
import { EntityId } from '@shared/domain/types';

export class CourseCommonCartridgeMetadataResponse {
constructor(id: EntityId, title: string, copyrightOwners: string[], creationDate?: Date) {
this.id = id;
this.title = title;
this.creationDate = creationDate;
this.copyRightOwners = copyrightOwners;
}

@ApiProperty({
description: 'The id of the course',
pattern: '[a-f0-9]{24}',
})
id: string;

@ApiProperty({
description: 'Title of the course',
})
title: string;

@ApiProperty({
description: 'Creation date of the course',
})
creationDate?: Date;

@ApiProperty({
description: 'Copy right owners of the course',
})
copyRightOwners: string[];
}
14 changes: 14 additions & 0 deletions apps/server/src/modules/learnroom/mapper/course.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Course } from '@shared/domain/entity';
import { CourseMetadataResponse } from '../controller/dto';
import { CourseCommonCartridgeMetadataResponse } from '../controller/dto/course-cc-metadata.response';

export class CourseMapper {
static mapToMetadataResponse(course: Course): CourseMetadataResponse {
Expand All @@ -15,4 +16,17 @@ export class CourseMapper {
);
return dto;
}

static mapToCommonCartridgeMetadataResponse(course: Course): CourseCommonCartridgeMetadataResponse {
const courseMetadata = course.getMetadata();
const teachers = course.teachers.toArray().map((teacher) => `${teacher.firstName} ${teacher.lastName}`);
const courseCCMetadataResopne: CourseCommonCartridgeMetadataResponse = new CourseCommonCartridgeMetadataResponse(
courseMetadata.id,
courseMetadata.title,
teachers,
courseMetadata.startDate
);

return courseCCMetadataResopne;
}
}
16 changes: 16 additions & 0 deletions apps/server/src/modules/learnroom/uc/course.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ describe('CourseUc', () => {
expect(roleService.findByName).toHaveBeenCalledWith(RoleName.TEACHER);
});
});

describe('findCourseById', () => {
const setup = () => {
const course = courseFactory.buildWithId();
courseService.findById.mockResolvedValue(course);
return { course };
};

it('should return course by id', async () => {
const { course } = setup();
const result = await uc.findCourseById(course.id);

expect(result).toEqual(course);
expect(courseService.findById).toHaveBeenCalledWith(course.id);
});
});
});
4 changes: 4 additions & 0 deletions apps/server/src/modules/learnroom/uc/course.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ export class CourseUc {

return role.permissions ?? [];
}

public async findCourseById(courseId: EntityId): Promise<Course> {
return this.courseService.findById(courseId);
}
}

0 comments on commit 4c04e4d

Please sign in to comment.