File size: 1,760 Bytes
02eec01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c9ba21
02eec01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from library.librerias import *
from models.almacen import Almacen

router = APIRouter(
    prefix="/almacen",
    tags=["Almacen"],
    responses={404: {"description": "No encontrado"}},
)

"""CREATE TABLE almacen (
  ID_Almacen INTEGER PRIMARY KEY AUTOINCREMENT,
  ID_Insumo INTEGER,
  Cantidad_Actual INTEGER,
  Cantidad_Minima INTEGER,
  FOREIGN KEY (ID_Insumo) REFERENCES insumos (ID_Insumo)
);"""


@router.get("/")
def get_almacen():
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM almacen")
            almacen = cursor.fetchall()
            return almacen
    except Exception as e:
        print(e)
        return []


@router.post("/")
def post_almacen(almacen: Almacen):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "INSERT INTO almacen (ID_Insumo, Cantidad_Actual, Cantidad_Minima) VALUES (?, ?, ?)",
                (
                    almacen.ID_Insumo,
                    almacen.Cantidad_Actual,
                    almacen.Cantidad_Minima,
                ),
            )
            conn.commit()
            return {"message": "Almacen creado"}
    except Exception as e:
        print(e)
        return []


# search by id_insumo in almacen table
@router.get("/{ID_Insumo}")
def get_almacen_by_id(ID_Insumo: int):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM almacen WHERE ID_Insumo = ?", (ID_Insumo,))
            almacen = cursor.fetchall()
            return almacen
    except Exception as e:
        print(e)
        return []