Spaces:
Sleeping
Sleeping
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 | |
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 | |
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 | |
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 | |
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 | |
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 [] | |