146 lines
5.3 KiB
TypeScript
Executable File
146 lines
5.3 KiB
TypeScript
Executable File
import { BadRequestException, Injectable } from "@nestjs/common";
|
|
import { IsNull, Not } from "typeorm";
|
|
|
|
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 { UpdateLearningCategoryDto } from "../DTO/update-learning-category.dto";
|
|
import { UpdateLearningDto } from "../DTO/update-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 createLearningVideo(createDto: CreateLearningDto) {
|
|
const { category } = await this.findCategoryById(createDto.categoryId);
|
|
|
|
const learning = this.learningRepository.create({ ...createDto, category });
|
|
await this.learningRepository.save(learning);
|
|
|
|
return {
|
|
message: CommonMessage.CREATED,
|
|
learning,
|
|
};
|
|
}
|
|
//********************************************* */
|
|
|
|
async updateLearningVideo(learningId: string, updateDto: UpdateLearningDto) {
|
|
const { learning } = await this.findLearningVideoById(learningId);
|
|
|
|
if (updateDto.categoryId) {
|
|
const { category } = await this.findCategoryById(updateDto.categoryId);
|
|
learning.category = category;
|
|
}
|
|
|
|
await this.learningRepository.save({ ...learning, ...updateDto });
|
|
return {
|
|
message: CommonMessage.UPDATE_SUCCESS,
|
|
learning,
|
|
};
|
|
}
|
|
//********************************************* */
|
|
|
|
async deleteLearningVideoById(learningId: string) {
|
|
await this.findLearningVideoById(learningId);
|
|
await this.learningRepository.softDelete(learningId);
|
|
|
|
return {
|
|
message: CommonMessage.DELETED,
|
|
};
|
|
}
|
|
//********************************************* */
|
|
|
|
async findLearningsVideos() {
|
|
const learnings = await this.learningRepository.findBy({ deletedAt: IsNull() });
|
|
|
|
return { learnings };
|
|
}
|
|
//********************************************* */
|
|
|
|
async findLearningVideoById(id: string) {
|
|
const learning = await this.learningRepository.findOneBy({ id, deletedAt: IsNull() });
|
|
if (!learning) throw new BadRequestException(LearningMessage.NOT_FOUND);
|
|
|
|
return { learning };
|
|
}
|
|
//********************************************* */
|
|
|
|
async createLearningCategory(createDto: CreateLearningCategoryDto) {
|
|
const existCategory = await this.learningCategoryRepository.findOneBy({ name: createDto.name });
|
|
if (existCategory) throw new BadRequestException(LearningMessage.CATEGORY_NAME_EXIST);
|
|
|
|
const category = this.learningCategoryRepository.create(createDto);
|
|
await this.learningCategoryRepository.save(category);
|
|
|
|
return { category };
|
|
}
|
|
//********************************************* */
|
|
|
|
async updateLearningCategory(categoryId: string, updateDto: UpdateLearningCategoryDto) {
|
|
const category = await this.learningCategoryRepository.findOneBy({ id: categoryId });
|
|
if (!category) throw new BadRequestException(LearningMessage.CATEGORY_NOT_FOUND);
|
|
|
|
if (updateDto.name) {
|
|
const existCategory = await this.learningCategoryRepository.findOneBy({ name: updateDto.name, id: Not(categoryId) });
|
|
if (existCategory) throw new BadRequestException(LearningMessage.CATEGORY_NAME_EXIST);
|
|
category.name = updateDto.name;
|
|
}
|
|
|
|
await this.learningCategoryRepository.save({ ...category });
|
|
return {
|
|
message: CommonMessage.UPDATE_SUCCESS,
|
|
category,
|
|
};
|
|
}
|
|
//********************************************* */
|
|
|
|
async deleteCategoryById(categoryId: string) {
|
|
await this.findCategoryById(categoryId);
|
|
|
|
await this.learningCategoryRepository.softDelete(categoryId);
|
|
return {
|
|
message: CommonMessage.DELETED,
|
|
};
|
|
}
|
|
//********************************************* */
|
|
|
|
async findCategories() {
|
|
const categories = await this.learningCategoryRepository.findBy({ deletedAt: IsNull() });
|
|
|
|
return { categories };
|
|
}
|
|
//********************************************* */
|
|
|
|
async findCategoryById(id: string) {
|
|
const category = await this.learningCategoryRepository.findOneBy({ id, deletedAt: IsNull() });
|
|
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.findLearningVideoById(learningId);
|
|
|
|
const progress = this.learningProgressRepository.create({ user, learning });
|
|
return this.learningProgressRepository.save(progress);
|
|
}
|
|
//********************************************* */
|
|
|
|
async getUserProgress(userId: string) {
|
|
return this.learningProgressRepository.find({ where: { user: { id: userId } }, relations: { learning: true } });
|
|
}
|
|
}
|