From d5c53b2953949ecde813279dcd5674f293508d7e Mon Sep 17 00:00:00 2001 From: Antony Kurniawan Date: Sat, 11 Oct 2025 16:21:14 +0700 Subject: [PATCH] add: crud subsection --- controllers/sub_section.controller.js | 71 ++++++++++++++++++++++ services/sub_section.service.js | 87 +++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 controllers/sub_section.controller.js create mode 100644 services/sub_section.service.js diff --git a/controllers/sub_section.controller.js b/controllers/sub_section.controller.js new file mode 100644 index 0000000..f2a9655 --- /dev/null +++ b/controllers/sub_section.controller.js @@ -0,0 +1,71 @@ +const SubSectionService = require('../services/sub_section.service'); +const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils'); +const { insertSubSectionSchema, updateSubSectionSchema } = require('../validate/sub_section.schema'); + +class SubSectionController { + // Get all sub sections + static async getAll(req, res) { + const queryParams = req.query; + + const results = await SubSectionService.getAll(queryParams); + const response = await setResponsePaging(queryParams, results, 'Sub section found'); + + res.status(response.statusCode).json(response); + } + + // Get sub section by ID + static async getById(req, res) { + const { id } = req.params; + + const results = await SubSectionService.getById(id); + const response = await setResponse(results, 'Sub section found'); + + res.status(response.statusCode).json(response); + } + + // Create sub section + static async create(req, res) { + const { error, value } = await checkValidate(insertSubSectionSchema, req); + + if (error) { + return res.status(400).json(setResponse(error, 'Validation failed', 400)); + } + + value.userId = req.user.user_id; + + const results = await SubSectionService.create(value); + const response = await setResponse(results, 'Sub section created successfully'); + + return res.status(response.statusCode).json(response); + } + + // Update sub section + static async update(req, res) { + const { id } = req.params; + + const { error, value } = checkValidate(updateSubSectionSchema, req); + + if (error) { + return res.status(400).json(setResponse(error, 'Validation failed', 400)); + } + + value.userId = req.user.user_id; + + const results = await SubSectionService.update(id, value); + const response = await setResponse(results, 'Sub section updated successfully'); + + res.status(response.statusCode).json(response); + } + + // Soft delete sub section + static async delete(req, res) { + const { id } = req.params; + + const results = await SubSectionService.delete(id, req.user.user_id); + const response = await setResponse(results, 'Sub section deleted successfully'); + + res.status(response.statusCode).json(response); + } +} + +module.exports = SubSectionController; \ No newline at end of file diff --git a/services/sub_section.service.js b/services/sub_section.service.js new file mode 100644 index 0000000..8860bfd --- /dev/null +++ b/services/sub_section.service.js @@ -0,0 +1,87 @@ +const { + getAllSubSectionsDb, + getSubSectionByIdDb, + createSubSectionDb, + updateSubSectionDb, + deleteSubSectionDb +} = require('../db/sub_section.db'); +const { ErrorHandler } = require('../helpers/error'); + +class SubSectionService { + // Get all sub sections + static async getAll(param) { + try { + const results = await getAllSubSectionsDb(param); + + results.data.map(el => {}); + + return results; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Get sub section by ID + static async getById(id) { + try { + const result = await getSubSectionByIdDb(id); + + if (result.length < 1) throw new ErrorHandler(404, 'Sub section not found'); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Create sub section + static async create(data) { + try { + if (!data || typeof data !== 'object') data = {}; + + const result = await createSubSectionDb(data); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Update sub section + static async update(id, data) { + try { + if (!data || typeof data !== 'object') data = {}; + + const dataExist = await getSubSectionByIdDb(id); + + if (dataExist.length < 1) { + throw new ErrorHandler(404, 'Sub section not found'); + } + + const result = await updateSubSectionDb(id, data); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } + + // Soft delete sub section + static async delete(id, userId) { + try { + const dataExist = await getSubSectionByIdDb(id); + + if (dataExist.length < 1) { + throw new ErrorHandler(404, 'Sub section not found'); + } + + const result = await deleteSubSectionDb(id, userId); + + return result; + } catch (error) { + throw new ErrorHandler(error.statusCode, error.message); + } + } +} + +module.exports = SubSectionService;