Middle
What actions will you take to implement the report deletion feature from the system?
sobes.tech AI
Answer from AI
- Receiving the delete request: The system receives an HTTP request (most likely DELETE) with the report ID to be deleted.
- Validation: Checks whether a report with the specified ID exists and whether the current user has permission to delete it. If the report is not found or the user is unauthorized/does not have rights, an appropriate HTTP status is returned (e.g., 404 Not Found or 403 Forbidden).
- Transaction: A transaction is started to ensure atomicity of the operation.
- Data deletion:
- Deletion of the report itself from the main reports table.
- Deletion of related data (e.g., audit records, attachments, comments) that reference this report. This may require cascade deletion at the database level or manual deletion.
- External systems handling: If the report is linked to external systems (e.g., sent to analytics or finance systems), these systems may need to be notified of the deletion.
- Commit/Rollback of the transaction: If all delete operations succeed, the transaction is committed. In case of any error (e.g., when deleting a related record), the transaction is rolled back to maintain data integrity.
- Logging: The deletion event is logged with details such as user, time, and report ID.
- Response: The system sends a response to the client indicating successful deletion (e.g., HTTP status 200 OK or 204 No Content). In case of an error, an appropriate status is returned (e.g., 500 Internal Server Error).**
Example code (simplified Spring Boot with JPA):
// ReportService.java
@Service
@Transactional // Transaction management
public class ReportService {
@Autowired
private ReportRepository reportRepository; // Repository for reports
@Autowired
private AuditLogService auditLogService; // Service for audit logging
@Autowired
private AttachmentRepository attachmentRepository; // Repository for attachments
public void deleteReport(Long reportId, User currentUser) {
// 1. Retrieve report and validate
Report report = reportRepository.findById(reportId)
.orElseThrow(() -> new ReportNotFoundException("Report with id " + reportId + " not found"));
// 2. Check permissions (example)
if (!currentUser.hasPermissionToDelete(report)) {
throw new AccessDeniedException("User does not have permission to delete this report");
}
// 3. Delete related data (if no cascade delete at DB level)
attachmentRepository.deleteByReportId(reportId); // Delete attachments
// 4. Delete the report itself
reportRepository.delete(report);
// 5. Log audit event
auditLogService.logReportDeletion(reportId, currentUser.getUsername());
// 6. Transaction is committed automatically upon successful method completion
// On exception - automatic rollback
// 7. Notify external systems (optional)
// externalSystemIntegrationService.notifyReportDeleted(reportId);
}
}
// ReportRepository.java
public interface ReportRepository extends JpaRepository<Report, Long> {
// Additional methods if needed
}
// AttachmentRepository.java
public interface AttachmentRepository extends JpaRepository<Attachment, Long> {
void deleteByReportId(Long reportId); // Method to delete attachments by report ID
}
Table of possible HTTP responses:
| HTTP Status | Description |
|---|---|
| 200 OK | Report successfully deleted. |
| 204 No Content | Report successfully deleted, response body is empty. |
| 403 Forbidden | User does not have permission to delete. |
| 404 Not Found | Report with the specified ID not found. |
| 500 Internal Server Error | Internal server error during deletion. |