Skip to content

Commit

Permalink
Merge branch 'feature/#2/jobEdu' into Develop
Browse files Browse the repository at this point in the history
  • Loading branch information
charBS0701 committed Jul 25, 2023
2 parents 5e37da2 + 998c592 commit 6d0375a
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ const dbconfig = {

const pool = new pg.Pool(dbconfig);

export default pool;
export default pool;
3 changes: 3 additions & 0 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module.exports = function () {
require('../src/app/Auth/authRoute')(app);
require('../src/app/JobCategory/jobCategoryRoute')(app);

require('../src/app/JobEdu/jobEduRoute')(app);

// require('../src/app/Board/boardRoute')(app);

return app;
};
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ logger.info(`${process.env.NODE_ENV} - API Server Start At Port ${port}`);
} catch (error) {
logger.error("Database 연결 실패");
logger.error(error);
}*/
}*/
59 changes: 59 additions & 0 deletions src/app/JobEdu/jobEduController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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;
const pageSize = req.query.pageSize || 10;
const active_status = req.query.active_status;

const jobEduListResult = await jobEduProvider.retrieveJobEduList(
pageSize,
page,
active_status
);

return res.send(response(baseResponse.SUCCESS, jobEduListResult));
} catch (error) {
console.error(error);
return res.send(errResponse(baseResponse.DB_ERROR));
}
};

export const getJobEduListCount = async (req, res) => {
const jobEduListCountResult = await jobEduProvider.retrieveJobEduListCount();

return res.send(response(baseResponse.SUCCESS, jobEduListCountResult));
};

export const getJobEduById = async (req, res) => {
const jobEduId = req.params.jobEduId;

if (!jobEduId) return res.send(errResponse(baseResponse.JOBEDU_ID_EMPTY));

const jobEduByIdResult = await jobEduProvider.retrieveJobEduById(jobEduId);

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 || 10; // 페이지 크기가 주어지지 않은 경우 기본값은 10
if (!keyword) {
return res.status(400).json({ error: "keyword is required" });
}

try {
const data = await jobEduProvider.searchWithPagination(
keyword,
page,
pageSize
);
return res.send(response(baseResponse.SUCCESS, data));
} catch (err) {
console.log(err.stack);
res.status(500).json({ error: "Something went wrong" });
}
};
73 changes: 73 additions & 0 deletions src/app/JobEdu/jobEduDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { logger } = require("../../../config/winston");
import pool from "../../../config/database";

export const selectJobEduList = async function (pageSize, offset, active_status) {
const query = active_status
? `SELECT * FROM job_educations WHERE active_status = ${active_status} ORDER BY posted_at DESC LIMIT ${pageSize} OFFSET ${offset}`
: `SELECT * FROM job_educations ORDER BY posted_at DESC LIMIT ${pageSize} OFFSET ${offset}`;

try {
const result = await pool.query(query);
return result.rows;
} catch (error) {
logger.error("selectJobEduList쿼리 실패");
throw error;
}
};

export const selectJobEduListCount = async function () {
const query = `
select count(*) as total_count from job_educations;
`;

try {
const result = await pool.query(query);
return result;
} catch (error) {
logger.error("selectJobEduListCount 쿼리 실패");
throw error;
}
};

export const selectJobEduById = async function (jobEduId) {
const query = `
SELECT * FROM job_educations WHERE job_edu_id = ${jobEduId};`;

try {
const result = await pool.query(query);
return result.rows[0];
} catch (error) {
logger.error("selectJobEduById 쿼리 실패");
throw error;
}
};

export const selectJobEduByKeyword = async function (
keyword,
offset,
pageSize
) {
const query = `
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;
} catch (error) {
logger.error("selectJobEduByKeyword 쿼리 실패");
throw error;
}
};

export const selectJobEduTotalCountByKeyword = async function (keyword) {
const query = `
SELECT count(*) as total_count FROM job_educations WHERE title LIKE '%${keyword}%' OR content LIKE '%${keyword}%';`;

try {
const result = await pool.query(query);
return result.rows[0]["total_count"];
} catch (error) {
logger.error("selectJobEduTotalCountByKeyword 쿼리 실패");
throw error;
}
};
61 changes: 61 additions & 0 deletions src/app/JobEdu/jobEduProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pool from "../../../config/database";
const jobEduDao = require("./jobEduDao");

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

export const retrieveJobEduList = async function (pageSize, page, active_status) {
try {
const offset = (page - 1) * pageSize;
const result = await jobEduDao.selectJobEduList(pageSize, offset, active_status);
return result;
} catch (error) {
logger.error("DB 연결 실패");
throw error;
}
};

// 취업지원 데이터 갯수 세는 함수
export const retrieveJobEduListCount = async function () {
try {
const result = await jobEduDao.selectJobEduListCount();

return parseInt(result.rows[0]["total_count"]);
} catch (error) {
logger.error("DB 연결 실패");
logger.error(error);
throw error;
}
};

export const retrieveJobEduById = async function (jobEduId) {
try {
const result = await jobEduDao.selectJobEduById(jobEduId);

return result;
} catch (error) {
logger.error("DB 연결 실패");
logger.error(error);
throw error;
}
};

// 취업지원 검색 함수
export const searchWithPagination = async function (keyword, page, pageSize) {
try {
const offset = (page - 1) * pageSize;
const result = await jobEduDao.selectJobEduByKeyword(
keyword,
offset,
pageSize
);
const totalCount = await jobEduDao.selectJobEduTotalCountByKeyword(keyword);
return {
totalCount: totalCount,
data: result,
};
} catch (error) {
logger.error("DB 연결 실패");
logger.error(error);
throw error;
}
};
22 changes: 22 additions & 0 deletions src/app/JobEdu/jobEduRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = function (app) {
const jobEdu = require("./jobEduController");

// 1. 취업교육 목록 API
app.get("/app/jobEdu", jobEdu.getJobEduList);

// 3. 취업교육 검색 API
app.get("/app/jobEdu/search", jobEdu.getJobEduBySearch);

// 2. 취업교육 상세 조회 API
app.get("/app/jobEdu/:jobEduId", jobEdu.getJobEduById);




/*
내부 사용 API
*/

// 취업지원 목록 개수 API
app.get("/app/jobEdu/count", jobEdu.getJobEduListCount);
};

0 comments on commit 6d0375a

Please sign in to comment.