Skip to content

Commit

Permalink
[FIX] #2 - 취업교육리스트 API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
charBS0701 committed Aug 8, 2023
1 parent 4fa246e commit 83b102b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 41 deletions.
5 changes: 5 additions & 0 deletions config/baseResponseStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ module.exports = {
code: 404,
message: "해당 취업교육이 존재하지 않습니다.",
},
JOBEDU_SEARCH_EMPTY: {
isSuccess: false,
code: 404,
message: "검색 결과가 없습니다.",
},

// JobPosting
JOBPOSTING_ID_EMPTY: {
Expand Down
37 changes: 17 additions & 20 deletions src/app/JobEdu/jobEduController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const jobEduProvider = require("./jobEduProvider");
const baseResponse = require("../../../config/baseResponseStatus");
const { response, errResponse } = require("../../../config/response");

// 취업교육 목록 조회
export const getJobEduList = async (req, res) => {
try {
const page = req.query.page || 1;
Expand All @@ -13,27 +14,13 @@ export const getJobEduList = async (req, res) => {
page,
active_status
);

const totalCount = await jobEduProvider.retrieveJobEduListCount();
const totalPage = Math.ceil(totalCount / pageSize);

// 3. 사용자가 요청한 페이지 번호(`page`)가 총 페이지 수보다 큰지 확인한다.
if (page > totalPage) {
throw new Error("Page out of bounds");
}

const resultData = {
totalCount,
totalPage,
jobEduListResult,
};

return res.send(response(baseResponse.SUCCESS, resultData));

return res.send(response(baseResponse.SUCCESS, jobEduListResult));
} catch (error) {
console.error(error);
if (error.message === "Page out of bounds") {
return res.send(errResponse(baseResponse.PAGE_OUT_OF_BOUNDS_ERROR));
}
console.error(error);
return res.send(errResponse(baseResponse.DB_ERROR));
}
};
Expand All @@ -57,10 +44,14 @@ export const getJobEduById = async (req, res) => {
return res.send(response(baseResponse.SUCCESS, jobEduByIdResult));
};

// 취업교육 키워드 검색
export const getJobEduBySearch = async (req, res) => {
const keyword = req.query.keyword;
const page = req.query.page || 1; // 페이지 번호가 주어지지 않은 경우 기본값은 1
const pageSize = req.query.pageSize || 11; // 페이지 크기가 주어지지 않은 경우 기본값은 1
const active_status = req.query.active_status;

// 키워드가 없으면 에러
if (!keyword) {
return res.status(400).json({ error: "keyword is required" });
}
Expand All @@ -69,11 +60,17 @@ export const getJobEduBySearch = async (req, res) => {
const data = await jobEduProvider.searchWithPagination(
keyword,
page,
pageSize
pageSize,
active_status
);
return res.send(response(baseResponse.SUCCESS, data));
} catch (err) {
console.log(err.stack);
} catch (error) {
if(error.message === "No search results") {
return res.send(errResponse(baseResponse.JOBEDU_SEARCH_EMPTY));
}
if(error.message === "Page out of bounds") {
return res.send(errResponse(baseResponse.PAGE_OUT_OF_BOUNDS_ERROR));
}
res.status(500).json({ error: "Something went wrong" });
}
};
32 changes: 22 additions & 10 deletions src/app/JobEdu/jobEduDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export const selectJobEduList = async function (
}
};

export const selectJobEduListCount = async function () {
const query = `
select count(*) as total_count from job_educations;
export const selectJobEduListCount = async function (active_status) {
const query = active_status
? `select count(*) as total_count from job_educations WHERE active_status = ${active_status};`
: ` select count(*) as total_count from job_educations;
`;

try {
Expand All @@ -46,14 +47,18 @@ export const selectJobEduById = async function (jobEduId) {
}
};

// 취업교육 검색결과 데이터 가져오는 쿼리
export const selectJobEduByKeyword = async function (
keyword,
offset,
pageSize
pageSize,
active_status
) {
const query = `
SELECT * FROM job_educations WHERE title LIKE '%${keyword}%' OR content LIKE '%${keyword}%' ORDER BY posted_at DESC LIMIT ${pageSize} OFFSET ${offset};`;

const query = active_status
? `SELECT * FROM job_educations WHERE (title LIKE '%${keyword}%' OR content LIKE '%${keyword}%') AND
active_status = ${active_status} ORDER BY posted_at DESC LIMIT ${pageSize} OFFSET ${offset};`
: `SELECT * FROM job_educations WHERE title LIKE '%${keyword}%' OR content LIKE '%${keyword}%' ORDER BY posted_at DESC LIMIT ${pageSize} OFFSET ${offset};`;

try {
const result = await pool.query(query);
return result.rows;
Expand All @@ -63,9 +68,16 @@ export const selectJobEduByKeyword = async function (
}
};

export const selectJobEduTotalCountByKeyword = async function (keyword) {
const query = `
SELECT count(*) as total_count FROM job_educations WHERE title LIKE '%${keyword}%' OR content LIKE '%${keyword}%';`;
// 취업교육 검색결과 데이터 갯수 세는 쿼리
export const selectJobEduTotalCountByKeyword = async function (
keyword,
active_status
) {
const query = active_status
? `SELECT count(*) as total_count FROM job_educations WHERE (title LIKE '%${keyword}%' OR content LIKE '%${keyword}%') AND
active_status = ${active_status};`
: `SELECT count(*) as total_count FROM job_educations WHERE title LIKE '%${keyword}%' OR content LIKE '%${keyword}%';
`;

try {
const result = await pool.query(query);
Expand Down
43 changes: 34 additions & 9 deletions src/app/JobEdu/jobEduProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ const jobEduDao = require("./jobEduDao");

const { logger } = require("../../../config/winston");

// 취업지원 목록 조회
export const retrieveJobEduList = async function (pageSize, page, active_status) {
try {
// 1. 총 데이터의 개수를 알아낸다.
const totalCountResult = await jobEduDao.selectJobEduListCount(active_status);
const totalCount = totalCountResult.rows[0].total_count;

// 2. 주어진 `pageSize`에 따라 총 페이지 수를 계산한다.
const totalPage = Math.ceil(totalCount / pageSize);

// 3. 사용자가 요청한 페이지 번호(`page`)가 총 페이지 수보다 큰지 확인한다.
if (page > totalPage) {
throw new Error("Page out of bounds");
}

const offset = (page - 1) * pageSize;
const result = await jobEduDao.selectJobEduList(pageSize, offset, active_status);
return result;
return { totalCount, totalPage, result };
} catch (error) {
logger.error("DB 연결 실패");
logger.error(error);
throw error;
}
};
Expand Down Expand Up @@ -40,19 +53,31 @@ export const retrieveJobEduById = async function (jobEduId) {
};

// 취업지원 검색 함수
export const searchWithPagination = async function (keyword, page, pageSize) {
export const searchWithPagination = async function (keyword, page, pageSize, active_status) {
try {
// 1. 총 데이터의 개수를 알아낸다.
const totalCount = await jobEduDao.selectJobEduTotalCountByKeyword(keyword, active_status);
// 검색결과가 없으면 에러
if (parseInt(totalCount) === 0) {
throw new Error("No search results");
}

// 2. 주어진 `pageSize`에 따라 총 페이지 수를 계산한다.
const totalPage = Math.ceil(totalCount / pageSize);

// 3. 사용자가 요청한 페이지 번호(`page`)가 총 페이지 수보다 큰지 확인한다.
if (page > totalPage) {
throw new Error("Page out of bounds");
}

const offset = (page - 1) * pageSize;
const result = await jobEduDao.selectJobEduByKeyword(
keyword,
offset,
pageSize
pageSize,
active_status
);
const totalCount = await jobEduDao.selectJobEduTotalCountByKeyword(keyword);
return {
totalCount: totalCount,
data: result,
};
return { totalCount, totalPage, result };
} catch (error) {
logger.error("DB 연결 실패");
logger.error(error);
Expand Down
2 changes: 1 addition & 1 deletion src/app/JobPosting/jobPostingDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const selectJobPostingById = async function (jobPostingId) {
}
};

// 채용공고 검색결과 데이터 가져오는 쿼리
export const selectJobPostingByKeyword = async function (
keyword,
offset,
Expand All @@ -73,7 +74,6 @@ export const selectJobPostingTotalCountByKeyword = async function (
keyword,
active_status
) {
console.log(active_status);
const query = active_status
? `SELECT count(*) as total_count FROM job_postings WHERE (title LIKE '%${keyword}%' OR content LIKE '%${keyword}%') AND
active_status = ${active_status};`
Expand Down
1 change: 0 additions & 1 deletion src/app/JobPosting/jobPostingProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const searchWithPagination = async function (keyword, page, pageSize, act
keyword,
active_status
);
console.log(totalCount);
// 검색결과가 없으면 에러
if (parseInt(totalCount) === 0) {
throw new Error("No search results");
Expand Down

0 comments on commit 83b102b

Please sign in to comment.