Emmanuel Frimpong Asante
"Update system"
c514c85
raw
history blame
3.1 kB
# controllers/health_management.py
from flask import request, jsonify
from models.schemas.health_schema import create_health_record, get_health_record_by_id, get_health_records_by_user
from services.disease_detection import detect_disease
from controllers.data_logging import log_health_record
# Detect Disease and Log Health Record
def detect_and_log_disease():
"""
Detect disease from the provided image and log the health record.
Expects a POST request with the following fields:
- user_id: ID of the user submitting the disease detection request.
- poultry_id: The ID of the poultry bird.
- image: Image of the poultry feces for disease detection (binary file).
Returns:
- JSON response with the disease detection result and health record logging status.
"""
try:
# Extract the data from the request
user_id = request.form.get('user_id')
poultry_id = request.form.get('poultry_id')
image = request.files.get('image')
if not user_id or not poultry_id or not image:
return jsonify({"error": "Missing required fields: user_id, poultry_id, or image"}), 400
# Run disease detection
disease_name, status, recommendation = detect_disease(image)
# Log the health record
record_id = log_health_record(
user_id=user_id,
poultry_id=poultry_id,
disease_name=disease_name,
status=status,
recommendation=recommendation
)
return jsonify({
"message": "Disease detected and health record logged successfully",
"disease_name": disease_name,
"status": status,
"recommendation": recommendation,
"record_id": record_id
}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
# Get Health Record by ID
def get_health_record(record_id):
"""
Retrieves a health record by its record_id (MongoDB ObjectId).
Expects:
- record_id: ID of the health record.
Returns:
- JSON response with the health record details or an error message if not found.
"""
try:
health_record = get_health_record_by_id(record_id)
if not health_record:
return jsonify({"message": "Health record not found"}), 404
return jsonify(health_record), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
# Get Health Records by User
def get_health_records_for_user(user_id):
"""
Retrieves all health records created by a specific user.
Expects:
- user_id: ID of the user whose records are being requested.
Returns:
- JSON response with the list of health records for the user or an error message if not found.
"""
try:
health_records = get_health_records_by_user(user_id)
if not health_records:
return jsonify({"message": "No health records found for this user"}), 404
return jsonify(health_records), 200
except Exception as e:
return jsonify({"error": str(e)}), 500