chore: complete learning module
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
|
||||
import { CommonMessage, LearningMessage } from "../../../common/enums/message.enum";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { CreateLearningCategoryDto } from "../DTO/create-learning-category.dto";
|
||||
import { CreateLearningDto } from "../DTO/create-learning.dto";
|
||||
import { LearningCategoryRepository } from "../repositories/learning-category.repository";
|
||||
import { LearningProgressRepository } from "../repositories/learning-progress.repository";
|
||||
import { LearningRepository } from "../repositories/learning.repository";
|
||||
|
||||
@Injectable()
|
||||
export class LearningService {
|
||||
constructor(
|
||||
private readonly learningRepository: LearningRepository,
|
||||
private readonly learningProgressRepository: LearningProgressRepository,
|
||||
private readonly learningCategoryRepository: LearningCategoryRepository,
|
||||
private readonly usersService: UsersService,
|
||||
) {}
|
||||
|
||||
//******************** */
|
||||
|
||||
async createLearning(createDto: CreateLearningDto) {
|
||||
const { category } = await this.findOneCategory(createDto.categoryId);
|
||||
|
||||
const learning = this.learningRepository.create({
|
||||
...createDto,
|
||||
category,
|
||||
});
|
||||
await this.learningRepository.save(learning);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
learning,
|
||||
};
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async findAllLearnings() {
|
||||
const learnings = await this.learningRepository.find();
|
||||
|
||||
return { learnings };
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async findOneLearning(id: string) {
|
||||
const learning = await this.learningRepository.findOneBy({
|
||||
id,
|
||||
});
|
||||
if (!learning) throw new BadRequestException(LearningMessage.NOT_FOUND);
|
||||
|
||||
return { learning };
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async createLearningCategory(createDto: CreateLearningCategoryDto) {
|
||||
const category = this.learningCategoryRepository.create(createDto);
|
||||
await this.learningCategoryRepository.save(category);
|
||||
|
||||
return { category };
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async findAllCategories() {
|
||||
const categories = await this.learningCategoryRepository.find();
|
||||
|
||||
return { categories };
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async findOneCategory(id: string) {
|
||||
const category = await this.learningCategoryRepository.findOneBy({
|
||||
id,
|
||||
});
|
||||
if (!category) throw new BadRequestException(LearningMessage.CATEGORY_NOT_FOUND);
|
||||
|
||||
return { category };
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async trackProgress(userId: string, learningId: string) {
|
||||
const { user } = await this.usersService.findOneById(userId);
|
||||
const { learning } = await this.findOneLearning(learningId);
|
||||
|
||||
const progress = this.learningProgressRepository.create({
|
||||
user,
|
||||
learning,
|
||||
watchedAt: new Date(),
|
||||
});
|
||||
return this.learningProgressRepository.save(progress);
|
||||
}
|
||||
|
||||
//******************** */
|
||||
|
||||
async getUserProgress(userId: string) {
|
||||
return this.learningProgressRepository.find({
|
||||
where: { user: { id: userId } },
|
||||
relations: ["learning"],
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user