From 4230666bb22d3667de8f211ee78870d209cc5fa8 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Tue, 13 Jun 2023 11:00:57 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Perfume=20readPage=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=EC=97=90=20where=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dao/PerfumeDao.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/dao/PerfumeDao.ts b/src/dao/PerfumeDao.ts index ade2b771..2f609067 100644 --- a/src/dao/PerfumeDao.ts +++ b/src/dao/PerfumeDao.ts @@ -24,7 +24,7 @@ import { sequelize, } from '@sequelize'; -import Sequelize, { QueryTypes } from 'sequelize'; +import Sequelize, { QueryTypes, WhereOptions } from 'sequelize'; const PERFUME_THUMB_COLUMNS: string[] = [ 'perfumeIdx', @@ -568,11 +568,15 @@ class PerfumeDao { * * @returns {Promise} */ - async readPage(offset: number, limit: number) { + async readPage(offset: number, limit: number, where?: WhereOptions) { logger.debug(`${LOG_TAG} readAll()`); return Perfume.findAll({ offset, limit, + include: [{ model: Brand, as: 'Brand' }], + where, + raw: true, + nest: true, }); } } From 40424c8406eaf7881dad95991a1545aced6201b2 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Tue, 13 Jun 2023 11:01:51 +0900 Subject: [PATCH 2/4] =?UTF-8?q?query=EC=97=90=20=EB=94=B0=EB=9D=BC=20where?= =?UTF-8?q?=20=ED=98=95=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/PerfumeService.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/service/PerfumeService.ts b/src/service/PerfumeService.ts index 5aee191e..6fe467e7 100644 --- a/src/service/PerfumeService.ts +++ b/src/service/PerfumeService.ts @@ -34,6 +34,7 @@ import fp from 'lodash/fp'; import _ from 'lodash'; import IngredientDao from '@src/dao/IngredientDao'; import { Ingredient } from '@sequelize'; +import { Op } from 'sequelize'; const LOG_TAG: string = '[Perfume/Service]'; const DEFAULT_VALUE_OF_INDEX = 0; @@ -624,9 +625,26 @@ class PerfumeService { async readPage( offset: number, - limit: number + limit: number, + query: any ): Promise> { - const perfumes = await perfumeDao.readPage(offset, limit); + const { target, keyword } = query; + const whereOptions = {} as any; + if (target && keyword) { + switch (target) { + case 'id': + whereOptions.perfumeIdx = keyword; + break; + case 'name': + whereOptions.name = { [Op.startsWith]: keyword }; + break; + case 'englishName': + whereOptions.englishName = { [Op.startsWith]: keyword }; + break; + } + } + + const perfumes = await perfumeDao.readPage(offset, limit, whereOptions); const list = perfumes.map((c) => PerfumeThumbDTO.createByJson(c)); return new ListAndCountDTO(list.length, list); } From e7496c4871a360163d557729b27be0efdec24113 Mon Sep 17 00:00:00 2001 From: Jinho Hyeon Date: Tue, 13 Jun 2023 11:02:06 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=ED=96=A5=EC=88=98=20=EB=AA=A9=EB=A1=9D?= =?UTF-8?q?=EC=97=90=20=EA=B2=80=EC=83=89=EC=96=B4=20=EC=A1=B0=EA=B1=B4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/Admin.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/controllers/Admin.ts b/src/controllers/Admin.ts index d32ba207..73baaccb 100644 --- a/src/controllers/Admin.ts +++ b/src/controllers/Admin.ts @@ -156,6 +156,18 @@ export const getPerfume: RequestHandler = async ( * required: true * type: integer * format: int64 + * - name: target + * in: query + * required: false + * type: string + # enum: + # - id + # - name + # - englishName + * - name: keyword + * in: query + * required: false + * type: string * responses: * 200: * description: 성공 @@ -191,7 +203,8 @@ export const getPerfumes: RequestHandler = async ( } const limit = 20; const offset = (page - 1) * limit; - const perfumes = await Perfume.readPage(offset, limit); + + const perfumes = await Perfume.readPage(offset, limit, req.query); res.status(StatusCode.OK).json( new ResponseDTO>( From 78df11e00bc8d0ca6a9d4a1af2df2678ddcd5e32 Mon Sep 17 00:00:00 2001 From: Taehoya Date: Mon, 12 Jun 2023 21:45:10 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=ED=96=A5=EC=88=98=20=EA=B2=80=EC=83=89?= =?UTF-8?q?=EC=97=90=20=ED=95=84=ED=84=B0=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/Admin.ts | 14 +++++++++++++- src/dao/IngredientDao.ts | 7 +++++-- src/service/IngredientService.ts | 25 +++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/controllers/Admin.ts b/src/controllers/Admin.ts index 73baaccb..7221342d 100644 --- a/src/controllers/Admin.ts +++ b/src/controllers/Admin.ts @@ -232,6 +232,18 @@ export const getPerfumes: RequestHandler = async ( * required: true * type: integer * format: int64 + * - name: target + * in: query + * required: false + * type: string + * enum: + * - id + * - name + * - englishName + * - name: keyword + * in: query + * required: false + * type: string * responses: * 200: * description: 성공 @@ -269,7 +281,7 @@ export const getIngredientAll: RequestHandler = async ( } const limit = 20; const offset = (page - 1) * limit; - const ingredients = await Ingredient.readPage(offset, limit); + const ingredients = await Ingredient.readPage(offset, limit, req.query); res.status(StatusCode.OK).json( new ResponseDTO>( diff --git a/src/dao/IngredientDao.ts b/src/dao/IngredientDao.ts index 2c7fa9c6..da9566f5 100644 --- a/src/dao/IngredientDao.ts +++ b/src/dao/IngredientDao.ts @@ -4,7 +4,7 @@ import { NotMatchedError } from '@errors'; import { IngredientDTO, ListAndCountDTO, PagingDTO } from '@dto/index'; import { Series, Ingredient, IngredientCategories } from '@sequelize'; -import { Op } from 'sequelize'; +import { Op, WhereOptions } from 'sequelize'; const LOG_TAG: string = '[Ingredient/DAO]'; @@ -90,7 +90,7 @@ class IngredientDao { * * @returns {Promise} */ - async readPage(offset: number, limit: number) { + async readPage(offset: number, limit: number, where?: WhereOptions) { logger.debug(`${LOG_TAG} readAll()`); return Ingredient.findAll({ offset, @@ -99,6 +99,9 @@ class IngredientDao { { model: Series, as: 'Series' }, { model: IngredientCategories, as: 'IngredientCategories' }, ], + where, + raw: true, + nest: true, }); } } diff --git a/src/service/IngredientService.ts b/src/service/IngredientService.ts index 89acd01e..f65c85b4 100644 --- a/src/service/IngredientService.ts +++ b/src/service/IngredientService.ts @@ -8,6 +8,7 @@ import { ListAndCountDTO, PagingDTO, } from '@dto/index'; +import { Op } from 'sequelize'; const LOG_TAG: string = '[Ingredient/Service]'; @@ -49,8 +50,28 @@ class IngredientService { this.ingredientDao = dao; } - async readPage(offset: number, limit: number) { - const perfumes = await this.ingredientDao.readPage(offset, limit); + async readPage(offset: number, limit: number, query: any) { + const { target, keyword } = query; + const whereOptions = {} as any; + if (target && keyword) { + switch (target) { + case 'id': + whereOptions.ingredientIdx = keyword; + break; + case 'name': + whereOptions.name = { [Op.startsWith]: keyword }; + break; + case 'englishName': + whereOptions.englishName = { [Op.startsWith]: keyword }; + break; + } + } + + const perfumes = await this.ingredientDao.readPage( + offset, + limit, + whereOptions + ); const perfumesWithCategory = perfumes.map((perfume) => { return { ...perfume,