diff --git a/src/constants/responseMessage.js b/src/constants/responseMessage.js index 5900ec8..10200dc 100644 --- a/src/constants/responseMessage.js +++ b/src/constants/responseMessage.js @@ -74,6 +74,9 @@ module.exports = { LIGHT_GET_NEW_SUCCESS: 'New 번개 조회 성공', LIGHT_GET_HOT_SUCCESS: 'Hot 번개 조회 성공', LIGHT_GET_SEARCH_SUCCESS: 'Search 번개 조회 성공', + LIGHT_GET_MY_ENTER_SEARCH_SUCCESS: '내가 참여한 번개 검색 조회 성공', + LIGHT_GET_MY_SCARP_SEARCH_SUCCESS: '내가 찜한 번개 검색 조회 성공', + LIGHT_GET_MY_OPEN_SEARCH_SUCCESS: '내가 오픈한 번개 검색 조회 성공', NOT_LIGHT_ORGANIZER: '번개 소유자가 아닙니다.', EXIST_LIGHT_USER: '해당 번개에 참여중 입니다.', EXIST_NOT_LIGHT_USER: '해당 번개에 참여중이 아닙니다.', diff --git a/src/controller/lightController.js b/src/controller/lightController.js index ba783b1..237281e 100644 --- a/src/controller/lightController.js +++ b/src/controller/lightController.js @@ -248,6 +248,87 @@ const ExistLightUser = async (req, res, next) => { return next(new Error('existLightUser Controller 에러: \n' + error)); } }; +const getSearchMyEnterLight = async (req, res, next) => { + const memberId = req.user.id; + const { crewId } = req.params; + const search = req.query.search; + const category = req.query.category; + + var curpage = req.query.curpage || 1; + var pageSize = req.query.pageSize || 5; + + let offset = (curpage - 1) * Number(pageSize); + let limit = Number(pageSize); + + if (!memberId) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NULL_VALUE)); + } + + if (search.length < 2) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NO_TWO_SEARCH_QUERY)); + } + try { + const lights = await lightService.getSearchMyEnterLight(memberId, crewId, search, category, offset, limit); + + return res.status(lights.status).json(lights); + } catch (error) { + return next(new Error('getSearchLight Controller 에러: \n' + error)); + } +}; +const getSearchMyScrapLight = async (req, res, next) => { + const memberId = req.user.id; + const { crewId } = req.params; + const search = req.query.search; + const category = req.query.category; + + var curpage = req.query.curpage || 1; + var pageSize = req.query.pageSize || 5; + + let offset = (curpage - 1) * Number(pageSize); + let limit = Number(pageSize); + + if (!memberId) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NULL_VALUE)); + } + + if (search.length < 2) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NO_TWO_SEARCH_QUERY)); + } + try { + const lights = await lightService.getSearchMyScrapLight(memberId, crewId, search, category, offset, limit); + + return res.status(lights.status).json(lights); + } catch (error) { + return next(new Error('getSearchLight Controller 에러: \n' + error)); + } +}; +const getSearchMyOpenLight = async (req, res, next) => { + const memberId = req.user.id; + const { crewId } = req.params; + const search = req.query.search; + const category = req.query.category; + + var curpage = req.query.curpage || 1; + var pageSize = req.query.pageSize || 5; + + let offset = (curpage - 1) * Number(pageSize); + let limit = Number(pageSize); + + if (!memberId) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NULL_VALUE)); + } + + if (search.length < 2) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NO_TWO_SEARCH_QUERY)); + } + try { + const lights = await lightService.getSearchMyOpenLight(memberId, crewId, search, category, offset, limit); + + return res.status(lights.status).json(lights); + } catch (error) { + return next(new Error('getSearchLight Controller 에러: \n' + error)); + } +}; module.exports = { addLight, @@ -262,5 +343,8 @@ module.exports = { getNewLight, getHotLight, getSearchLight, - ExistLightUser + ExistLightUser, + getSearchMyEnterLight, + getSearchMyScrapLight, + getSearchMyOpenLight }; diff --git a/src/db/lightDao.js b/src/db/lightDao.js index 10d3193..dc0f012 100644 --- a/src/db/lightDao.js +++ b/src/db/lightDao.js @@ -321,7 +321,125 @@ const IsLightOrganizer = async (client, lightId, userId) => { throw new Error('lightdao.IsLightOrganizer 에러 발생했습니다 \n' + error); } }; +const getSearchLightUseCategoryInMyEnterLight = async (client, search, category, crewId,memberId, offset, limit) => { + try { + const { rows } = await client.query( + ` + select l.id, category, scp_cnt, join_cnt, title, date, time, people_cnt, description, image, place from light l + left join (select light_id, count(id) join_cnt from light_user group by light_id) ls on l.id = ls.light_id + left join (select light_id, count(id) scp_cnt from scrap group by light_id) ld on l.id = ld.light_id + inner join light_user lu on l.id = lu.light_id + where (l.title LIKE CONCAT('%', $1::text, '%') or l.description Like CONCAT('%', $1::text, '%')) and category = $2 and l.crew_id = $3 + and lu.member_id = $4 + offset $5 + limit $6; + `, + [search, category, crewId, memberId, offset, limit], + ); + return convertSnakeToCamel.keysToCamel(rows); + } catch (error) { + throw new Error('lightdao.getSearchLightUseCategoryInMyEnterLight 에러 발생했습니다 \n' + error); + } +}; +const getSearchLightNotCategoryInMyEnterLight = async (client, search, crewId, memberId, offset, limit) => { + try { + const { rows } = await client.query( + ` + select l.id, category, scp_cnt, join_cnt, title, date, time, people_cnt, description, image, place from light l + left join (select light_id, count(id) join_cnt from light_user group by light_id) ls on l.id = ls.light_id + left join (select light_id, count(id) scp_cnt from scrap group by light_id) ld on l.id = ld.light_id + inner join light_user lu on l.id = lu.light_id + where (l.title LIKE CONCAT('%', $1::text, '%') or l.description Like CONCAT('%', $1::text, '%')) and l.crew_id = $2 + and lu.member_id = $3 + offset $4 + limit $5; + `, + [search, crewId, memberId, offset, limit], + ); + return convertSnakeToCamel.keysToCamel(rows); + } catch (error) { + throw new Error('lightdao.getSearchLightNotCategoryInMyEnterLight 에러 발생했습니다 \n' + error); + } +}; +const getSearchLightUseCategoryInMyScrapLight = async (client, search, category, crewId,memberId, offset, limit) => { + try { + const { rows } = await client.query( + ` + select l.id, category, scp_cnt, join_cnt, title, date, time, people_cnt, description, image, place from light l + left join (select light_id, count(id) join_cnt from light_user group by light_id) ls on l.id = ls.light_id + left join (select light_id, count(id) scp_cnt from scrap group by light_id) ld on l.id = ld.light_id + inner join scrap s on l.id = s.light_id + where (l.title LIKE CONCAT('%', $1::text, '%') or l.description Like CONCAT('%', $1::text, '%')) and category = $2 and l.crew_id = $3 + and s.member_id = $4 + offset $5 + limit $6; + `, + [search, category, crewId, memberId, offset, limit], + ); + return convertSnakeToCamel.keysToCamel(rows); + } catch (error) { + throw new Error('lightdao.getSearchLightUseCategoryInMyScrapLight 에러 발생했습니다 \n' + error); + } +}; +const getSearchLightNotCategoryInMyScrapLight = async (client, search, crewId, memberId, offset, limit) => { + try { + const { rows } = await client.query( + ` + select l.id, category, scp_cnt, join_cnt, title, date, time, people_cnt, description, image, place from light l + left join (select light_id, count(id) join_cnt from light_user group by light_id) ls on l.id = ls.light_id + left join (select light_id, count(id) scp_cnt from scrap group by light_id) ld on l.id = ld.light_id + inner join scrap s on l.id = s.light_id + where (l.title LIKE CONCAT('%', $1::text, '%') or l.description Like CONCAT('%', $1::text, '%')) and l.crew_id = $2 + and s.member_id = $3 + offset $4 + limit $5; + `, + [search, crewId, memberId, offset, limit], + ); + return convertSnakeToCamel.keysToCamel(rows); + } catch (error) { + throw new Error('lightdao.getSearchLightNotCategoryInMyScrapLight 에러 발생했습니다 \n' + error); + } +}; +const getSearchLightUseCategoryInMyOpenLight = async (client, search, category, crewId,memberId, offset, limit) => { + try { + const { rows } = await client.query( + ` + select l.id, category, scp_cnt, join_cnt, title, date, time, people_cnt, description, image, place from light l + left join (select light_id, count(id) join_cnt from light_user group by light_id) ls on l.id = ls.light_id + left join (select light_id, count(id) scp_cnt from scrap group by light_id) ld on l.id = ld.light_id + where (l.title LIKE CONCAT('%', $1::text, '%') or l.description Like CONCAT('%', $1::text, '%')) and category = $2 and l.crew_id = $3 + and l.organizer_id = $4 + offset $5 + limit $6; + `, + [search, category, crewId, memberId, offset, limit], + ); + return convertSnakeToCamel.keysToCamel(rows); + } catch (error) { + throw new Error('lightdao.getSearchLightUseCategoryInMyOpenLight 에러 발생했습니다 \n' + error); + } +}; +const getSearchLightNotCategoryInMyOpenLight = async (client, search, crewId, memberId, offset, limit) => { + try { + const { rows } = await client.query( + ` + select l.id, category, scp_cnt, join_cnt, title, date, time, people_cnt, description, image, place from light l + left join (select light_id, count(id) join_cnt from light_user group by light_id) ls on l.id = ls.light_id + left join (select light_id, count(id) scp_cnt from scrap group by light_id) ld on l.id = ld.light_id + where (l.title LIKE CONCAT('%', $1::text, '%') or l.description Like CONCAT('%', $1::text, '%')) and l.crew_id = $2 + and l.organizer_id = $3 + offset $4 + limit $5; + `, + [search, crewId, memberId, offset, limit], + ); + return convertSnakeToCamel.keysToCamel(rows); + } catch (error) { + throw new Error('lightdao.getSearchLightNotCategoryInMyOpenLight 에러 발생했습니다 \n' + error); + } +}; module.exports = { addLight, putLight, @@ -340,5 +458,12 @@ module.exports = { getHotLight, getSearchLightUseCategory, getSearchLightNotCategory, - IsLightOrganizer + IsLightOrganizer, + getSearchLightUseCategoryInMyEnterLight, + getSearchLightNotCategoryInMyEnterLight, + getSearchLightUseCategoryInMyScrapLight, + getSearchLightNotCategoryInMyScrapLight, + getSearchLightUseCategoryInMyOpenLight, + getSearchLightNotCategoryInMyOpenLight + }; diff --git a/src/routes/lightApi.js b/src/routes/lightApi.js index 8b5b1dc..cabd9be 100644 --- a/src/routes/lightApi.js +++ b/src/routes/lightApi.js @@ -16,7 +16,10 @@ router.get('/:crewId/new', authMiddleware, lightController.getNewLight); router.get('/:crewId/search', authMiddleware, lightController.getSearchLight); router.get('/:crewId/hot', authMiddleware, lightController.getHotLight); router.get('/:crewId', lightController.getCategoryLight); -router.get('/:lightId', lightController.getLightDetail); +router.get('/detail/:lightId', lightController.getLightDetail); router.get('/exist/:lightId', authMiddleware, lightController.ExistLightUser); +router.get('/:crewId/enter/search', authMiddleware, lightController.getSearchMyEnterLight); +router.get('/:crewId/scrap/search', authMiddleware, lightController.getSearchMyScrapLight); +router.get('/:crewId/open/search', authMiddleware, lightController.getSearchMyOpenLight); module.exports = router; diff --git a/src/service/lightService.js b/src/service/lightService.js index b5cfc04..8243069 100644 --- a/src/service/lightService.js +++ b/src/service/lightService.js @@ -705,7 +705,259 @@ const existLightUser = async (lightId, memberId) => { client.release(); } }; +const getSearchMyEnterLight = async (memberId, crewId, search, category, offset, limit) => { + let client; + + const log = `lightService.getSearchMyEnterLight | memberId = ${memberId}`; + try { + client = await db.connect(log); + await client.query('BEGIN'); + + // 존재하는 유저인지 확인 + const exist = await userDao.getUserById(client, memberId); + if (!exist) { + return util.fail(statusCode.BAD_REQUEST, responseMessage.NO_USER); + } + // 존재 하는 동아리인지 검사 + const existCrew = await crewDao.getExistCrew(client, crewId); + if (!existCrew) { + return util.fail(statusCode.BAD_REQUEST, responseMessage.NO_CREW); + } + + if (category) { + const result = await lightDao.getSearchLightUseCategoryInMyEnterLight(client, search, category,crewId, memberId, offset, limit); + + const totalCount = result.length; + const totalPage = pageNation.getTotalPage(totalCount, limit); + + const lightData = result.map((light) => { + const is_opened = light.joinCnt >= light.peopleCnt || light.date < new Date() ? false : true; + const time = light.time == null ? null : light.time.slice(0, -3); + const date = light.date == null ? null : dayjs(light.date).format('YYYY-MM-DD'); + const place = light.place == null ? null : light.place; + const people_cnt = light.peopleCnt == null ? null : light.peopleCnt; + return { + light_id: Number(light.id), + title: light.title, + category: light.category, + scp_cnt: Number(light.scpCnt), + date, + time, + people_cnt, + place, + LightMemberCnt: Number(light.joinCnt), + is_opened, + }; + }); + + await client.query('COMMIT'); + return util.success(statusCode.OK, responseMessage.LIGHT_GET_MY_ENTER_SEARCH_SUCCESS, { lightData, offset, limit, totalCount, totalPage }); + } + if (!category) { + const result = await lightDao.getSearchLightNotCategoryInMyEnterLight(client, search, crewId, memberId,offset, limit); + + const totalCount = result.length; + const totalPage = pageNation.getTotalPage(totalCount, limit); + + const lightData = result.map((light) => { + const is_opened = light.joinCnt >= light.peopleCnt || light.date < new Date() ? false : true; + const time = light.time == null ? null : light.time.slice(0, -3); + const date = light.date == null ? null : dayjs(light.date).format('YYYY-MM-DD'); + const place = light.place == null ? null : light.place; + const people_cnt = light.peopleCnt == null ? null : light.peopleCnt; + return { + light_id: Number(light.id), + title: light.title, + category: light.category, + scp_cnt: Number(light.scpCnt), + date, + time, + people_cnt, + place, + LightMemberCnt: Number(light.joinCnt), + is_opened, + }; + }); + + await client.query('COMMIT'); + return util.success(statusCode.OK, responseMessage.LIGHT_GET_MY_ENTER_SEARCH_SUCCESS, { lightData, offset, limit, totalCount, totalPage }); + } + } catch (error) { + await client.query('ROLLBACK'); + throw new Error('lightService getSearchMyEnterLight error 발생: \n' + error); + } finally { + client.release(); + } +}; +const getSearchMyScrapLight = async (memberId, crewId, search, category, offset, limit) => { + let client; + + const log = `lightService.getSearchMyScrapLight | memberId = ${memberId}`; + try { + client = await db.connect(log); + await client.query('BEGIN'); + + // 존재하는 유저인지 확인 + const exist = await userDao.getUserById(client, memberId); + if (!exist) { + return util.fail(statusCode.BAD_REQUEST, responseMessage.NO_USER); + } + // 존재 하는 동아리인지 검사 + const existCrew = await crewDao.getExistCrew(client, crewId); + if (!existCrew) { + return util.fail(statusCode.BAD_REQUEST, responseMessage.NO_CREW); + } + + if (category) { + const result = await lightDao.getSearchLightUseCategoryInMyScrapLight(client, search, category,crewId, memberId, offset, limit); + const totalCount = result.length; + const totalPage = pageNation.getTotalPage(totalCount, limit); + + const lightData = result.map((light) => { + const is_opened = light.joinCnt >= light.peopleCnt || light.date < new Date() ? false : true; + const time = light.time == null ? null : light.time.slice(0, -3); + const date = light.date == null ? null : dayjs(light.date).format('YYYY-MM-DD'); + const place = light.place == null ? null : light.place; + const people_cnt = light.peopleCnt == null ? null : light.peopleCnt; + return { + light_id: Number(light.id), + title: light.title, + category: light.category, + scp_cnt: Number(light.scpCnt), + date, + time, + people_cnt, + place, + LightMemberCnt: Number(light.joinCnt), + is_opened, + }; + }); + + await client.query('COMMIT'); + return util.success(statusCode.OK, responseMessage.LIGHT_GET_MY_SCARP_SEARCH_SUCCESS, { lightData, offset, limit, totalCount, totalPage }); + } + if (!category) { + const result = await lightDao.getSearchLightNotCategoryInMyScrapLight(client, search, crewId, memberId,offset, limit); + + const totalCount = result.length; + const totalPage = pageNation.getTotalPage(totalCount, limit); + + const lightData = result.map((light) => { + const is_opened = light.joinCnt >= light.peopleCnt || light.date < new Date() ? false : true; + const time = light.time == null ? null : light.time.slice(0, -3); + const date = light.date == null ? null : dayjs(light.date).format('YYYY-MM-DD'); + const place = light.place == null ? null : light.place; + const people_cnt = light.peopleCnt == null ? null : light.peopleCnt; + return { + light_id: Number(light.id), + title: light.title, + category: light.category, + scp_cnt: Number(light.scpCnt), + date, + time, + people_cnt, + place, + LightMemberCnt: Number(light.joinCnt), + is_opened, + }; + }); + + await client.query('COMMIT'); + return util.success(statusCode.OK, responseMessage.LIGHT_GET_MY_SCARP_SEARCH_SUCCESS, { lightData, offset, limit, totalCount, totalPage }); + } + } catch (error) { + await client.query('ROLLBACK'); + throw new Error('lightService getSearchMyScrapLight error 발생: \n' + error); + } finally { + client.release(); + } +}; + +const getSearchMyOpenLight = async (memberId, crewId, search, category, offset, limit) => { + let client; + + const log = `lightService.getSearchMyOpenLight | memberId = ${memberId}`; + try { + client = await db.connect(log); + await client.query('BEGIN'); + + // 존재하는 유저인지 확인 + const exist = await userDao.getUserById(client, memberId); + if (!exist) { + return util.fail(statusCode.BAD_REQUEST, responseMessage.NO_USER); + } + // 존재 하는 동아리인지 검사 + const existCrew = await crewDao.getExistCrew(client, crewId); + if (!existCrew) { + return util.fail(statusCode.BAD_REQUEST, responseMessage.NO_CREW); + } + + if (category) { + const result = await lightDao.getSearchLightUseCategoryInMyOpenLight(client, search, category,crewId, memberId, offset, limit); + + const totalCount = result.length; + const totalPage = pageNation.getTotalPage(totalCount, limit); + + const lightData = result.map((light) => { + const is_opened = light.joinCnt >= light.peopleCnt || light.date < new Date() ? false : true; + const time = light.time == null ? null : light.time.slice(0, -3); + const date = light.date == null ? null : dayjs(light.date).format('YYYY-MM-DD'); + const place = light.place == null ? null : light.place; + const people_cnt = light.peopleCnt == null ? null : light.peopleCnt; + return { + light_id: Number(light.id), + title: light.title, + category: light.category, + scp_cnt: Number(light.scpCnt), + date, + time, + people_cnt, + place, + LightMemberCnt: Number(light.joinCnt), + is_opened, + }; + }); + + await client.query('COMMIT'); + return util.success(statusCode.OK, responseMessage.LIGHT_GET_MY_OPEN_SEARCH_SUCCESS, { lightData, offset, limit, totalCount, totalPage }); + } + if (!category) { + const result = await lightDao.getSearchLightNotCategoryInMyOpenLight(client, search, crewId, memberId,offset, limit); + + const totalCount = result.length; + const totalPage = pageNation.getTotalPage(totalCount, limit); + + const lightData = result.map((light) => { + const is_opened = light.joinCnt >= light.peopleCnt || light.date < new Date() ? false : true; + const time = light.time == null ? null : light.time.slice(0, -3); + const date = light.date == null ? null : dayjs(light.date).format('YYYY-MM-DD'); + const place = light.place == null ? null : light.place; + const people_cnt = light.peopleCnt == null ? null : light.peopleCnt; + return { + light_id: Number(light.id), + title: light.title, + category: light.category, + scp_cnt: Number(light.scpCnt), + date, + time, + people_cnt, + place, + LightMemberCnt: Number(light.joinCnt), + is_opened, + }; + }); + + await client.query('COMMIT'); + return util.success(statusCode.OK, responseMessage.LIGHT_GET_MY_OPEN_SEARCH_SUCCESS, { lightData, offset, limit, totalCount, totalPage }); + } + } catch (error) { + await client.query('ROLLBACK'); + throw new Error('lightService getSearchMyOpenLight error 발생: \n' + error); + } finally { + client.release(); + } +}; module.exports = { addLight, putLight, @@ -722,4 +974,7 @@ module.exports = { getHotLight, getSearchLight, existLightUser, + getSearchMyEnterLight, + getSearchMyScrapLight, + getSearchMyOpenLight };