Skip to content

Commit

Permalink
redis 변경사항 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
neverlish committed May 27, 2023
1 parent e54ae0c commit e990091
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
56 changes: 29 additions & 27 deletions src/controllers/Perfume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ const searchPerfume: RequestHandler = (
* - application/json
* parameters:
* - in: body
* name: body
* name: body
* schema:
* type: object
* type: object
* responses:
* 200:
* description: 성공
Expand All @@ -266,34 +266,31 @@ const updateSimilarPerfumes: RequestHandler = async (
next: NextFunction
): Promise<any> => {
try {
const perfumeSimilarRequest : any = req.body;
const perfumeSimilarRequest: any = req.body;

logger.debug(
`${LOG_TAG} updateSimilarPerfumes(
body = ${JSON.stringify(req.body)}
)`
);

const result: any = await Perfume.updateSimilarPerfumes(perfumeSimilarRequest);


const result = await Perfume.updateSimilarPerfumes(
perfumeSimilarRequest
);

LoggerHelper.logTruncated(
logger.debug,
`${LOG_TAG} updateSimilarPerfumes's result = ${result}`
);

res.status(StatusCode.OK).json(
new SimpleResponseDTO(
MSG_POST_PERFUME_RECOMMEND_SIMMILAR_SUCCESS
)
new SimpleResponseDTO(MSG_POST_PERFUME_RECOMMEND_SIMMILAR_SUCCESS)
);
} catch(err: any) {
} catch (err: any) {
next(err);
}


}
};


/**
* @swagger
* /perfume/{perfumeIdx}/like:
Expand Down Expand Up @@ -506,7 +503,7 @@ const recommendPersonalPerfume: RequestHandler = (
)})`
);
const pagingDTO: PagingDTO = pagingRequestDTO.toPageDTO();
return Perfume.getPerfumesByRandom(pagingDTO.limit, 3)
return Perfume.getPerfumesByRandom(pagingDTO.limit, 3)
.then((result: ListAndCountDTO<PerfumeThumbKeywordDTO>) => {
return supplementPerfumeWithRandom(result, pagingDTO.limit);
})
Expand Down Expand Up @@ -582,12 +579,10 @@ const recommendSimilarPerfumeList: RequestHandler = async (
next: NextFunction
): Promise<any> => {
try {
const pagingRequestDTO: PagingRequestDTO = PagingRequestDTO.createByJson(
req.query,
{
const pagingRequestDTO: PagingRequestDTO =
PagingRequestDTO.createByJson(req.query, {
requestSize: DEFAULT_SIMILAR_PERFUMES_REQUEST_SIZE,
}
);
});
logger.debug(
`${LOG_TAG} recommendSimilarPerfumeList(query = ${JSON.stringify(
req.query
Expand All @@ -596,23 +591,30 @@ const recommendSimilarPerfumeList: RequestHandler = async (
const pagingDTO: PagingDTO = pagingRequestDTO.toPageDTO();
const perfumeIdx: number = req.params['perfumeIdx'];

const similarPerfumeList: ListAndCountDTO<PerfumeThumbKeywordDTO> = await Perfume.getRecommendedSimilarPerfumeList(perfumeIdx, pagingDTO.limit)
const response: ListAndCountDTO<PerfumeRecommendResponse> = similarPerfumeList.convertType(PerfumeRecommendResponse.createByJson);

const similarPerfumeList: ListAndCountDTO<PerfumeThumbKeywordDTO> =
await Perfume.getRecommendedSimilarPerfumeList(
perfumeIdx,
pagingDTO.limit
);
const response: ListAndCountDTO<PerfumeRecommendResponse> =
similarPerfumeList.convertType(
PerfumeRecommendResponse.createByJson
);

LoggerHelper.logTruncated(
logger.debug,
`${LOG_TAG} recommendPersonalPerfume's result = ${response}`
);

res.status(StatusCode.OK).json(
new ResponseDTO<ListAndCountDTO<PerfumeRecommendResponse>>(
MSG_GET_RECOMMEND_SIMILAR_PERFUMES,
response
)
);
} catch(err: any) {
} catch (err: any) {
next(err);
}
}
};

/**
Expand Down
30 changes: 12 additions & 18 deletions src/dao/PerfumeDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const PERFUME_THUMB_COLUMNS: string[] = [
'updatedAt',
];

import redis from '@utils/db/redis';

const SQL_SEARCH_PERFUME_SELECT: string =
'SELECT ' +
'p.perfume_idx AS perfumeIdx, p.brand_idx AS brandIdx, p.name, p.english_name AS englishName, p.image_url AS imageUrl, p.created_at AS createdAt, p.updated_at AS updatedAt, ' +
Expand Down Expand Up @@ -391,23 +393,17 @@ class PerfumeDao {
*/
async updateSimilarPerfumes(similarPerfumes: {
[x: number]: number[];
}): Promise<[err: number, result: number][]> {
try {
const redis = require('@utils/db/redis.js');

const obj = similarPerfumes;
const multi = await redis.multi();
}): Promise<[error: Error | null, result: unknown][] | null> {
const obj = similarPerfumes;
const multi = await redis.multi();

for (const key in obj) {
multi.del(`recs.perfume:${key}`);
obj[key].forEach((v: any) => {
multi.rpush(`recs.perfume:${key}`, v);
});
}
return await multi.exec();
} catch (err) {
throw err;
for (const key in obj) {
multi.del(`recs.perfume:${key}`);
obj[key].forEach((v: any) => {
multi.rpush(`recs.perfume:${key}`, v);
});
}
return await multi.exec();
}
/**
* 서베이 추천 향수 조회
Expand Down Expand Up @@ -468,9 +464,7 @@ class PerfumeDao {
`${LOG_TAG} getSimilarPerfumeIdxList(perfumeIdx = ${perfumeIdx}, size = ${size})`
);

const client = require('@utils/db/redis.js');

const result = await client.lrange(
const result = await redis.lrange(
`recs.perfume:${perfumeIdx}`,
0,
size - 1
Expand Down
9 changes: 4 additions & 5 deletions tests/test_unit/dao/PerfumeDao.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,10 @@ describe('# perfumeDao Test', () => {
describe('# recommend similar Test', () => {
it('# update recommended similar perfumes ', async () => {
try {
const result: any[] =
await perfumeDao.updateSimilarPerfumes({
1: [2, 3],
2: [3, 4, 5],
});
const result: any = await perfumeDao.updateSimilarPerfumes({
1: [2, 3],
2: [3, 4, 5],
});

expect(result.length).to.be.eq(7);

Expand Down

0 comments on commit e990091

Please sign in to comment.