forked from WeAcademy/ByteChain-Academy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlessons.service.ts
More file actions
199 lines (168 loc) · 5.62 KB
/
Copy pathlessons.service.ts
File metadata and controls
199 lines (168 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Course } from '../courses/entities/course.entity';
import { Lesson } from './entities/lesson.entity';
import { In, Repository } from 'typeorm';
import { PaginationService } from '../common/services/pagination.service';
import { PaginatedResult } from '../common/services/pagination.service';
import { CreateLessonDto } from './dto/create-lesson.dto';
import { UpdateLessonDto } from './dto/update-lesson.dto';
@Injectable()
export class LessonsService {
constructor(
@InjectRepository(Lesson)
private lessonRepository: Repository<Lesson>,
@InjectRepository(Course)
private courseRepository: Repository<Course>,
@InjectRepository(Quiz)
private quizRepository: Repository<Quiz>,
private readonly paginationService: PaginationService,
) {}
async create(createLessonDto: CreateLessonDto): Promise<Lesson> {
// Verify course exists
const course = await this.courseRepository.findOne({
where: { id: createLessonDto.courseId },
});
if (!course) {
throw new NotFoundException(
`Course with ID ${createLessonDto.courseId} not found`,
);
}
// Create lesson
const lesson = this.lessonRepository.create({
title: createLessonDto.title,
content: createLessonDto.content,
videoUrl: createLessonDto.videoUrl,
videoStartTimestamp: createLessonDto.videoStartTimestamp,
order: createLessonDto.order ?? 0,
courseId: createLessonDto.courseId,
});
return this.lessonRepository.save(lesson);
}
async findAllByCourse(courseId: string): Promise<Lesson[]> {
// Verify course exists
const course = await this.courseRepository.findOne({
where: { id: courseId },
});
if (!course) {
throw new NotFoundException(`Course with ID ${courseId} not found`);
}
return this.lessonRepository.find({
where: { courseId },
order: { order: 'ASC', createdAt: 'ASC' },
});
}
async findAllPaginated(
page: number,
limit: number,
): Promise<PaginatedResult<Lesson>> {
return this.paginationService.paginate(
this.lessonRepository,
{ page, limit },
{
order: { order: 'ASC', createdAt: 'ASC' },
},
);
}
async findAllByCoursePaginated(
courseId: string,
page: number,
limit: number,
): Promise<PaginatedResult<Lesson & { hasQuiz: boolean; quizId: string | null }>> {
const course = await this.courseRepository.findOne({
where: { id: courseId },
});
if (!course) {
throw new NotFoundException(`Course with ID ${courseId} not found`);
}
const result = await this.paginationService.paginate(
this.lessonRepository,
{ page, limit },
{
where: { courseId },
order: { order: 'ASC', createdAt: 'ASC' },
},
);
const lessonIds = result.data.map((l) => l.id);
const quizzes = lessonIds.length
? await this.quizRepository.find({ where: { lessonId: In(lessonIds) }, select: ['id', 'lessonId'] })
: [];
const quizMap = new Map(quizzes.map((q) => [q.lessonId, q.id]));
return {
...result,
data: result.data.map((l) => ({
...l,
hasQuiz: quizMap.has(l.id),
quizId: quizMap.get(l.id) ?? null,
})),
};
}
async findOne(id: string): Promise<Lesson> {
const lesson = await this.lessonRepository.findOne({
where: { id },
relations: ['course'],
});
if (!lesson) {
throw new NotFoundException(`Lesson with ID ${id} not found`);
}
return lesson;
}
async findOneWithQuizFlag(id: string): Promise<Lesson & { hasQuiz: boolean; quizId: string | null }> {
const lesson = await this.lessonRepository.findOne({
where: { id },
relations: ['course'],
});
if (!lesson) {
throw new NotFoundException(`Lesson with ID ${id} not found`);
}
const quiz = await this.quizRepository.findOne({ where: { lessonId: id }, select: ['id'] });
return { ...lesson, hasQuiz: !!quiz, quizId: quiz?.id ?? null };
}
async update(id: string, updateLessonDto: UpdateLessonDto): Promise<Lesson> {
const lesson = await this.lessonRepository.findOne({
where: { id },
});
if (!lesson) {
throw new NotFoundException(`Lesson with ID ${id} not found`);
}
// Update fields if provided
if (updateLessonDto.title !== undefined) {
lesson.title = updateLessonDto.title;
}
if (updateLessonDto.content !== undefined) {
lesson.content = updateLessonDto.content;
}
if (updateLessonDto.videoUrl !== undefined) {
lesson.videoUrl = updateLessonDto.videoUrl;
}
if (updateLessonDto.videoStartTimestamp !== undefined) {
lesson.videoStartTimestamp = updateLessonDto.videoStartTimestamp;
}
if (updateLessonDto.order !== undefined) {
lesson.order = updateLessonDto.order;
}
return this.lessonRepository.save(lesson);
}
async remove(id: string): Promise<void> {
const lesson = await this.lessonRepository.findOne({
where: { id },
});
if (!lesson) {
throw new NotFoundException(`Lesson with ID ${id} not found`);
}
await this.lessonRepository.remove(lesson);
}
async reorderLessons(courseId: string, orderedIds: string[]): Promise<void> {
const course = await this.courseRepository.findOne({
where: { id: courseId },
});
if (!course) {
throw new NotFoundException(`Course with ID ${courseId} not found`);
}
await Promise.all(
orderedIds.map((id, index) =>
this.lessonRepository.update({ id, courseId }, { order: index }),
),
);
}
}