wisdom #2
71
controllers/sub_section.controller.js
Normal file
71
controllers/sub_section.controller.js
Normal file
@@ -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;
|
||||||
87
services/sub_section.service.js
Normal file
87
services/sub_section.service.js
Normal file
@@ -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;
|
||||||
Reference in New Issue
Block a user