2023-11-12 12:05:59 +04:00
|
|
|
from flask import Flask, jsonify, request
|
2023-12-04 18:24:11 +04:00
|
|
|
import requests
|
2023-11-12 12:05:59 +04:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
# набор данных пациентов и их врачей
|
|
|
|
patients_data = [
|
|
|
|
{'id': 1, 'name': 'Patient A', 'email': 'patient.a@example.com', 'doctor_id': 1},
|
|
|
|
{'id': 2, 'name': 'Patient B', 'email': 'patient.b@example.com', 'doctor_id': 2},
|
|
|
|
{'id': 3, 'name': 'Patient C', 'email': 'patient.c@example.com', 'doctor_id': 1},
|
|
|
|
]
|
|
|
|
|
|
|
|
# URL сервиса докторов
|
|
|
|
doctor_service_url = "http://localhost:5000"
|
2023-11-12 12:05:59 +04:00
|
|
|
|
|
|
|
|
2023-11-12 19:01:15 +04:00
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def get_index():
|
2023-12-04 18:24:11 +04:00
|
|
|
return "это второй сервис"
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/patients', methods=['GET'])
|
|
|
|
def get_patients():
|
|
|
|
return jsonify(patients_data)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/patients/<int:id>', methods=['GET'])
|
|
|
|
def get_patient(id):
|
|
|
|
for patient in patients_data:
|
|
|
|
if patient['id'] == id:
|
|
|
|
return jsonify(patient)
|
|
|
|
return jsonify({'error': 'Пациент не найден'}), 404
|
|
|
|
|
2023-11-12 12:05:59 +04:00
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
@app.route('/patients', methods=['POST'])
|
|
|
|
def create_patient():
|
|
|
|
new_patient = request.json
|
|
|
|
# назначить уникальный id новому пациенту
|
|
|
|
new_patient['id'] = len(patients_data) + 1
|
|
|
|
patients_data.append(new_patient)
|
2023-11-12 12:05:59 +04:00
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
# отправляем запрос другому сервису
|
|
|
|
requests.post(f"{doctor_service_url}/doctors/1/patients", json=new_patient)
|
2023-11-12 12:05:59 +04:00
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
return jsonify(new_patient), 201
|
2023-11-12 12:05:59 +04:00
|
|
|
|
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
@app.route('/patients/<int:id>', methods=['PUT'])
|
|
|
|
def update_patient(id):
|
|
|
|
for patient in patients_data:
|
|
|
|
if patient['id'] == id:
|
|
|
|
patient.update(request.json)
|
|
|
|
# отправляем запрос другому сервису
|
2023-12-04 18:25:17 +04:00
|
|
|
requests.put(f"{doctor_service_url}/doctors/1/patients/{id}", json=request.json)
|
2023-12-04 18:24:11 +04:00
|
|
|
return jsonify(patient), 200
|
|
|
|
return jsonify({'error': 'Пациент не найден'}), 404
|
2023-11-12 12:05:59 +04:00
|
|
|
|
2023-11-12 19:01:15 +04:00
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
@app.route('/patients/<int:id>', methods=['DELETE'])
|
|
|
|
def delete_patient(id):
|
|
|
|
# отправляем запрос другому сервису
|
2023-12-04 18:25:17 +04:00
|
|
|
requests.delete(f"{doctor_service_url}/doctors/1/patients/{id}")
|
2023-11-12 19:01:15 +04:00
|
|
|
|
2023-12-04 18:24:11 +04:00
|
|
|
global patients_data
|
|
|
|
patients_data = [patient for patient in patients_data if patient['id'] != id]
|
|
|
|
return jsonify({'message': 'Пациент удален'}), 200
|
2023-11-12 12:05:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0', port=5001)
|