Skip to content
Snippets Groups Projects
Commit 6bc18cd8 authored by Lee WooChang's avatar Lee WooChang
Browse files

Merge branch 'feat/combinations' into 'main'

조합 API 추가

See merge request !11
parents 34be008f 613d8e4b
No related branches found
No related tags found
1 merge request!11조합 API 추가
Pipeline #10726 failed
......@@ -57,6 +57,36 @@ const PartRepository = {
const result = await pool.query(query, queryValues);
return result.rows;
},
async getAllCombinations() {
const query = `
SELECT array_agg(r.part_id) AS partIds
FROM combinations c
LEFT JOIN relations r ON c.id = r.combination_id
GROUP BY c.id;
`;
const result = await pool.query(query);
return result.rows;
},
async getFilteredCombinations(queryValues) {
const query = `
SELECT
c.id AS combination_id,
array_agg(r.part_id) AS partIds
FROM combinations c
LEFT JOIN relations r ON c.id = r.combination_id
WHERE r.combination_id IN (
SELECT DISTINCT combination_id
FROM relations
WHERE part_id = ANY($1::int[])
)
GROUP BY c.id;
`;
const result = await pool.query(query, queryValues);
return result.rows;
},
};
export default PartRepository;
......@@ -33,4 +33,23 @@ partRouter.get(
})
);
partRouter.get(
'/combination/all',
wrapAsync(async (req, res) => {
const allCombinations = await PartService.getCombinations();
res.sendResponse('', 200, { combination: allCombinations });
})
);
partRouter.get(
'/combination/filtered',
wrapAsync(async (req, res) => {
const filters = req.query;
const filteredCombinations =
await PartService.getFilteredCombinations(filters);
res.sendResponse('', 200, { combination: filteredCombinations });
})
);
export default partRouter;
......@@ -74,6 +74,38 @@ const PartService = {
);
return parts;
},
async getCombinations() {
const allCombinations = await PartRepository.getAllCombinations();
return allCombinations;
},
async getFilteredCombinations(filters) {
if (typeof filters.partId === 'string') {
filters.partId = filters.partId.split(',').map((id) => id.trim());
}
if (
!filters ||
!Array.isArray(filters.partId) ||
filters.partId.length === 0
) {
throw new ReportableError(400, '유효한 partId 값이 필요합니다.');
}
const validFilters = filters.partId.filter((value) => value != null);
if (validFilters.length === 0) {
throw new ReportableError(400, '유효한 partId 값이 없습니다.');
}
const queryValues = [validFilters];
const filteredCombinations =
await PartRepository.getFilteredCombinations(queryValues);
return filteredCombinations.map((combination) => ({
partIds: combination.partids,
}));
},
};
export default PartService;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment