189 lines
6.6 KiB
Python
189 lines
6.6 KiB
Python
import sys
|
|
import requests
|
|
import json
|
|
import datetime
|
|
import time
|
|
from PySide6.QtCore import QObject, Slot, Signal, Property, QTimer
|
|
from PySide6.QtQml import QQmlApplicationEngine
|
|
from PySide6.QtWidgets import QApplication
|
|
from PySide6.QtQuickControls2 import QQuickStyle
|
|
|
|
# API Configuration
|
|
URI = 'http://localhost:5244/api/'
|
|
CITATION = URI + 'Cita'
|
|
PATIENTS = URI + 'Paciente'
|
|
MEDICS = URI + 'Medico'
|
|
HEADERS = {"content-type": "application/json"}
|
|
|
|
|
|
class ApiManager(QObject):
|
|
"""Backend manager for API calls"""
|
|
|
|
dataLoaded = Signal(str, str) # Signal(dataType, jsonData)
|
|
operationComplete = Signal(str, bool, str) # Signal(operation, success, message)
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
# GET Methods
|
|
@Slot(str)
|
|
def loadData(self, dataType):
|
|
"""Load data from API (patients, medics, or citations)"""
|
|
try:
|
|
url_map = {
|
|
"patients": PATIENTS,
|
|
"medics": MEDICS,
|
|
"citations": CITATION
|
|
}
|
|
response = requests.get(url_map[dataType], headers=HEADERS)
|
|
data = response.json()
|
|
self.dataLoaded.emit(dataType, json.dumps(data))
|
|
except Exception as e:
|
|
self.operationComplete.emit(f"load_{dataType}", False, str(e))
|
|
|
|
# PATIENTS
|
|
@Slot(str, str)
|
|
def addPatient(self, nombre, apellido):
|
|
try:
|
|
datat = {
|
|
"nombre": nombre,
|
|
"apellido": apellido,
|
|
"registro": datetime.date.fromtimestamp(time.time()).isoformat()
|
|
}
|
|
response = requests.post(PATIENTS, json=datat, headers=HEADERS)
|
|
self.operationComplete.emit("add_patient", True, "Paciente agregado exitosamente")
|
|
self.loadData("patients")
|
|
except Exception as e:
|
|
self.operationComplete.emit("add_patient", False, str(e))
|
|
|
|
@Slot(int, str, str)
|
|
def updatePatient(self, pat_id, nombre, apellido):
|
|
try:
|
|
datat = {
|
|
"paciente_id": pat_id,
|
|
"nombre": nombre,
|
|
"apellido": apellido,
|
|
"registro": datetime.date.fromtimestamp(time.time()).isoformat()
|
|
}
|
|
response = requests.put(f"{PATIENTS}/{pat_id}", json=datat, headers=HEADERS)
|
|
self.operationComplete.emit("update_patient", True, "Paciente actualizado exitosamente")
|
|
self.loadData("patients")
|
|
except Exception as e:
|
|
self.operationComplete.emit("update_patient", False, str(e))
|
|
|
|
@Slot(int)
|
|
def deletePatient(self, pat_id):
|
|
try:
|
|
response = requests.delete(f"{PATIENTS}/{pat_id}", headers=HEADERS)
|
|
self.operationComplete.emit("delete_patient", True, "Paciente eliminado exitosamente")
|
|
self.loadData("patients")
|
|
except Exception as e:
|
|
self.operationComplete.emit("delete_patient", False, str(e))
|
|
|
|
# MEDICS
|
|
@Slot(str, str, str)
|
|
def addMedic(self, nombre, apellido, especialidad):
|
|
try:
|
|
datat = {
|
|
"nombre": nombre,
|
|
"apellido": apellido,
|
|
"especialidad": especialidad,
|
|
"registro": datetime.date.fromtimestamp(time.time()).isoformat()
|
|
}
|
|
response = requests.post(MEDICS, json=datat, headers=HEADERS)
|
|
self.operationComplete.emit("add_medic", True, "Médico agregado exitosamente")
|
|
self.loadData("medics")
|
|
except Exception as e:
|
|
self.operationComplete.emit("add_medic", False, str(e))
|
|
|
|
@Slot(int, str, str, str)
|
|
def updateMedic(self, med_id, nombre, apellido, especialidad):
|
|
try:
|
|
datat = {
|
|
"medico_id": med_id,
|
|
"nombre": nombre,
|
|
"apellido": apellido,
|
|
"especialidad": especialidad,
|
|
"registro": datetime.date.fromtimestamp(time.time()).isoformat()
|
|
}
|
|
response = requests.put(f"{MEDICS}/{med_id}", json=datat, headers=HEADERS)
|
|
self.operationComplete.emit("update_medic", True, "Médico actualizado exitosamente")
|
|
self.loadData("medics")
|
|
except Exception as e:
|
|
self.operationComplete.emit("update_medic", False, str(e))
|
|
|
|
@Slot(int)
|
|
def deleteMedic(self, med_id):
|
|
try:
|
|
response = requests.delete(f"{MEDICS}/{med_id}", headers=HEADERS)
|
|
self.operationComplete.emit("delete_medic", True, "Médico eliminado exitosamente")
|
|
self.loadData("medics")
|
|
except Exception as e:
|
|
self.operationComplete.emit("delete_medic", False, str(e))
|
|
|
|
# CITATIONS
|
|
@Slot(str, int, int)
|
|
def addCitation(self, detalles, med_id, pat_id):
|
|
try:
|
|
datat = {
|
|
"detalles": detalles,
|
|
"medico_id": med_id,
|
|
"paciente_id": pat_id
|
|
}
|
|
response = requests.post(CITATION, json=datat, headers=HEADERS)
|
|
self.operationComplete.emit("add_citation", True, "Cita agregada exitosamente")
|
|
self.loadData("citations")
|
|
except Exception as e:
|
|
self.operationComplete.emit("add_citation", False, str(e))
|
|
|
|
@Slot(int, str, int, int)
|
|
def updateCitation(self, cit_id, detalles, med_id, pat_id):
|
|
try:
|
|
datat = {
|
|
"cita_id": cit_id,
|
|
"detalles": detalles,
|
|
"medico_id": med_id,
|
|
"paciente_id": pat_id
|
|
}
|
|
response = requests.put(f"{CITATION}/{cit_id}", json=datat, headers=HEADERS)
|
|
self.operationComplete.emit("update_citation", True, "Cita actualizada exitosamente")
|
|
self.loadData("citations")
|
|
except Exception as e:
|
|
self.operationComplete.emit("update_citation", False, str(e))
|
|
|
|
@Slot(int)
|
|
def deleteCitation(self, cit_id):
|
|
try:
|
|
response = requests.delete(f"{CITATION}/{cit_id}", headers=HEADERS)
|
|
self.operationComplete.emit("delete_citation", True, "Cita eliminada exitosamente")
|
|
self.loadData("citations")
|
|
except Exception as e:
|
|
self.operationComplete.emit("delete_citation", False, str(e))
|
|
|
|
|
|
def main():
|
|
app = QApplication(sys.argv)
|
|
QQuickStyle.setStyle("Material")
|
|
|
|
# Create API manager
|
|
api_manager = ApiManager()
|
|
|
|
# Create QML engine
|
|
engine = QQmlApplicationEngine()
|
|
|
|
# Expose API manager to QML
|
|
engine.rootContext().setContextProperty("apiManager", api_manager)
|
|
|
|
# Load QML file
|
|
qml_file = "main.qml"
|
|
engine.load(qml_file)
|
|
|
|
if not engine.rootObjects():
|
|
sys.exit(-1)
|
|
|
|
sys.exit(app.exec())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|