diff --git a/backend/databases/organization_manager.py b/backend/databases/organization_manager.py index c8cc954..6bfd0f3 100644 --- a/backend/databases/organization_manager.py +++ b/backend/databases/organization_manager.py @@ -2,6 +2,7 @@ This module provides a OrganizationManager class that handles database operations related to the Organization model. It uses SQLAlchemy for ORM operations and facilitates CRUD operations on user data. """ +from fastapi import HTTPException from sqlalchemy.orm import Session from models.organization import Organization @@ -65,3 +66,15 @@ def create_organization(self, organization: Organization): self.db_session.commit() self.db_session.refresh(organization) return organization + + def delete_organization(self, org_id: int): + organization = ( + self.db_session.query(Organization) + .filter(Organization.id == org_id) + .first() + ) + if organization: + self.db_session.delete(organization) + self.db_session.commit() + else: + raise HTTPException(status_code=404, detail="Organization not found") diff --git a/backend/routes/organization_routes.py b/backend/routes/organization_routes.py index 20c71ea..8568e01 100644 --- a/backend/routes/organization_routes.py +++ b/backend/routes/organization_routes.py @@ -42,3 +42,13 @@ async def save_organization( created_organization = org_manager.create_organization(new_organization) response = OrganizationCreateResponse(created_organization.name) return response + + +@organization_router.delete("/organization/{org_id}") +async def delete_organization( + org_id: int, current_admin: User = Depends(get_current_admin_user) +): + with DatabaseManager() as session: + org_manager = OrganizationManager(session) + org_manager.delete_organization(org_id) + return {"detail": "Organization deleted successfully"} diff --git a/frontend/src/pages/Admin/OrganizationManagement.jsx b/frontend/src/pages/Admin/OrganizationManagement.jsx index 9be6f23..fdad9a5 100644 --- a/frontend/src/pages/Admin/OrganizationManagement.jsx +++ b/frontend/src/pages/Admin/OrganizationManagement.jsx @@ -1,34 +1,63 @@ // OrganizationManagement.jsx -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { API_URL } from '../../utils/constants.jsx'; -import { Button, Card, CardContent, TextField, Typography, Snackbar, Alert } from '@mui/material'; +import { Button, Card, CardContent, TextField, Typography, Snackbar, Alert, Select, MenuItem, Box } from '@mui/material'; function OrganizationManagement() { const [organizationName, setOrganizationName] = useState(''); + const [organizations, setOrganizations] = useState([]); + const [selectedOrganization, setSelectedOrganization] = useState(''); const [openSnackbar, setOpenSnackbar] = useState(false); const [snackbarMessage, setSnackbarMessage] = useState(''); const [snackbarSeverity, setSnackbarSeverity] = useState('success'); // 'error' or 'success' + useEffect(() => { + // Fetch organizations when the component mounts + fetchOrganizations(); + }, []); + + const fetchOrganizations = () => { + axios.get(`${API_URL}organizations/`) + .then(response => { + setOrganizations(response.data); + }) + .catch(error => { + console.error('Error fetching organizations', error); + }); + }; + const handleAddOrganization = () => { axios.post(`${API_URL}organization/`, { name: organizationName }) .then(response => { - // Handle successful addition - console.log('Organization added:', response.data); setSnackbarMessage('Organization added successfully.'); setSnackbarSeverity('success'); setOpenSnackbar(true); - setOrganizationName(''); // Reset the input field + setOrganizationName(''); + fetchOrganizations(); // Refresh the list of organizations }) .catch(error => { - // Handle error - console.error('Error adding organization', error); setSnackbarMessage('Error adding organization.'); setSnackbarSeverity('error'); setOpenSnackbar(true); }); }; + const handleDeleteOrganization = () => { + axios.delete(`${API_URL}organization/${selectedOrganization}`) + .then(response => { + setSnackbarMessage('Organization deleted successfully.'); + setSnackbarSeverity('success'); + setOpenSnackbar(true); + fetchOrganizations(); // Refresh the list of organizations + }) + .catch(error => { + setSnackbarMessage('Error deleting organization.'); + setSnackbarSeverity('error'); + setOpenSnackbar(true); + }); + }; + return ( @@ -40,16 +69,41 @@ function OrganizationManagement() { fullWidth margin="normal" /> - + + + {/* Added margin top to the dropdown as well */} + + + + - + - {/* Snackbar for showing notifications */} + setOpenSnackbar(false)}> setOpenSnackbar(false)} severity={snackbarSeverity} sx={{ width: '100%' }}> {snackbarMessage}