From aa3bbac5a5ebee7b40ce3000497073519dfee72c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Mon, 30 Sep 2024 15:30:04 +0700 Subject: [PATCH] add search #619 --- src/controllers/PortfolioController.ts | 41 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/controllers/PortfolioController.ts b/src/controllers/PortfolioController.ts index e4d174c..7eac17d 100644 --- a/src/controllers/PortfolioController.ts +++ b/src/controllers/PortfolioController.ts @@ -12,6 +12,7 @@ import { SuccessResponse, Response, Get, + Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -21,6 +22,7 @@ import { Not } from "typeorm"; import { CreatePortfolio, Portfolio } from "../entities/Portfolio"; import { RequestWithUser } from "../middlewares/user"; import { setLogDataDiff } from "../interfaces/utils"; +import { Brackets } from "typeorm"; @Route("api/v1/development/portfolio") @Tags("Portfolio") @@ -40,20 +42,31 @@ export class PortfolioController extends Controller { * */ @Get() - async GetResult(@Request() request: RequestWithUser) { - const _portfolio = await this.portfolioRepository.find({ - where: { createdUserId: request.user.sub }, - select: [ - "id", - "name", - "detail", - "createdAt", - "lastUpdatedAt", - "createdFullName", - "lastUpdateFullName", - ], - order: { name: "ASC" }, - }); + async GetResult(@Request() request: RequestWithUser, @Query("keyword") keyword?: string) { + const _portfolio = await this.portfolioRepository + .createQueryBuilder("portfolio") + .select([ + "portfolio.id", + "portfolio.name", + "portfolio.detail", + "portfolio.createdAt", + "portfolio.lastUpdatedAt", + "portfolio.createdFullName", + "portfolio.lastUpdateFullName", + ]) + .where("portfolio.createdUserId = :userId", { userId: request.user.sub }) + .andWhere( + new Brackets((qb) => { + qb.where(keyword != null && keyword != "" ? "portfolio.name LIKE :keyword" : "1=1", { + keyword: `%${keyword}%`, + }).orWhere(keyword != null && keyword != "" ? "portfolio.detail LIKE :keyword" : "1=1", { + keyword: `%${keyword}%`, + }); + }), + ) + .getMany(); + + return new HttpSuccess(_portfolio); }