router.post(‘/catalog‘, adminAuthMiddleware, login.single(‘id_proof‘), async (req, res) => {
console.log(req.body);
const { file_name, file_id, workspace, store, title, description } = req.body;
if (!file_name || !file_id || !workspace || !store || !title || !description) {
const data = { message: ‘All fields are required‘, title: “Warning“, icon: “warning“ };
return res.json(data);
}
// Define attributes
const attributes = [
{ name: ‘name‘, binding: ‘java.lang.String‘ },
{ name: ‘description‘, binding: ‘java.lang.String‘ },
{ name: ‘latitude‘, binding: ‘java.lang.Double‘ },
{ name: ‘longitude‘, binding: ‘java.lang.Double‘ }
];
const auth = {
username: ‘admin‘,
password: ‘geoserver‘
};
try {
// Check if feature type exists
const featureTypesResponse = await axios.get(`http://localhost:8080/geoserver/rest/workspaces/${workspace}/datastores/${store}/featuretypes`, {
auth: {
username: ‘admin‘,
password: ‘geoserver‘
}
});
// // Check if the featureType key is present and if it contains any feature types
let featureTypeExists = false;
console.log(“——————————————“)
console.log(“featureTypesResponse“)
// console.log(featureTypesResponse)
console.log(“featureTypesResponse“)
console.log(“——————————————“)
console.log(“——————————————“)
console.log(“featureTypesResponse.data“)
// console.log(featureTypesResponse.data)
console.log(“featureTypesResponse.data“)
console.log(“——————————————“)
console.log(“——————————————“)
console.log(“featureTypesResponse.data.featureTypes“)
// console.log(featureTypesResponse.data.featureTypes)
console.log(“featureTypesResponse.data.featureTypes“)
console.log(“——————————————“)
console.log(“Array.isArray(featureTypesResponse.data.featureTypes.featureType)“)
// console.log(Array.isArray(featureTypesResponse.data.featureTypes.featureType))
console.log(“Array.isArray(featureTypesResponse.data.featureTypes.featureType)“)
console.log(“——————————————“)
console.log(“featureTypesResponse.data.featureTypes.featureType“)
// console.log(featureTypesResponse.data.featureTypes.featureType)
console.log(“featureTypesResponse.data.featureTypes.featureType“)
if (featureTypesResponse.data.featureTypes && Array.isArray(featureTypesResponse.data.featureTypes.featureType)) {
const existingFeatureTypes = featureTypesResponse.data.featureTypes.featureType;
featureTypeExists = existingFeatureTypes.some(ft => ft.name === file_name);
console.log(“existingFeatureTypes“);
console.log(existingFeatureTypes)
console.log(“existingFeatureTypes“)
}
console.log(“featureTypeExists“)
console.log(featureTypeExists)
console.log(“featureTypeExists“)
const geoserverUrl = ‘http://localhost:8080/geoserver/rest‘;
const auth = {
username: ‘admin‘,
password: ‘geoserver‘
};
if (featureTypeExists) {
const layerResponse = await axios.delete(`${geoserverUrl}/layers/${file_name}`,
{ auth }
).then(function (response) {
console.log(“res del ————————–ftr“);
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
console.log(“res del ————————–lyr“);
}).catch(function (err) {
console.log(“res del ————————–lyrerr“);
console.log(err);
console.log(“res del ————————–lyrerr“);
})
console.log(‘Layer deleted:‘);
// // Optionally, delete the feature type
console.log(“NOT EXISTtttttttttttttttttttttttt“);
const featureTypeResponse = await axios.delete(
`${geoserverUrl}/workspaces/${workspace}/datastores/${store}/featuretypes/${file_name}?recurse=true`,
{ auth }
).then(function (response) {
console.log(“res del ————————–ftr“);
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
console.log(“res del ————————–ftr“);
}).catch(function (err) {
console.log(“res del ————————–ftrerr“);
console.log(err);
console.log(“res del ————————–ftrerr“);
})
// console.log(‘FeatureType deleted:’, featureTypeResponse.statusText);
const data = { message: ‘Layer with same already exists in the workspace and deleted successfully‘, title: “warning“, icon: “warning“ };
return res.status(201).json(data);
}
else {
// Create new feature type
await axios.post(`http://localhost:8080/geoserver/rest/workspaces/${workspace}/datastores/${store}/featuretypes`, {
featureType: {
name: file_name,
nativeName: file_name,
title: title,
srs: ‘EPSG:4326‘,
// attributes: { attribute: attributes }
}
}, {
auth: {
username: ‘admin‘,
password: ‘geoserver‘
}
})
.then(function (response) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
})
.catch(function (error) {
console.log(error);
})
// Update existing feature type if necessary
// You can add logic here to update attributes if required
}
// // Process database update and response
// const client = await pool.poolUser.connect();
// const visibility = true;
// const query = `
// INSERT INTO catalog(file_name, file_id, workspace, store, title, description, visibility)
// VALUES ($1, $2, $3, $4, $5, $6, $7)
// `;
// const values = [file_name, file_id, workspace, store, title, description, visibility];
// await client.query(query, values);
// const shapefileTableQuery = `
// UPDATE shapefiles
// SET is_added = $1
// WHERE file_name = $2
// `;
// const shapefileTableUpdate = [true, file_name];
// await client.query(shapefileTableQuery, shapefileTableUpdate);
// client.release();
const data = { message: ‘File Published to GeoServer‘, title: “Success“, icon: “success“ };
return res.status(201).json(data);
} catch (error) {
// if (error) {
// // Handle specific GeoServer errors
// // if (error.response.data.includes(‘property named name’)) {
// // Handle attribute not found error
// console.error(‘Attribute error:’, error);
// // const data = { message: ‘Feature type attribute missing’, title: “Error”, icon: “danger” };
// return res.status(400).json(data);
// // } else if (error.response.data.includes(‘no attributes were specified’)) {
// // Handle missing attributes error
// console.error(‘Missing attributes error:’, error);
// const data = { message: ‘Attributes were not specified’, title: “Error”, icon: “danger” };
// return res.status(400).json(data);
// // }
// }
console.log(“—————–error———————–“);
console.log(error)
console.log(“—————–error———————–“);
console.error(`Unexpected error: ${error}`);
const data = { message: ‘Unexpected server error‘, title: “Error“, icon: “danger“ };
return res.status(500).json(data);
}
});
Please follow and like us: