File size: 3,414 Bytes
3751757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from library.librerias import *
from models.produccion import Produccion

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

"""CREATE TABLE produccion (
  ID_Produccion INTEGER PRIMARY KEY AUTOINCREMENT,
  ID_Maquina INTEGER,
  Fecha DATE,
  Cantidad_Producida INTEGER,
  FOREIGN KEY (ID_Maquina) REFERENCES maquinas (ID_Maquina)
);
"""


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


# @router.post("/")
def post_produccion(produccion: Produccion):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "INSERT INTO produccion (ID_Maquina, Fecha, Cantidad_Producida) VALUES (?, ?, ?)",
                (
                    produccion.ID_Maquina,
                    produccion.Fecha,
                    produccion.Cantidad_Producida,
                ),
            )
            conn.commit()
            return {"message": "Produccion creada"}
    except Exception as e:
        print(e)
        return []


# @router.put("/")
def put_produccion(produccion: Produccion):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "UPDATE produccion SET ID_Maquina = ?, Fecha = ?, Cantidad_Producida = ? WHERE ID_Produccion = ?",
                (
                    produccion.ID_Maquina,
                    produccion.Fecha,
                    produccion.Cantidad_Producida,
                    produccion.ID_Produccion,
                ),
            )
            conn.commit()
            return {"message": "Produccion actualizada"}
    except Exception as e:
        print(e)
        return []


# @router.delete("/")
def delete_produccion(ID_Produccion: int):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "DELETE FROM produccion WHERE ID_Produccion = ?",
                (ID_Produccion,),
            )
            conn.commit()
            return {"message": "Produccion eliminada"}
    except Exception as e:
        print(e)
        return []


# @router.get("/maquina/{ID_Maquina}")
def get_produccion_maquina(ID_Maquina: int):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "SELECT * FROM produccion WHERE ID_Maquina = ?", (ID_Maquina,)
            )
            produccion = cursor.fetchall()
            return produccion
    except Exception as e:
        print(e)
        return []


@router.get("/fecha/{Fecha}")
def get_produccion_fecha(Fecha: str):
    try:
        with DatabaseConnection().get_connection() as conn:
            Fecha = datetime.strptime(Fecha, "%Y-%m-%d").date()
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM produccion WHERE Fecha = ?", (Fecha,))
            produccion = cursor.fetchall()
            return produccion
    except Exception as e:
        print(e)
        return []