Spaces:
Sleeping
Sleeping
File size: 2,431 Bytes
217ab42 33223a2 217ab42 33223a2 217ab42 |
1 2 3 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
from library.librerias import *
from models.insumos import Insumos
router = APIRouter(
prefix="/insumos",
tags=["Insumos"],
responses={404: {"description": "No encontrado"}},
)
"""CREATE TABLE insumos (
ID_Insumo INTEGER PRIMARY KEY AUTOINCREMENT,
Descripcion TEXT,
Tipo TEXT,
Costo REAL,
Cantidad INTEGER
);"""
@router.get("/")
def get_insumos():
try:
with DatabaseConnection().get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM insumos")
insumos = cursor.fetchall()
return insumos
except Exception as e:
print(e)
return []
@router.post("/")
def post_insumo(insumo: Insumos):
try:
with DatabaseConnection().get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"INSERT INTO insumos (Descripcion, Tipo, Costo, Cantidad) VALUES (?, ?, ?, ?)",
(
insumo.Descripcion,
insumo.Tipo,
insumo.Costo,
insumo.Cantidad,
),
)
conn.commit()
return {"message": "Insumo creado"}
except Exception as e:
print(e)
return []
@router.put("/")
def put_insumo(insumo: Insumos):
try:
with DatabaseConnection().get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"UPDATE insumos SET Descripcion = ?, Tipo = ?, Costo = ?, Cantidad = ? WHERE ID_Insumo = ?",
(
insumo.Descripcion,
insumo.Tipo,
insumo.Costo,
insumo.Cantidad,
insumo.ID_Insumo,
),
)
conn.commit()
return {"message": "Insumo actualizado"}
except Exception as e:
print(e)
return []
# search by descripcion in insumos table with like
@router.get("/{Descripcion}")
def get_insumo_by_descripcion(Descripcion: str):
try:
with DatabaseConnection().get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM insumos WHERE Descripcion LIKE ?", (f"%{Descripcion}%",)
)
insumo = cursor.fetchall()
return insumo
except Exception as e:
print(e)
return []
|