Spaces:
Sleeping
Sleeping
JairoDanielMT
commited on
Commit
•
5043970
1
Parent(s):
6d6cc91
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,80 @@
|
|
1 |
-
# uvicorn app:app --host localhost --port 7860 --reload
|
2 |
-
from library.librerias import *
|
3 |
-
from routers.routers import *
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
"
|
16 |
-
"
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
app.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# uvicorn app:app --host localhost --port 7860 --reload
|
2 |
+
from library.librerias import *
|
3 |
+
from routers.routers import *
|
4 |
+
from pydantic import BaseModel
|
5 |
+
import sqlite3
|
6 |
+
|
7 |
+
app = FastAPI(
|
8 |
+
title="API de la heladería Anhelados",
|
9 |
+
description="Esta es la API de la microempresa Anhelados, la cual se dedica a la venta de helados y postres. Aquí se pueden realizar operaciones CRUD para los recursos de la empresa.",
|
10 |
+
version="2.8.7",
|
11 |
+
openapi_url="/anhelados-openapi.json",
|
12 |
+
docs_url="/anhelados-docs",
|
13 |
+
redoc_url="/anhelados-redoc",
|
14 |
+
contact={
|
15 |
+
"name": "Anhelados",
|
16 |
+
"url": "https://www.anhelados.com",
|
17 |
+
"email": "contacto@anhelados.com",
|
18 |
+
},
|
19 |
+
license_info={"name": "MIT", "url": "https://opensource.org/licenses/MIT"},
|
20 |
+
)
|
21 |
+
|
22 |
+
# CORS
|
23 |
+
app.add_middleware(
|
24 |
+
CORSMiddleware,
|
25 |
+
allow_origins=["*"],
|
26 |
+
allow_credentials=True,
|
27 |
+
allow_methods=["*"],
|
28 |
+
allow_headers=["*"],
|
29 |
+
)
|
30 |
+
|
31 |
+
|
32 |
+
# saludo
|
33 |
+
@app.get("/", summary="Saludo", description="Saludo de la API")
|
34 |
+
def read_root():
|
35 |
+
return {"message": "Bienvenido a la API de la heladeria"}
|
36 |
+
|
37 |
+
# Modelo para recibir consultas SQL
|
38 |
+
class SQLQuery(BaseModel):
|
39 |
+
query: str
|
40 |
+
|
41 |
+
# Conexión a la base de datos SQLite
|
42 |
+
def get_db_connection():
|
43 |
+
conn = sqlite3.connect('database/anhelados.db')
|
44 |
+
conn.row_factory = sqlite3.Row
|
45 |
+
return conn
|
46 |
+
|
47 |
+
@app.post("/execute", summary="Ejecutar consulta SQL", description="Ejecuta una consulta SQL en la base de datos SQLite")
|
48 |
+
def execute_query(sql_query: SQLQuery):
|
49 |
+
conn = get_db_connection()
|
50 |
+
try:
|
51 |
+
cursor = conn.cursor()
|
52 |
+
cursor.execute(sql_query.query)
|
53 |
+
conn.commit()
|
54 |
+
if cursor.description: # Verifica si la consulta tiene resultados
|
55 |
+
results = cursor.fetchall()
|
56 |
+
return {"results": [dict(row) for row in results]}
|
57 |
+
return {"message": "Consulta ejecutada exitosamente"}
|
58 |
+
except sqlite3.Error as e:
|
59 |
+
raise HTTPException(status_code=400, detail=str(e))
|
60 |
+
finally:
|
61 |
+
conn.close()
|
62 |
+
|
63 |
+
# Routers
|
64 |
+
|
65 |
+
app.router.include_router(almacen.router)
|
66 |
+
app.router.include_router(clientes.router)
|
67 |
+
app.router.include_router(detalle_pedidos.router)
|
68 |
+
app.router.include_router(detalle_ventas.router)
|
69 |
+
app.router.include_router(empleados.router)
|
70 |
+
app.router.include_router(gastos_imprevistos.router)
|
71 |
+
app.router.include_router(gastos.router)
|
72 |
+
app.router.include_router(insumos.router)
|
73 |
+
app.router.include_router(maquinas.router)
|
74 |
+
app.router.include_router(pedidos_proveedor.router)
|
75 |
+
app.router.include_router(produccion.router)
|
76 |
+
app.router.include_router(productos.router)
|
77 |
+
app.router.include_router(registro_energetico.router)
|
78 |
+
app.router.include_router(ventas.router)
|
79 |
+
|
80 |
+
|