BE-SPAN/controller/spcoImportInfoController.js
githubna-ilham b7f9f07963 ADD api test
2024-10-23 15:58:38 +07:00

59 lines
1.8 KiB
JavaScript

const spcoImportInfoService = require("../services/spcoImportInfoService");
const { sequelize } = require("../models");
// exports.getAllImportInfo = async (req, res) => {
// try {
// const importInfo = await spcoImportInfoService.getAllImportInfo();
// res.status(200).json(importInfo);
// } catch (error) {
// res
// .status(500)
// .json({ message: "Error fetching data", error: error.message });
// }
// };
exports.getAllImportInfo = async (req, res) => {
try {
// Ambil parameter limit dan page dari request query, atau gunakan nilai default
const limit = parseInt(req.query.limit) || 10; // Default 10 item per page
const page = parseInt(req.query.page) || 1; // Default halaman 1
const offset = (page - 1) * limit; // Hitung offset untuk pagination
// Panggil service dan tambahkan pagination
const importInfo = await spcoImportInfoService.getAllImportInfo(
limit,
offset
);
res.status(200).json({
data: importInfo.rows, // Data yang diambil
currentPage: page, // Halaman saat ini
totalItems: importInfo.count, // Total data
totalPages: Math.ceil(importInfo.count / limit), // Total halaman
});
} catch (error) {
res
.status(500)
.json({ message: "Error fetching data", error: error.message });
}
};
exports.testRawSQL = async (req, res) => {
try {
// Menjalankan SQL mentah untuk melihat data dari tabel
const results = await sequelize.query(
"SELECT * FROM SOA_ADM_DASHBOARD.SPCO_IMPORT_INFO",
{
type: sequelize.QueryTypes.SELECT, // Menentukan tipe query SELECT
}
);
res.status(200).json(results); // Mengirimkan hasil query ke client
} catch (err) {
res
.status(500)
.json({ message: "Error executing raw SQL", error: err.message });
}
};