Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin list query #507

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/controllers/Admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: 성공
Expand Down Expand Up @@ -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<ListAndCountDTO<PerfumeResponse>>(
Expand Down Expand Up @@ -219,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: 성공
Expand Down Expand Up @@ -256,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<ListAndCountDTO<IngredientFullResponse>>(
Expand Down
7 changes: 5 additions & 2 deletions src/dao/IngredientDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]';

Expand Down Expand Up @@ -90,7 +90,7 @@ class IngredientDao {
*
* @returns {Promise<IngredientDTO[]>}
*/
async readPage(offset: number, limit: number) {
async readPage(offset: number, limit: number, where?: WhereOptions) {
logger.debug(`${LOG_TAG} readAll()`);
return Ingredient.findAll({
offset,
Expand All @@ -99,6 +99,9 @@ class IngredientDao {
{ model: Series, as: 'Series' },
{ model: IngredientCategories, as: 'IngredientCategories' },
],
where,
raw: true,
nest: true,
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/dao/PerfumeDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -568,11 +568,15 @@ class PerfumeDao {
*
* @returns {Promise<Perfume[]>}
*/
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,
});
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/service/IngredientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ListAndCountDTO,
PagingDTO,
} from '@dto/index';
import { Op } from 'sequelize';

const LOG_TAG: string = '[Ingredient/Service]';

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions src/service/PerfumeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -624,9 +625,26 @@ class PerfumeService {

async readPage(
offset: number,
limit: number
limit: number,
query: any
): Promise<ListAndCountDTO<PerfumeThumbDTO>> {
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);
}
Expand Down