Spaces:
Sleeping
Sleeping
| 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'; | |
| ('🔍 Search') | |
| ('JWT-auth') | |
| ('api/search') | |
| (JwtAuthGuard, RolesGuard) | |
| export class SearchController { | |
| constructor(private readonly searchService: SearchService) {} | |
| () | |
| ({ | |
| summary: 'Global search', | |
| description: 'Search across all indexed entities (courses, materials, users, etc.) with optional filters.', | |
| }) | |
| ({ status: 200, description: 'Search results grouped by entity type' }) | |
| async globalSearch(() dto: GlobalSearchDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.searchService.globalSearch(dto, userId); | |
| } | |
| ('courses') | |
| ({ | |
| summary: 'Search courses', | |
| description: 'Search specifically within indexed courses.', | |
| }) | |
| ({ name: 'query', required: true, description: 'Search query string' }) | |
| ({ name: 'page', required: false, description: 'Page number', example: 1 }) | |
| ({ name: 'limit', required: false, description: 'Results per page', example: 20 }) | |
| ({ status: 200, description: 'Paginated course search results' }) | |
| async searchCourses( | |
| ('query') query: string, | |
| ('page') page: number = 1, | |
| ('limit') limit: number = 20, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.searchService.searchCourses(query, userId, +page || 1, +limit || 20); | |
| } | |
| ('users') | |
| ({ | |
| summary: 'Search users', | |
| description: 'Search specifically within indexed users.', | |
| }) | |
| ({ name: 'query', required: true, description: 'Search query string' }) | |
| ({ name: 'page', required: false, description: 'Page number', example: 1 }) | |
| ({ name: 'limit', required: false, description: 'Results per page', example: 20 }) | |
| ({ status: 200, description: 'Paginated user search results' }) | |
| async searchUsers( | |
| ('query') query: string, | |
| ('page') page: number = 1, | |
| ('limit') limit: number = 20, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.searchService.searchUsers(query, userId, +page || 1, +limit || 20); | |
| } | |
| ('materials') | |
| ({ | |
| summary: 'Search materials', | |
| description: 'Search specifically within indexed materials.', | |
| }) | |
| ({ name: 'query', required: true, description: 'Search query string' }) | |
| ({ name: 'page', required: false, description: 'Page number', example: 1 }) | |
| ({ name: 'limit', required: false, description: 'Results per page', example: 20 }) | |
| ({ status: 200, description: 'Paginated material search results' }) | |
| async searchMaterials( | |
| ('query') query: string, | |
| ('page') page: number = 1, | |
| ('limit') limit: number = 20, | |
| () req: any, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.searchService.searchMaterials(query, userId, +page || 1, +limit || 20); | |
| } | |
| ('history') | |
| ({ | |
| summary: 'Get search history', | |
| description: 'Retrieve the authenticated user\'s search history with pagination.', | |
| }) | |
| ({ status: 200, description: 'Paginated search history' }) | |
| async getHistory(() dto: SearchHistoryQueryDto, () req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.searchService.getHistory(userId, dto.page, dto.limit); | |
| } | |
| ('history') | |
| (HttpStatus.OK) | |
| ({ | |
| summary: 'Clear search history', | |
| description: 'Delete all search history for the authenticated user.', | |
| }) | |
| ({ status: 200, description: 'Search history cleared successfully' }) | |
| async clearHistory(() req: any) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.searchService.clearHistory(userId); | |
| } | |
| } | |