created API For Aplication Absensi
This commit is contained in:
80
app/helpers/responses.js
Normal file
80
app/helpers/responses.js
Normal file
@@ -0,0 +1,80 @@
|
||||
const bodyParser = require('body-parser');
|
||||
const express = require('express');
|
||||
const { getErrorMessage } = require('../constants/api_constans');
|
||||
|
||||
const success = (res, data, msg, param = null) => {
|
||||
var dataJson = data;
|
||||
var data = {};
|
||||
if (param != null) data[param] = dataJson;
|
||||
else data = dataJson;
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: msg,
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
const failed = (res, errorCode, message = null) => {
|
||||
const error = getErrorMessage(errorCode);
|
||||
|
||||
return res.status(error.httpCode).json({
|
||||
success: false,
|
||||
message: message || error.message,
|
||||
error_code: errorCode,
|
||||
data: null,
|
||||
});
|
||||
};
|
||||
|
||||
const pagination = (page, size) => {
|
||||
let limit = null;
|
||||
let offset = null;
|
||||
|
||||
if(page == null) page = 1;
|
||||
if(size == null) size = 10;
|
||||
|
||||
if (page != null) {
|
||||
limit = size ? +size : 5;
|
||||
offset = limit ? 0 + (page - 1) * limit : null;
|
||||
}
|
||||
|
||||
return {
|
||||
limit,
|
||||
offset,
|
||||
};
|
||||
}
|
||||
|
||||
function paginationResponse(model, page, limit, dataKey, resource) {
|
||||
const totalItem = model.count;
|
||||
const currentPage = parseInt(page);
|
||||
const totalPages = Math.ceil(totalItem / limit);
|
||||
|
||||
return {
|
||||
[dataKey]: resource,
|
||||
pagination: {
|
||||
total_item: totalItem,
|
||||
total_page: totalPages,
|
||||
current_page: currentPage,
|
||||
limit: parseInt(limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const error = (res, errorCode, message = null) => {
|
||||
const error = getErrorMessage(errorCode);
|
||||
|
||||
return res.status(error.httpCode).json({
|
||||
success: false,
|
||||
message: message || error.message,
|
||||
error_code: errorCode,
|
||||
data: null,
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
success,
|
||||
pagination,
|
||||
paginationResponse,
|
||||
failed,
|
||||
error,
|
||||
};
|
||||
Reference in New Issue
Block a user