JairoDanielMT commited on
Commit
a7b87f2
1 Parent(s): 02eec01

Update routers/clientes.py

Browse files
Files changed (1) hide show
  1. routers/clientes.py +109 -109
routers/clientes.py CHANGED
@@ -1,109 +1,109 @@
1
- from library.librerias import *
2
- from models.clientes import Clientes, SearchClientes
3
-
4
- router = APIRouter(
5
- prefix="/clientes",
6
- tags=["Clientes"],
7
- responses={404: {"description": "No encontrado"}},
8
- )
9
-
10
- """
11
- 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:
12
-
13
- GET /clientes: Retrieves all clients from the database.
14
- POST /clientes: Creates a new client in the database.
15
- PUT /clientes: Updates an existing client in the database.
16
- DELETE /clientes/{ID_Proveedor}: Deletes a client from the database based on the provided ID_Proveedor.
17
- GET /clientes/buscar: Searches for clients based on the provided name or contact.
18
- Each endpoint is wrapped in a try-except block to handle any exceptions that may occur during the database operations.
19
- """
20
-
21
-
22
- # get/clientes -> obtener todos los clientes con with connection as conn, manejo de errores
23
- @router.get("/")
24
- def get_clientes():
25
- try:
26
- with DatabaseConnection().get_connection() as conn:
27
- cursor = conn.cursor()
28
- cursor.execute("SELECT * FROM clientes")
29
- clientes = cursor.fetchall()
30
- return clientes
31
- except Exception as e:
32
- print(e)
33
- return []
34
-
35
-
36
- # post/clientes -> crear un cliente con with connection as conn, manejo de errores
37
- @router.post("/")
38
- def post_cliente(cliente: Clientes):
39
- try:
40
- with DatabaseConnection().get_connection() as conn:
41
- cursor = conn.cursor()
42
- cursor.execute(
43
- "INSERT INTO clientes (Nombre, Contacto, Direccion) VALUES (?, ?, ?)",
44
- (
45
- cliente.Nombre,
46
- cliente.Contacto,
47
- cliente.Direccion,
48
- ),
49
- )
50
- conn.commit()
51
- return {"message": "Cliente creado"}
52
- except Exception as e:
53
- print(e)
54
- return []
55
-
56
-
57
- # put/clientes -> actualizar un cliente con with connection as conn, manejo de errores
58
- @router.put("/")
59
- def put_cliente(cliente: Clientes):
60
- try:
61
- with DatabaseConnection().get_connection() as conn:
62
- cursor = conn.cursor()
63
- cursor.execute(
64
- "UPDATE clientes SET Nombre = ?, Contacto = ?, Direccion = ? WHERE ID_Proveedor = ?",
65
- (
66
- cliente.Nombre,
67
- cliente.Contacto,
68
- cliente.Direccion,
69
- cliente.ID_Proveedor,
70
- ),
71
- )
72
- conn.commit()
73
- return {"message": "Cliente actualizado"}
74
- except Exception as e:
75
- print(e)
76
- return []
77
-
78
-
79
- # delete/clientes -> eliminar un cliente con with connection as conn, manejo de errores
80
- @router.delete("/{ID_Proveedor}")
81
- def delete_cliente(ID_Proveedor: int):
82
- try:
83
- with DatabaseConnection().get_connection() as conn:
84
- cursor = conn.cursor()
85
- cursor.execute(
86
- "DELETE FROM clientes WHERE ID_Proveedor = ?", (ID_Proveedor,)
87
- )
88
- conn.commit()
89
- return {"message": "Cliente eliminado"}
90
- except Exception as e:
91
- print(e)
92
- return []
93
-
94
-
95
- # metodo de busqueda de clientes por nombre con with connection as conn, manejo de errores
96
- @router.get("/buscar")
97
- def search_clientes(search: SearchClientes):
98
- try:
99
- with DatabaseConnection().get_connection() as conn:
100
- cursor = conn.cursor()
101
- cursor.execute(
102
- "SELECT * FROM clientes WHERE Nombre LIKE ?",
103
- ("%" + search.nombre + "%"),
104
- )
105
- clientes = cursor.fetchall()
106
- return clientes
107
- except Exception as e:
108
- print(e)
109
- return []
 
1
+ from library.librerias import *
2
+ from models.clientes import Clientes, SearchClientes
3
+
4
+ router = APIRouter(
5
+ prefix="/clientes",
6
+ tags=["Clientes"],
7
+ responses={404: {"description": "No encontrado"}},
8
+ )
9
+
10
+ """
11
+ 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:
12
+
13
+ GET /clientes: Retrieves all clients from the database.
14
+ POST /clientes: Creates a new client in the database.
15
+ PUT /clientes: Updates an existing client in the database.
16
+ DELETE /clientes/{ID_Proveedor}: Deletes a client from the database based on the provided ID_Proveedor.
17
+ GET /clientes/buscar: Searches for clients based on the provided name or contact.
18
+ Each endpoint is wrapped in a try-except block to handle any exceptions that may occur during the database operations.
19
+ """
20
+
21
+
22
+ # get/clientes -> obtener todos los clientes con with connection as conn, manejo de errores
23
+ @router.get("/")
24
+ def get_clientes():
25
+ try:
26
+ with DatabaseConnection().get_connection() as conn:
27
+ cursor = conn.cursor()
28
+ cursor.execute("SELECT * FROM clientes")
29
+ clientes = cursor.fetchall()
30
+ return clientes
31
+ except Exception as e:
32
+ print(e)
33
+ return []
34
+
35
+
36
+ # post/clientes -> crear un cliente con with connection as conn, manejo de errores
37
+ # @router.post("/")
38
+ def post_cliente(cliente: Clientes):
39
+ try:
40
+ with DatabaseConnection().get_connection() as conn:
41
+ cursor = conn.cursor()
42
+ cursor.execute(
43
+ "INSERT INTO clientes (Nombre, Contacto, Direccion) VALUES (?, ?, ?)",
44
+ (
45
+ cliente.Nombre,
46
+ cliente.Contacto,
47
+ cliente.Direccion,
48
+ ),
49
+ )
50
+ conn.commit()
51
+ return {"message": "Cliente creado"}
52
+ except Exception as e:
53
+ print(e)
54
+ return []
55
+
56
+
57
+ # put/clientes -> actualizar un cliente con with connection as conn, manejo de errores
58
+ # @router.put("/")
59
+ def put_cliente(cliente: Clientes):
60
+ try:
61
+ with DatabaseConnection().get_connection() as conn:
62
+ cursor = conn.cursor()
63
+ cursor.execute(
64
+ "UPDATE clientes SET Nombre = ?, Contacto = ?, Direccion = ? WHERE ID_Proveedor = ?",
65
+ (
66
+ cliente.Nombre,
67
+ cliente.Contacto,
68
+ cliente.Direccion,
69
+ cliente.ID_Proveedor,
70
+ ),
71
+ )
72
+ conn.commit()
73
+ return {"message": "Cliente actualizado"}
74
+ except Exception as e:
75
+ print(e)
76
+ return []
77
+
78
+
79
+ # delete/clientes -> eliminar un cliente con with connection as conn, manejo de errores
80
+ #@router.delete("/{ID_Proveedor}")
81
+ def delete_cliente(ID_Proveedor: int):
82
+ try:
83
+ with DatabaseConnection().get_connection() as conn:
84
+ cursor = conn.cursor()
85
+ cursor.execute(
86
+ "DELETE FROM clientes WHERE ID_Proveedor = ?", (ID_Proveedor,)
87
+ )
88
+ conn.commit()
89
+ return {"message": "Cliente eliminado"}
90
+ except Exception as e:
91
+ print(e)
92
+ return []
93
+
94
+
95
+ # metodo de busqueda de clientes por nombre con with connection as conn, manejo de errores
96
+ @router.get("/buscar")
97
+ def search_clientes(search: SearchClientes):
98
+ try:
99
+ with DatabaseConnection().get_connection() as conn:
100
+ cursor = conn.cursor()
101
+ cursor.execute(
102
+ "SELECT * FROM clientes WHERE Nombre LIKE ?",
103
+ ("%" + search.nombre + "%"),
104
+ )
105
+ clientes = cursor.fetchall()
106
+ return clientes
107
+ except Exception as e:
108
+ print(e)
109
+ return []