File size: 2,471 Bytes
8268e91
 
 
f45e448
8268e91
 
 
 
426f2a4
8268e91
73746a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f45e448
 
 
 
 
 
 
73746a8
 
 
 
 
 
f45e448
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73746a8
 
 
 
 
 
426f2a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73746a8
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
import {
  Controller,
  Get,
  Post,
  Param,
  UseGuards,
  Request,
  ParseIntPipe,
  Body,
} from '@nestjs/common';
import { CoursesService } from './courses.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('api/courses')
export class CoursesController {
  constructor(private readonly coursesService: CoursesService) {}

  @Get()
  async findAll() {
    const data = await this.coursesService.findAll();
    return { success: true, data };
  }

  @UseGuards(JwtAuthGuard)
  @Get('my')
  async findMyCourses(@Request() req) {
    const data = await this.coursesService.getUserCourses(req.user.userId);
    return { success: true, data };
  }

  @UseGuards(JwtAuthGuard)
  @Get('my-stars')
  async findMyStars(@Request() req) {
    const data = await this.coursesService.getUserStars(req.user.userId);
    return { success: true, data };
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    const data = await this.coursesService.findOne(id);
    return { success: true, data };
  }

  @Post(':id/view')
  async incrementViewCount(@Param('id', ParseIntPipe) id: number) {
    await this.coursesService.incrementViewCount(id);
    return { success: true };
  }

  @Post(':id/like')
  async toggleLike(@Param('id', ParseIntPipe) id: number) {
    await this.coursesService.toggleLike(id);
    return { success: true };
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/star')
  async toggleStar(@Param('id', ParseIntPipe) id: number, @Request() req) {
    await this.coursesService.toggleStar(id, req.user.userId);
    return { success: true };
  }

  @UseGuards(JwtAuthGuard)
  @Get(':id/access')
  async getAccess(@Param('id', ParseIntPipe) id: number, @Request() req) {
    const data = await this.coursesService.getCourseAccess(req.user.userId, id);
    return { success: true, data };
  }

  @Get(':id/comments')
  async getComments(@Param('id', ParseIntPipe) id: number) {
    const data = await this.coursesService.getComments(id);
    return { success: true, data };
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/comments')
  async addComment(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: { content: string; parentId?: number; replyToCommentId?: number },
    @Request() req,
  ) {
    const data = await this.coursesService.addComment(
      id,
      req.user.userId,
      body.content,
      body.parentId,
      body.replyToCommentId,
    );
    return { success: true, data };
  }
}