Skip to content
Snippets Groups Projects
Commit f7b4fbfa authored by Tarek Shah's avatar Tarek Shah
Browse files

implemented deleting collections

parent 32130bec
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -57,6 +57,20 @@ class SqliteDatabaseAPI(database_api.CrisisEventsDatabase): ...@@ -57,6 +57,20 @@ class SqliteDatabaseAPI(database_api.CrisisEventsDatabase):
self._disconnect(connection, cur) self._disconnect(connection, cur)
return collection_id return collection_id
def delete_collection(self, collection_name):
connection, cur = self._connect()
cur.execute("DELETE FROM event_collections WHERE collection_name = ?", (collection_name,))
# Check if any row was affected
if cur.rowcount > 0:
print(f"Deleted collection '{collection_name}'")
self._disconnect(connection, cur)
return True
else:
print(f"Collection '{collection_name}' not found")
self._disconnect(connection, cur)
return False
def create_raw_text_file(self,collection_id,path): def create_raw_text_file(self,collection_id,path):
connection, cur = self._connect() connection, cur = self._connect()
cur.execute("INSERT INTO raw_text_files VALUES (NULL, ?, ?)", (collection_id,path,)) cur.execute("INSERT INTO raw_text_files VALUES (NULL, ?, ?)", (collection_id,path,))
......
...@@ -186,6 +186,28 @@ def v1_create_collection(): ...@@ -186,6 +186,28 @@ def v1_create_collection():
"collection_id":id "collection_id":id
}, 200 }, 200
@app.route('/api/v1/delete_collection', methods=['DELETE'])
def v1_delete_collection():
data = request.json
if "collection_info" not in data:
abort(400)
collection_info = data["collection_info"]
if "collection_name" not in collection_info:
abort(400)
collection_name = collection_info["collection_name"]
# Call the database function to delete the collection
success = database.delete_collection(collection_name)
if success:
return jsonify({"status": "success", "message": f"Collection '{collection_name}' deleted"}), 200
else:
abort(404, f"Collection '{collection_name}' not found")
@app.route('/api/v1/update_collection_glob', methods=['POST']) @app.route('/api/v1/update_collection_glob', methods=['POST'])
def v1_update_collection_glob(): def v1_update_collection_glob():
......
...@@ -127,6 +127,11 @@ function App() { ...@@ -127,6 +127,11 @@ function App() {
updateFileLists(); updateFileLists();
} }
const handleDeletedCollection = () => {
setSelectedCollection(null);
showEditor();
}
const logoutFunction = () => { const logoutFunction = () => {
setAuthenticated(false); setAuthenticated(false);
sessionStorage.setItem('authenticated', 'false'); sessionStorage.setItem('authenticated', 'false');
...@@ -192,7 +197,7 @@ function App() { ...@@ -192,7 +197,7 @@ function App() {
{/* <Box sx={{border: '1px dashed grey', padding:1}}> */} {/* <Box sx={{border: '1px dashed grey', padding:1}}> */}
<Grid container spacing={2}> <Grid container spacing={2}>
<Grid item xs={2}> <Grid item xs={2}>
<CollectionList collections={loadedCollections} onSelectCollection={setSelectedCollection} onListUpdate={updateCollectionList} onFileSelect={onFileSelect} collectionFileCache={collectionFileCache}/> <CollectionList collections={loadedCollections} onSelectCollection={setSelectedCollection} onListUpdate={updateCollectionList} onFileSelect={onFileSelect} collectionFileCache={collectionFileCache} onDeletedCollection={handleDeletedCollection}/>
</Grid> </Grid>
<Grid item xs={10}> <Grid item xs={10}>
{ {
......
import * as React from 'react' import * as React from 'react'
import './Sidebar.css' import './Sidebar.css'
import {dbCreateCollection, dbGetCollection, dbUploadRawTextFiles, dbUploadUrlFile, dbUploadRawHTMLFiles} from './database_util' import {dbCreateCollection, dbGetCollection, dbUploadRawTextFiles, dbUploadUrlFile, dbUploadRawHTMLFiles, dbDeleteCollection} from './database_util'
//General MUI imports //General MUI imports
...@@ -85,6 +85,18 @@ const CollectionList = (props) => { ...@@ -85,6 +85,18 @@ const CollectionList = (props) => {
props.onSelectCollection(detailCollection); props.onSelectCollection(detailCollection);
} }
const handleDelete = async (collectionName) => {
const isDeleted = await dbDeleteCollection(collectionName);
if (isDeleted) {
console.log(`Collection "${collectionName}" deleted successfully`);
props.onDeletedCollection(true);
props.onListUpdate();
} else {
console.error(`Failed to delete collection "${collectionName}"`);
}
handleDeleteClose();
}
return ( return (
<div className="sidebar-container"> <div className="sidebar-container">
<aside id="aside" className="sidebar"> <aside id="aside" className="sidebar">
...@@ -184,7 +196,7 @@ const CollectionList = (props) => { ...@@ -184,7 +196,7 @@ const CollectionList = (props) => {
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>
<Button onClick={handleDeleteClose}>Cancel</Button> <Button onClick={handleDeleteClose}>Cancel</Button>
<Button >Delete</Button> <Button onClick={() => handleDelete(displayedCollection)}>Delete</Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
</div> </div>
......
...@@ -29,6 +29,31 @@ export const dbCreateCollection = async (newCollectionName, newCollectionType) ...@@ -29,6 +29,31 @@ export const dbCreateCollection = async (newCollectionName, newCollectionType)
return result return result
}; };
export const dbDeleteCollection = async (collectionName) => {
console.log("dbDeleteCollection");
const response = await fetch(
"http://127.0.0.1:5000/api/v1/delete_collection",
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
"collection_info": { "collection_name": collectionName }
}),
}
);
const responseData = await response.json();
if (responseData.status === "success") {
console.log("dbDeleteCollection Success");
return true;
} else {
console.error("dbDeleteCollection Failed");
return false;
}
};
export const dbGetCollection = async (collection_id) => { export const dbGetCollection = async (collection_id) => {
console.log("dbGetCollection"); console.log("dbGetCollection");
let result = await fetch("http://127.0.0.1:5000//api/v1/get_collection?collection=" + collection_id,) let result = await fetch("http://127.0.0.1:5000//api/v1/get_collection?collection=" + collection_id,)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment