mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 13:51:12 -05:00
Refactor code structure and remove redundant changes
This commit is contained in:
100
django-backend/api/v1/endpoints/history.py
Normal file
100
django-backend/api/v1/endpoints/history.py
Normal file
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
Generic history endpoints for all entity types.
|
||||
|
||||
Provides cross-entity history operations and utilities.
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.http import Http404
|
||||
from ninja import Router, Query
|
||||
|
||||
from api.v1.services.history_service import HistoryService
|
||||
from api.v1.schemas import (
|
||||
HistoryEventDetailSchema,
|
||||
HistoryComparisonSchema,
|
||||
ErrorSchema
|
||||
)
|
||||
|
||||
router = Router(tags=['History'])
|
||||
|
||||
|
||||
@router.get(
|
||||
'/events/{event_id}',
|
||||
response={200: HistoryEventDetailSchema, 404: ErrorSchema},
|
||||
summary="Get event by ID",
|
||||
description="Retrieve any historical event by its ID (requires entity_type parameter)"
|
||||
)
|
||||
def get_event_by_id(
|
||||
request,
|
||||
event_id: int,
|
||||
entity_type: str = Query(..., description="Entity type (park, ride, company, ridemodel, review)")
|
||||
):
|
||||
"""Get a specific historical event by ID."""
|
||||
try:
|
||||
event = HistoryService.get_event(entity_type, event_id, request.user)
|
||||
if not event:
|
||||
return 404, {"error": "Event not found or not accessible"}
|
||||
|
||||
# Get entity info for response
|
||||
entity_id = str(event['entity_id'])
|
||||
entity_name = event.get('entity_name', 'Unknown')
|
||||
|
||||
# Build response
|
||||
response_data = {
|
||||
'id': event['id'],
|
||||
'timestamp': event['timestamp'],
|
||||
'operation': event['operation'],
|
||||
'entity_id': entity_id,
|
||||
'entity_type': entity_type,
|
||||
'entity_name': entity_name,
|
||||
'snapshot': event['snapshot'],
|
||||
'changed_fields': event.get('changed_fields'),
|
||||
'metadata': event.get('metadata', {}),
|
||||
'can_rollback': HistoryService.can_rollback(request.user),
|
||||
'rollback_preview': None # Could add rollback preview logic if needed
|
||||
}
|
||||
|
||||
return response_data
|
||||
except ValueError as e:
|
||||
return 404, {"error": str(e)}
|
||||
|
||||
|
||||
@router.get(
|
||||
'/compare',
|
||||
response={200: HistoryComparisonSchema, 400: ErrorSchema, 404: ErrorSchema},
|
||||
summary="Compare two events",
|
||||
description="Compare two historical events (must be same entity)"
|
||||
)
|
||||
def compare_events(
|
||||
request,
|
||||
entity_type: str = Query(..., description="Entity type (park, ride, company, ridemodel, review)"),
|
||||
event1: int = Query(..., description="First event ID"),
|
||||
event2: int = Query(..., description="Second event ID")
|
||||
):
|
||||
"""Compare two historical events."""
|
||||
try:
|
||||
comparison = HistoryService.compare_events(
|
||||
entity_type, event1, event2, request.user
|
||||
)
|
||||
|
||||
if not comparison:
|
||||
return 404, {"error": "One or both events not found or not accessible"}
|
||||
|
||||
# Format response
|
||||
response_data = {
|
||||
'entity_id': comparison['entity_id'],
|
||||
'entity_type': entity_type,
|
||||
'entity_name': comparison.get('entity_name', 'Unknown'),
|
||||
'event1': comparison['event1'],
|
||||
'event2': comparison['event2'],
|
||||
'differences': comparison['differences'],
|
||||
'changed_field_count': comparison['changed_field_count'],
|
||||
'unchanged_field_count': comparison['unchanged_field_count'],
|
||||
'time_between': comparison['time_between']
|
||||
}
|
||||
|
||||
return response_data
|
||||
except ValueError as e:
|
||||
return 400, {"error": str(e)}
|
||||
Reference in New Issue
Block a user