Skip to content

Commit

Permalink
feat: various fetchLessonByX + video
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeyAlmighty committed Sep 12, 2024
1 parent 4e41a95 commit 58840be
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/lecturers/lecturer.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Learner } from "src/learners/learner.entity";
import { Lesson } from "src/lessons/lesson.entity";
import {
Column,
CreateDateColumn,
Expand Down Expand Up @@ -43,4 +44,7 @@ export class Lecturer {

@OneToMany(() => Learner, (learner) => learner.lecturer)
learners: Learner[];

@OneToMany(() => Lesson, (lesson) => lesson.lecturer)
lessons: Lesson[];
}
16 changes: 14 additions & 2 deletions src/lecturers/lecturers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@ export class LecturersController {
"id",
new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
)
id: string,
lecturerId: string,
) {
return this.lecturerService.getLearnersByLecturerId(lecturerId);
}

@Get(":id/lessons")
@UseGuards(JwtAuthGuard)
getLessonsByLecturerId(
@Param(
"id",
new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
)
lecturerId: string,
) {
return this.lecturerService.getLearnersByLecturerId(id);
return this.lecturerService.getLessonsByLecturerId(lecturerId);
}

@Put(":id")
Expand Down
11 changes: 11 additions & 0 deletions src/lecturers/lecturers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ export class LecturersService {
relations: ["learners"],
});
}

async getLessonsByLecturerId(lecturerId: string) {
const lecturer = await this.lecturerRepository.findOne({
where: { id: lecturerId },
relations: ["lessons"],
});
if (!lecturer) throw new Error("Lecturer not found");

// Return all lessons for this lecturer
return lecturer.lessons;
}
}
4 changes: 4 additions & 0 deletions src/lessons/dto/create-lesson.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class CreateLessonDto {
@IsNotEmpty()
title: string;

@IsString()
@IsNotEmpty()
lecturerId: string;

@IsString()
readonly description: string;

Expand Down
5 changes: 5 additions & 0 deletions src/lessons/lesson.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
Entity,
JoinTable,
ManyToMany,
ManyToOne,
OneToMany,
PrimaryColumn,
} from "typeorm";

import { Learner } from "src/learners/learner.entity";
import { Question } from "./question.entity";
import { Lecturer } from "src/lecturers/lecturer.entity";

@Entity({ name: "lessons" })
export class Lesson {
Expand Down Expand Up @@ -43,4 +45,7 @@ export class Lesson {
@ManyToMany(() => Learner, (learner) => learner.lesson)
@JoinTable()
learners: Learner[];

@ManyToOne(() => Lecturer, (lecturer) => lecturer.lessons)
lecturer: Lecturer;
}
67 changes: 60 additions & 7 deletions src/lessons/lessons.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ import { validate } from "class-validator";
export class LessonsController {
constructor(private lessonService: LessonsService) {}

@Post()
@Post(":id")
@UseGuards(JwtAuthGuard)
@UseInterceptors(FileInterceptor("video"))
async create(@UploadedFile() video: Express.Multer.File, @Body() body) {
async create(
@UploadedFile() video: Express.Multer.File,
@Body() body,
@Param(
"id",
new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
)
lecturerId: string,
) {
let parsedQuestions;
try {
parsedQuestions = JSON.parse(body.questions);
Expand All @@ -38,6 +46,7 @@ export class LessonsController {

const createLessonDto = plainToClass(CreateLessonDto, {
...body,
lecturerId,
questions: parsedQuestions,
});

Expand All @@ -51,22 +60,66 @@ export class LessonsController {
return this.lessonService.createLesson(createLessonDto, video);
}

@Get()
// @Get("/learners/:id")
// @UseGuards(JwtAuthGuard)
// getLearnersByLessonId(
// @Param(
// "id",
// new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
// )
// lessonId: string,
// ) {
// return this.lessonService.getLearnersByLessonId(lessonId);
// }

// @Get("lecturer/:id")
// @UseGuards(JwtAuthGuard)
// getLecturerByLessonId(
// @Param(
// "id",
// new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
// )
// lessonId: string,
// ) {
// return this.lessonService.getLecturerByLessonId(lessonId);
// }

@Get("learner/:id")
@UseGuards(JwtAuthGuard)
getLessonsByLearnerId(
@Param(
"id",
new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
)
learnerId: string,
) {
return this.lessonService.getLessonsByLearnerId(learnerId);
}

@Get("lecturer/:id")
@UseGuards(JwtAuthGuard)
getLessons() {
return this.lessonService.getAll();
getLessonsByLecturerId(
@Param(
"id",
new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
)
lecturerId: string,
) {
return this.lessonService.getLessonsByLecturerId(lecturerId);
}

@Get(":id")
@UseGuards(JwtAuthGuard)
findLessonById(
async findLessonById(
@Param(
"id",
new ParseUUIDPipe({ errorHttpStatusCode: HttpStatus.NOT_ACCEPTABLE }),
)
id: string,
) {
return this.lessonService.findLessonById(id);
const lesson = await this.lessonService.findLessonById(id);
console.log("returning lesson: ", lesson);
return lesson;
}

@Put(":id")
Expand Down
4 changes: 3 additions & 1 deletion src/lessons/lessons.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { LessonsController } from "./lessons.controller";
import { Lesson } from "./lesson.entity";
import { Question } from "./question.entity";
import { Answer } from "./answer.entity";
import { Lecturer } from "src/lecturers/lecturer.entity";

@Module({
imports: [
TypeOrmModule.forFeature([Lesson, Answer, Question]),
TypeOrmModule.forFeature([Lecturer, Lesson, Answer, Question]),
Repository<Lecturer>,
Repository<Lesson>,
Repository<Answer>,
Repository<Question>,
Expand Down
65 changes: 60 additions & 5 deletions src/lessons/lessons.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { Lesson } from "./lesson.entity";
import { Question } from "./question.entity";
import { Answer } from "./answer.entity";
import { EventEmitter2 } from "@nestjs/event-emitter";
import { Lecturer } from "src/lecturers/lecturer.entity";

@Injectable()
export class LessonsService {
constructor(
@InjectRepository(Lesson)
private lessonRepository: Repository<Lesson>,
@InjectRepository(Lecturer)
private lecturerRepository: Repository<Lecturer>,
@InjectRepository(Lesson) private lessonRepository: Repository<Lesson>,
@InjectRepository(Question)
private questionRepository: Repository<Question>,
@InjectRepository(Answer)
Expand All @@ -25,17 +27,22 @@ export class LessonsService {
lessonDetails: CreateLessonDto,
video: Express.Multer.File,
) {
const { id, questions, title, description } = lessonDetails;
const { id, questions, title, description, lecturerId } = lessonDetails;

this.eventEmitter.emit("user.video.upload", {
lessonId: id,
video,
});

const lecturer = await this.lecturerRepository.findOneBy({
id: lecturerId,
});

// Create a new Lesson
const newLesson = this.lessonRepository.create({
id,
title,
lecturer,
createdAt: new Date(),
releaseDate: new Date(),
description,
Expand Down Expand Up @@ -75,15 +82,63 @@ export class LessonsService {
await this.questionRepository.save(questionEntities);
}

async getLearnersByLessonId(lessonId: string) {
const lesson = await this.lessonRepository.findOne({
where: { id: lessonId },
relations: ["learners"],
});
if (!lesson) throw new Error("Lesson not found");

// Return all learners for this lesson
return lesson.learners;
}

async getLecturerByLessonId(lessonId: string) {
const lesson = await this.lessonRepository.findOne({
where: { id: lessonId },
relations: ["lecturer"], // Include lecturer in the result
});
if (!lesson) throw new Error("Lesson not found");

return lesson.lecturer; // Return the lecturer for this lesson
}

async getLessonsByLecturerId(lecturerId: string) {
const lessons = await this.lessonRepository.find({
where: { lecturer: { id: lecturerId } },
relations: ["lecturer"],
});

if (!lessons.length) {
throw new Error("No lessons found for this lecturer");
}

return lessons;
}

async getLessonsByLearnerId(learnerId: string) {
const lessons = await this.lessonRepository.find({
where: { learners: { id: learnerId } },
relations: ["learners"],
});

if (!lessons.length) {
throw new Error("No lessons found for this learner");
}

return lessons;
}

getAll() {
return this.lessonRepository.find({
relations: ["questions"],
relationLoadStrategy: "join",
});
}

findLessonById(id: string) {
return this.lessonRepository.findOne({
async findLessonById(id: string) {
console.log("id: ", id);
return await this.lessonRepository.findOne({
where: { id },
relations: ["questions"],
});
Expand Down

0 comments on commit 58840be

Please sign in to comment.