File size: 3,825 Bytes
a7b87f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4424be5
a7b87f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4424be5
a7b87f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4424be5
a7b87f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from library.librerias import *
from models.clientes import Clientes, SearchClientes

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

"""
The code that would fit at $PLACEHOLDER$ is the definition of the router endpoints. Each endpoint corresponds to a specific HTTP method and URL path. The router is responsible for handling requests related to clients. It includes the following endpoints:

GET /clientes: Retrieves all clients from the database.
POST /clientes: Creates a new client in the database.
PUT /clientes: Updates an existing client in the database.
DELETE /clientes/{ID_Proveedor}: Deletes a client from the database based on the provided ID_Proveedor.
GET /clientes/buscar: Searches for clients based on the provided name or contact.
Each endpoint is wrapped in a try-except block to handle any exceptions that may occur during the database operations.
"""


# get/clientes -> obtener todos los clientes con with connection as conn, manejo de errores
@router.get("/")
def get_clientes():
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM clientes")
            clientes = cursor.fetchall()
            return clientes
    except Exception as e:
        print(e)
        return []


# post/clientes -> crear un cliente con with connection as conn, manejo de errores
@router.post("/")
def post_cliente(cliente: Clientes):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "INSERT INTO clientes (Nombre, Contacto, Direccion) VALUES (?, ?, ?)",
                (
                    cliente.Nombre,
                    cliente.Contacto,
                    cliente.Direccion,
                ),
            )
            conn.commit()
            return {"message": "Cliente creado"}
    except Exception as e:
        print(e)
        return []


# put/clientes -> actualizar un cliente con with connection as conn, manejo de errores
@router.put("/")
def put_cliente(cliente: Clientes):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "UPDATE clientes SET Nombre = ?, Contacto = ?, Direccion = ? WHERE ID_Proveedor = ?",
                (
                    cliente.Nombre,
                    cliente.Contacto,
                    cliente.Direccion,
                    cliente.ID_Proveedor,
                ),
            )
            conn.commit()
            return {"message": "Cliente actualizado"}
    except Exception as e:
        print(e)
        return []


# delete/clientes -> eliminar un cliente con with connection as conn, manejo de errores
@router.delete("/{ID_Proveedor}")
def delete_cliente(ID_Proveedor: int):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "DELETE FROM clientes WHERE ID_Proveedor = ?", (ID_Proveedor,)
            )
            conn.commit()
            return {"message": "Cliente eliminado"}
    except Exception as e:
        print(e)
        return []


# metodo de busqueda de clientes por nombre con with connection as conn, manejo de errores
@router.get("/buscar")
def search_clientes(search: SearchClientes):
    try:
        with DatabaseConnection().get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(
                "SELECT * FROM clientes WHERE Nombre LIKE ?",
                ("%" + search.nombre + "%"),
            )
            clientes = cursor.fetchall()
            return clientes
    except Exception as e:
        print(e)
        return []