JairoDanielMT commited on
Commit
5043970
1 Parent(s): 6d6cc91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -52
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
- app = FastAPI(
7
- title="API de la heladería Anhelados",
8
- 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.",
9
- version="2.8.7",
10
- openapi_url="/anhelados-openapi.json",
11
- docs_url="/anhelados-docs",
12
- redoc_url="/anhelados-redoc",
13
- contact={
14
- "name": "Anhelados",
15
- "url": "https://www.anhelados.com",
16
- "email": "contacto@anhelados.com",
17
- },
18
- license_info={"name": "MIT", "url": "https://opensource.org/licenses/MIT"},
19
- )
20
-
21
- # CORS
22
- app.add_middleware(
23
- CORSMiddleware,
24
- allow_origins=["*"],
25
- allow_credentials=True,
26
- allow_methods=["*"],
27
- allow_headers=["*"],
28
- )
29
-
30
-
31
- # saludo
32
- @app.get("/", summary="Saludo", description="Saludo de la API")
33
- def read_root():
34
- return {"message": "Bienvenido a la API de la heladeria"}
35
-
36
-
37
- # Routers
38
-
39
- app.router.include_router(almacen.router)
40
- app.router.include_router(clientes.router)
41
- app.router.include_router(detalle_pedidos.router)
42
- app.router.include_router(detalle_ventas.router)
43
- app.router.include_router(empleados.router)
44
- app.router.include_router(gastos_imprevistos.router)
45
- app.router.include_router(gastos.router)
46
- app.router.include_router(insumos.router)
47
- app.router.include_router(maquinas.router)
48
- app.router.include_router(pedidos_proveedor.router)
49
- app.router.include_router(produccion.router)
50
- app.router.include_router(productos.router)
51
- app.router.include_router(registro_energetico.router)
52
- app.router.include_router(ventas.router)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+