JairoDanielMT commited on
Commit
6d6cc91
1 Parent(s): 221c09e

Update routers/productos.py

Browse files
Files changed (1) hide show
  1. routers/productos.py +13 -7
routers/productos.py CHANGED
@@ -1,6 +1,8 @@
1
  from library.librerias import *
2
  from models.productos import Productos
3
- import urllib.parse # Importar para decodificar URL
 
 
4
 
5
  router = APIRouter(
6
  prefix="/productos",
@@ -45,20 +47,24 @@ def post_producto(producto: Productos):
45
  return []
46
 
47
 
 
48
  # search by Nombre order by Precio
49
- @router.get("/search")
50
- def search_productos(nombre: str):
51
  try:
52
  with DatabaseConnection().get_connection() as conn:
53
  cursor = conn.cursor()
54
  nombre_decodificado = urllib.parse.unquote(nombre)
55
  like_pattern = f"%{nombre_decodificado}%"
56
  cursor.execute(
57
- "SELECT Nombre, Precio FROM productos WHERE Nombre LIKE ? ORDER BY Precio",
58
- (like_pattern,)
59
  )
60
  productos = cursor.fetchall()
61
- return productos
 
 
 
62
  except Exception as e:
63
  print(e)
64
- return []
 
1
  from library.librerias import *
2
  from models.productos import Productos
3
+ from fastapi import APIRouter, HTTPException, Query
4
+ import urllib.parse
5
+ from typing import List
6
 
7
  router = APIRouter(
8
  prefix="/productos",
 
47
  return []
48
 
49
 
50
+
51
  # search by Nombre order by Precio
52
+ @router.get("/search", response_model=List[Productos])
53
+ def search_productos(nombre: str = Query(..., alias="Nombre")):
54
  try:
55
  with DatabaseConnection().get_connection() as conn:
56
  cursor = conn.cursor()
57
  nombre_decodificado = urllib.parse.unquote(nombre)
58
  like_pattern = f"%{nombre_decodificado}%"
59
  cursor.execute(
60
+ "SELECT ID_Producto, Nombre, Precio FROM productos WHERE Nombre LIKE ? ORDER BY Precio",
61
+ (like_pattern,),
62
  )
63
  productos = cursor.fetchall()
64
+ return [
65
+ Productos(ID_Producto=row[0], Nombre=row[1], Precio=row[2])
66
+ for row in productos
67
+ ]
68
  except Exception as e:
69
  print(e)
70
+ raise HTTPException(status_code=500, detail="Error al buscar los productos")