From e8035be302ac93703324fd841d0eed56c3072079 Mon Sep 17 00:00:00 2001 From: batuhancetin Date: Sat, 23 Dec 2023 01:41:43 +0300 Subject: [PATCH] username keyword search is added --- app/backend/src/poll/poll.controller.ts | 1 - app/backend/src/user/user.controller.ts | 11 +++++++++++ app/backend/src/user/user.service.ts | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/backend/src/poll/poll.controller.ts b/app/backend/src/poll/poll.controller.ts index efaca9a6..2ba7e637 100644 --- a/app/backend/src/poll/poll.controller.ts +++ b/app/backend/src/poll/poll.controller.ts @@ -50,7 +50,6 @@ export class PollController { return await this.pollService.syncVectorStore(); } - @UseGuards(AuthGuard, VerificationGuard) @ApiResponse({ status: 200, description: 'Polls are searched successfully.', diff --git a/app/backend/src/user/user.controller.ts b/app/backend/src/user/user.controller.ts index b94167e6..4aa58d87 100644 --- a/app/backend/src/user/user.controller.ts +++ b/app/backend/src/user/user.controller.ts @@ -10,6 +10,7 @@ import { Req, Put, NotFoundException, + Query, } from '@nestjs/common'; import { UserService } from './user.service'; @@ -40,6 +41,16 @@ export class UserController { return await this.userService.findAll(); } + @Get('search/:username') + @ApiResponse({ status: 200, description: 'Users are fetched successfully.', type: [GetUserResponseDto] }) + @ApiResponse({ + status: 500, + description: 'Internal server error, contact with backend team.', + }) + public async searchUsernames(@Query('username') username: string): Promise { + return await this.userService.searchUsernames(username); + } + @ApiResponse({ status: 200, description: 'User is fetched successfully.', type: GetUserResponseDto }) @ApiResponse({ status: 404, description: 'User is not found.' }) @ApiResponse({ diff --git a/app/backend/src/user/user.service.ts b/app/backend/src/user/user.service.ts index cb179e43..d6b96740 100644 --- a/app/backend/src/user/user.service.ts +++ b/app/backend/src/user/user.service.ts @@ -226,6 +226,13 @@ export class UserService { await this.userRepository.save(user); } + public async searchUsernames(query: string): Promise { + return this.userRepository + .createQueryBuilder('user') + .where('user.username ILIKE :query', { query: `%${query}%` }) + .getMany(); + } + public generateCode(): number { return Math.floor(Math.random() * 9000 + 1000); }