File size: 4,405 Bytes
e67d35c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
  Controller,
  Get,
  Delete,
  Query,
  Req,
  UseGuards,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { SearchService } from '../services/search.service';
import { GlobalSearchDto } from '../dto/global-search.dto';
import { SearchHistoryQueryDto } from '../dto/search-history-query.dto';

@ApiTags('🔍 Search')
@ApiBearerAuth('JWT-auth')
@Controller('api/search')
@UseGuards(JwtAuthGuard, RolesGuard)
export class SearchController {
  constructor(private readonly searchService: SearchService) {}

  @Get()
  @ApiOperation({
    summary: 'Global search',
    description: 'Search across all indexed entities (courses, materials, users, etc.) with optional filters.',
  })
  @ApiResponse({ status: 200, description: 'Search results grouped by entity type' })
  async globalSearch(@Query() dto: GlobalSearchDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.searchService.globalSearch(dto, userId);
  }

  @Get('courses')
  @ApiOperation({
    summary: 'Search courses',
    description: 'Search specifically within indexed courses.',
  })
  @ApiQuery({ name: 'query', required: true, description: 'Search query string' })
  @ApiQuery({ name: 'page', required: false, description: 'Page number', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: 'Results per page', example: 20 })
  @ApiResponse({ status: 200, description: 'Paginated course search results' })
  async searchCourses(
    @Query('query') query: string,
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 20,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    return this.searchService.searchCourses(query, userId, +page || 1, +limit || 20);
  }

  @Get('users')
  @ApiOperation({
    summary: 'Search users',
    description: 'Search specifically within indexed users.',
  })
  @ApiQuery({ name: 'query', required: true, description: 'Search query string' })
  @ApiQuery({ name: 'page', required: false, description: 'Page number', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: 'Results per page', example: 20 })
  @ApiResponse({ status: 200, description: 'Paginated user search results' })
  async searchUsers(
    @Query('query') query: string,
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 20,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    return this.searchService.searchUsers(query, userId, +page || 1, +limit || 20);
  }

  @Get('materials')
  @ApiOperation({
    summary: 'Search materials',
    description: 'Search specifically within indexed materials.',
  })
  @ApiQuery({ name: 'query', required: true, description: 'Search query string' })
  @ApiQuery({ name: 'page', required: false, description: 'Page number', example: 1 })
  @ApiQuery({ name: 'limit', required: false, description: 'Results per page', example: 20 })
  @ApiResponse({ status: 200, description: 'Paginated material search results' })
  async searchMaterials(
    @Query('query') query: string,
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 20,
    @Req() req: any,
  ) {
    const userId = req.user.userId || req.user.id;
    return this.searchService.searchMaterials(query, userId, +page || 1, +limit || 20);
  }

  @Get('history')
  @ApiOperation({
    summary: 'Get search history',
    description: 'Retrieve the authenticated user\'s search history with pagination.',
  })
  @ApiResponse({ status: 200, description: 'Paginated search history' })
  async getHistory(@Query() dto: SearchHistoryQueryDto, @Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.searchService.getHistory(userId, dto.page, dto.limit);
  }

  @Delete('history')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Clear search history',
    description: 'Delete all search history for the authenticated user.',
  })
  @ApiResponse({ status: 200, description: 'Search history cleared successfully' })
  async clearHistory(@Req() req: any) {
    const userId = req.user.userId || req.user.id;
    return this.searchService.clearHistory(userId);
  }
}