fix: remove duplicate /roles in API endpoints

- Fix getAllRole endpoint from roles/roles to roles
- Fix getRoleById endpoint from roles/roles/:id to roles/:id
- Fix createRole endpoint from roles/roles to roles
- Fix updateRole endpoint from roles/roles/:id to roles/:id
- Fix deleteRole endpoint from roles/roles/:id to roles/:id
- Add try-catch error handling in getAllRole
- Support both server-side and client-side pagination

This fixes SQL error: Conversion failed when converting the nvarchar value 'roles' to data type int
Backend was interpreting duplicate /roles as an ID parameter
This commit is contained in:
2025-10-10 15:45:39 +07:00
parent c3b5ec2121
commit 7a8a46ee64

View File

@@ -1,30 +1,40 @@
import { SendRequest } from '../components/Global/ApiRequest'; import { SendRequest } from '../components/Global/ApiRequest';
const getAllRole = async (queryParams) => { const getAllRole = async (queryParams) => {
try {
const response = await SendRequest({ const response = await SendRequest({
method: 'get', method: 'get',
prefix: `roles/roles?${queryParams.toString()}`, prefix: `roles?${queryParams.toString()}`,
}); });
console.log('Role API Response:', response); console.log('Role API Response:', response);
// Parse query params to get page and limit // Check if backend returns paginated data
if (response.paging) {
// Backend already provides pagination info
return {
status: response.statusCode || 200,
data: {
data: response.data || [],
paging: response.paging,
total: response.paging.total || 0
}
};
}
// Fallback: If backend returns all data without pagination
const params = Object.fromEntries(queryParams); const params = Object.fromEntries(queryParams);
const currentPage = parseInt(params.page) || 1; const currentPage = parseInt(params.page) || 1;
const currentLimit = parseInt(params.limit) || 10; const currentLimit = parseInt(params.limit) || 10;
// Backend returns all data, so we need to do client-side pagination
const allData = response.data || []; const allData = response.data || [];
const totalData = allData.length; const totalData = allData.length;
// Calculate start and end index for current page // Client-side pagination
const startIndex = (currentPage - 1) * currentLimit; const startIndex = (currentPage - 1) * currentLimit;
const endIndex = startIndex + currentLimit; const endIndex = startIndex + currentLimit;
// Slice data for current page
const paginatedData = allData.slice(startIndex, endIndex); const paginatedData = allData.slice(startIndex, endIndex);
// Transform response to match TableList expected structure
return { return {
status: response.statusCode || 200, status: response.statusCode || 200,
data: { data: {
@@ -38,12 +48,29 @@ const getAllRole = async (queryParams) => {
total: totalData, total: totalData,
}, },
}; };
} catch (error) {
console.error('getAllRole error:', error);
return {
status: 500,
data: {
data: [],
paging: {
page: 1,
limit: 10,
total: 0,
page_total: 0
},
total: 0
},
error: error.message
};
}
}; };
const getRoleById = async (id) => { const getRoleById = async (id) => {
const response = await SendRequest({ const response = await SendRequest({
method: 'get', method: 'get',
prefix: `roles/roles/${id}`, prefix: `roles/${id}`,
}); });
return response.data; return response.data;
}; };
@@ -51,7 +78,7 @@ const getRoleById = async (id) => {
const createRole = async (queryParams) => { const createRole = async (queryParams) => {
const response = await SendRequest({ const response = await SendRequest({
method: 'post', method: 'post',
prefix: `roles/roles`, prefix: `roles`,
params: queryParams, params: queryParams,
}); });
@@ -90,7 +117,7 @@ const createRole = async (queryParams) => {
const updateRole = async (role_id, queryParams) => { const updateRole = async (role_id, queryParams) => {
const response = await SendRequest({ const response = await SendRequest({
method: 'put', method: 'put',
prefix: `roles/roles/${role_id}`, prefix: `roles/${role_id}`,
params: queryParams, params: queryParams,
}); });
@@ -129,7 +156,7 @@ const updateRole = async (role_id, queryParams) => {
const deleteRole = async (queryParams) => { const deleteRole = async (queryParams) => {
const response = await SendRequest({ const response = await SendRequest({
method: 'delete', method: 'delete',
prefix: `roles/roles/${queryParams}`, prefix: `roles/${queryParams}`,
}); });
console.log('Delete API Response:', response); console.log('Delete API Response:', response);