|
1 | 1 | import { HttpService } from '@nestjs/axios' |
2 | | -import { Inject, Injectable, NotFoundException } from '@nestjs/common' |
| 2 | +import { Injectable, NotFoundException } from '@nestjs/common' |
3 | 3 | import { ConfigService } from '@nestjs/config' |
4 | 4 | import { User } from '@prisma/generated' |
5 | 5 | import { randomBytes } from 'crypto' |
6 | 6 | import { firstValueFrom } from 'rxjs' |
7 | 7 |
|
8 | | -import type { AllConfigs } from '@/config/definitions' |
9 | 8 | import { RedisService } from '@/infra/redis/redis.service' |
10 | | -import { slugify } from '@/shared/utils/slugify' |
| 9 | +import { slugify } from '@/shared/utils' |
11 | 10 |
|
12 | | -import { CourseRepository } from '../../domain/repositories/course.repository.interface' |
13 | | -import { DownloadLogRepository } from '../../domain/repositories/download-log.repository.interface' |
| 11 | +import { CourseRepository } from './course.repository' |
14 | 12 |
|
15 | 13 | @Injectable() |
16 | | -export class CourseApplicationService { |
| 14 | +export class CourseService { |
17 | 15 | public constructor( |
18 | | - @Inject('CourseRepository') |
19 | | - private readonly courseRepo: CourseRepository, |
20 | | - @Inject('DownloadLogRepository') |
21 | | - private readonly downloadLogRepo: DownloadLogRepository, |
| 16 | + private readonly courseRepository: CourseRepository, |
22 | 17 | private readonly redisService: RedisService, |
23 | | - private readonly configService: ConfigService<AllConfigs>, |
| 18 | + private readonly configService: ConfigService, |
24 | 19 | private readonly httpService: HttpService |
25 | 20 | ) {} |
26 | 21 |
|
27 | 22 | public async getAll() { |
28 | | - return this.courseRepo.findAll({ |
29 | | - isPublished: true, |
| 23 | + return this.courseRepository.findAll({ |
| 24 | + where: { |
| 25 | + isPublished: true |
| 26 | + }, |
30 | 27 | orderBy: { |
31 | | - field: 'createdAt', |
32 | | - direction: 'desc' |
| 28 | + createdAt: 'desc' |
33 | 29 | } |
34 | 30 | }) |
35 | 31 | } |
36 | 32 |
|
37 | 33 | public async getPopular() { |
38 | | - return this.courseRepo.findPopular(4) |
| 34 | + return this.courseRepository.findPopular(4) |
39 | 35 | } |
40 | 36 |
|
41 | 37 | public async getBySlug(slug: string) { |
42 | 38 | const cached = await this.redisService.get(`courses:${slug}`) |
43 | 39 |
|
44 | 40 | if (cached) return JSON.parse(cached) |
45 | 41 |
|
46 | | - const course = await this.courseRepo.findBySlug(slug) |
| 42 | + const course = await this.courseRepository.findBySlug(slug) |
47 | 43 |
|
48 | 44 | if (!course || !course.isPublished) |
49 | 45 | throw new NotFoundException('Course not found') |
@@ -71,22 +67,22 @@ export class CourseApplicationService { |
71 | 67 | } |
72 | 68 |
|
73 | 69 | public async getCourseLessons(id: string) { |
74 | | - const course = await this.courseRepo.findById(id) |
| 70 | + const course = await this.courseRepository.findById(id) |
75 | 71 |
|
76 | 72 | if (!course || !course.isPublished) |
77 | 73 | throw new NotFoundException('Course not found') |
78 | 74 |
|
79 | | - return this.courseRepo.findLessons(course.id) |
| 75 | + return this.courseRepository.findLessons(course.id) |
80 | 76 | } |
81 | 77 |
|
82 | 78 | public async incrementViews(id: string) { |
83 | | - await this.courseRepo.incrementViews(id) |
| 79 | + await this.courseRepository.incrementViews(id) |
84 | 80 |
|
85 | 81 | return true |
86 | 82 | } |
87 | 83 |
|
88 | 84 | public async generateDownloadLink(id: string, user: User) { |
89 | | - const course = await this.courseRepo.findById(id) |
| 85 | + const course = await this.courseRepository.findById(id) |
90 | 86 |
|
91 | 87 | if (!course || !course.attachment) |
92 | 88 | throw new NotFoundException('Course not found') |
@@ -116,11 +112,11 @@ export class CourseApplicationService { |
116 | 112 |
|
117 | 113 | const { courseId, userId } = JSON.parse(data) |
118 | 114 |
|
119 | | - const course = await this.courseRepo.findById(courseId) |
| 115 | + const course = await this.courseRepository.findById(courseId) |
120 | 116 |
|
121 | 117 | if (!course) throw new NotFoundException('Course not found') |
122 | 118 |
|
123 | | - await this.downloadLogRepo.logDownload({ |
| 119 | + await this.courseRepository.logDownload({ |
124 | 120 | token, |
125 | 121 | ip, |
126 | 122 | userAgent, |
@@ -155,7 +151,7 @@ export class CourseApplicationService { |
155 | 151 | public async create(request: { title: string }) { |
156 | 152 | const slug = slugify(request.title) |
157 | 153 |
|
158 | | - const created = await this.courseRepo.create({ |
| 154 | + const created = await this.courseRepository.create({ |
159 | 155 | title: request.title, |
160 | 156 | slug |
161 | 157 | }) |
|
0 commit comments