-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* adding route, dto, mapper and uc implementation
- Loading branch information
Showing
8 changed files
with
263 additions
and
7 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
apps/server/src/modules/lesson/controller/api-test/lesson-tasks.api.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { EntityManager } from '@mikro-orm/core'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { courseFactory, lessonFactory, taskFactory, TestApiClient, UserAndAccountTestFactory } from '@shared/testing'; | ||
import { ServerTestModule } from '@src/modules/server'; | ||
|
||
describe('Lesson Controller (API) - GET list of lesson tasks /lessons/:lessonId/tasks', () => { | ||
let module: TestingModule; | ||
let app: INestApplication; | ||
let em: EntityManager; | ||
let testApiClient: TestApiClient; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [ServerTestModule], | ||
}).compile(); | ||
app = module.createNestApplication(); | ||
em = module.get(EntityManager); | ||
testApiClient = new TestApiClient(app, '/lessons'); | ||
|
||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
await module.close(); | ||
}); | ||
|
||
describe('when lesson exists', () => { | ||
const setup = async () => { | ||
const { teacherAccount, teacherUser } = UserAndAccountTestFactory.buildTeacher(); | ||
const course = courseFactory.buildWithId({ teachers: [teacherUser] }); | ||
const lesson = lessonFactory.build({ course }); | ||
const tasks = taskFactory.buildList(3, { creator: teacherUser, lesson }); | ||
|
||
await em.persistAndFlush([teacherAccount, teacherUser, course, lesson, ...tasks]); | ||
|
||
const loggedInClient = await testApiClient.login(teacherAccount); | ||
|
||
return { loggedInClient, course, lesson, tasks }; | ||
}; | ||
|
||
it('should return a list of tasks', async () => { | ||
const { loggedInClient, lesson } = await setup(); | ||
|
||
const response = await loggedInClient.get(`/${lesson.id}/tasks`); | ||
|
||
expect(response.status).toBe(200); | ||
expect((response.body as []).length).toEqual(3); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './lesson.url.params'; | ||
export { LessonsUrlParams } from './lessons.url.params'; | ||
export * from './lesson-content.response'; | ||
export * from './lesson-linked-task.response'; | ||
export * from './lesson.response'; | ||
export * from './lesson.url.params'; | ||
export { LessonsUrlParams } from './lessons.url.params'; | ||
export * from './material.response'; |
55 changes: 55 additions & 0 deletions
55
apps/server/src/modules/lesson/controller/dto/lesson-linked-task.response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { InputFormat } from '@shared/domain/types'; | ||
|
||
export class LessonLinkedTaskResponse { | ||
@ApiProperty() | ||
public readonly name: string; | ||
|
||
@ApiProperty() | ||
public readonly description: string; | ||
|
||
@ApiProperty({ enum: InputFormat }) | ||
public readonly descriptionInputFormat: InputFormat; | ||
|
||
@ApiProperty({ type: Date, nullable: true }) | ||
availableDate?: Date; | ||
|
||
@ApiProperty({ type: Date, nullable: true }) | ||
dueDate?: Date; | ||
|
||
@ApiProperty() | ||
public readonly private: boolean = true; | ||
|
||
@ApiProperty({ nullable: true }) | ||
public readonly publicSubmissions?: boolean; | ||
|
||
@ApiProperty({ nullable: true }) | ||
public readonly teamSubmissions?: boolean; | ||
|
||
@ApiProperty({ nullable: true }) | ||
public readonly creator?: string; | ||
|
||
@ApiProperty({ nullable: true }) | ||
public readonly courseId?: string; | ||
|
||
@ApiProperty({ type: [String] }) | ||
public readonly submissionIds: string[] = []; | ||
|
||
@ApiProperty({ type: [String] }) | ||
public readonly finishedIds: string[] = []; | ||
|
||
constructor(props: Readonly<LessonLinkedTaskResponse>) { | ||
this.name = props.name; | ||
this.description = props.description; | ||
this.descriptionInputFormat = props.descriptionInputFormat; | ||
this.availableDate = props.availableDate; | ||
this.dueDate = props.dueDate; | ||
this.private = props.private; | ||
this.creator = props.creator; | ||
this.courseId = props.courseId; | ||
this.publicSubmissions = props.publicSubmissions; | ||
this.teamSubmissions = props.teamSubmissions; | ||
this.submissionIds = props.submissionIds; | ||
this.finishedIds = props.finishedIds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
apps/server/src/modules/lesson/controller/mapper/lesson.mapper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { MikroORM } from '@mikro-orm/core'; | ||
import { setupEntities, submissionFactory, taskFactory, userFactory } from '@shared/testing'; | ||
import { LessonLinkedTaskResponse } from '../dto/lesson-linked-task.response'; | ||
import { LessonMapper } from './lesson.mapper'; | ||
|
||
describe('LessonMapper', () => { | ||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await setupEntities(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(); | ||
}); | ||
|
||
describe('mapTaskToResponse', () => { | ||
describe('when mapping task to response', () => { | ||
const setup = () => { | ||
const task = taskFactory.buildWithId({ | ||
publicSubmissions: true, | ||
teamSubmissions: true, | ||
submissions: submissionFactory.buildListWithId(2), | ||
finished: userFactory.buildListWithId(2), | ||
}); | ||
|
||
return { task }; | ||
}; | ||
|
||
it('should map task to response', () => { | ||
const { task } = setup(); | ||
|
||
const result = LessonMapper.mapTaskToResponse(task); | ||
|
||
expect(result).toEqual<LessonLinkedTaskResponse>({ | ||
name: task.name, | ||
description: task.description, | ||
descriptionInputFormat: task.descriptionInputFormat, | ||
availableDate: task.availableDate, | ||
dueDate: task.dueDate, | ||
private: task.private, | ||
creator: task.creator?.id, | ||
publicSubmissions: task.publicSubmissions, | ||
teamSubmissions: task.teamSubmissions, | ||
courseId: task.course?.id, | ||
submissionIds: task.submissions.toArray().map((submission) => submission.id), | ||
finishedIds: task.finished.toArray().map((submission) => submission.id), | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
24 changes: 22 additions & 2 deletions
24
apps/server/src/modules/lesson/controller/mapper/lesson.mapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import { LessonEntity } from '@shared/domain/entity'; | ||
import { LessonEntity, Task } from '@shared/domain/entity'; | ||
import { LessonMetadataResponse } from '../dto'; | ||
import { LessonLinkedTaskResponse } from '../dto/lesson-linked-task.response'; | ||
|
||
export class LessonMapper { | ||
static mapToMetadataResponse(lesson: LessonEntity): LessonMetadataResponse { | ||
public static mapToMetadataResponse(lesson: LessonEntity): LessonMetadataResponse { | ||
const dto = new LessonMetadataResponse({ _id: lesson.id, name: lesson.name }); | ||
return dto; | ||
} | ||
|
||
public static mapTaskToResponse(task: Task): LessonLinkedTaskResponse { | ||
const response = new LessonLinkedTaskResponse({ | ||
name: task.name, | ||
description: task.description, | ||
descriptionInputFormat: task.descriptionInputFormat, | ||
availableDate: task.availableDate, | ||
dueDate: task.dueDate, | ||
private: task.private, | ||
creator: task.creator?.id, | ||
publicSubmissions: task.publicSubmissions, | ||
teamSubmissions: task.teamSubmissions, | ||
courseId: task.course?.id, | ||
submissionIds: task.submissions.toArray().map((submission) => submission.id), | ||
finishedIds: task.finished.toArray().map((submission) => submission.id), | ||
}); | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters