From b9ff5671f70ce14d4d9fdd2750a1b4b64c024948 Mon Sep 17 00:00:00 2001 From: u634434 Date: Tue, 29 Apr 2025 17:55:59 -0300 Subject: [PATCH 1/5] feat: update of main and add clients --- Backend/FastAPI/clients.py | 28 ++++++++++++++++++++++++++++ Backend/FastAPI/main.py | 16 ++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 Backend/FastAPI/clients.py diff --git a/Backend/FastAPI/clients.py b/Backend/FastAPI/clients.py new file mode 100644 index 00000000..f161ba17 --- /dev/null +++ b/Backend/FastAPI/clients.py @@ -0,0 +1,28 @@ +from fastapi import FastAPI +from pydantic import BaseModel + +app = FastAPI() +# Inicia el server: uvicorn users:app --reload + +# Entidad client +class Client(BaseModel): + name: str + lastname: str + age: int + location: str + +clients_list = [Client(name="Gianni", lastname="Tibaldi", age=35, location="General Deheza"), + Client(name="Joaquín", lastname="Fernandez", age=65, location="Rio Cuarto"), + Client(name="Fernando", lastname="Gomez", age=44, location="Baigorria"), + Client(name="Roberto", lastname="Ramirez", age=39, location="Alcira Gigena")] + +@app.get("/clientsjson") +async def clientsjson(): + return [{"name": "Gianni", "lastname": "Tibaldi", "age": "35","location": "General Deheza"}, + {"name": "Joaquín", "lastname": "Fernandez", "age": "65", "location": "Rio Cuarto"}, + {"name": "Fernando", "lastname": "Gomez", "age": "44","location": "Baigorria"}, + {"name": "Roberto", "lastname": "Ramirez", "age": "39","location": "Alcira Gigena"}] + +@app.get("/clients") +async def clients(): + return clients_list \ No newline at end of file diff --git a/Backend/FastAPI/main.py b/Backend/FastAPI/main.py index 09d72fa5..395fffb8 100644 --- a/Backend/FastAPI/main.py +++ b/Backend/FastAPI/main.py @@ -7,26 +7,26 @@ # Instala FastAPI: pip install "fastapi[all]" from fastapi import FastAPI -from routers import products, users, basic_auth_users, jwt_auth_users, users_db -from fastapi.staticfiles import StaticFiles +#from routers import products, users, basic_auth_users, jwt_auth_users, users_db +#from fastapi.staticfiles import StaticFiles app = FastAPI() # Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=12475 -app.include_router(products.router) -app.include_router(users.router) +#app.include_router(products.router) +#app.include_router(users.router) # Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=14094 -app.include_router(basic_auth_users.router) +#app.include_router(basic_auth_users.router) # Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=17664 -app.include_router(jwt_auth_users.router) +#app.include_router(jwt_auth_users.router) # Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=20480 -app.include_router(users_db.router) +#app.include_router(users_db.router) # Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=13618 -app.mount("/static", StaticFiles(directory="static"), name="static") +#app.mount("/static", StaticFiles(directory="static"), name="static") # Url local: http://127.0.0.1:8000 From 7c5122a39d102e12673d5d3e8ed2baf44aa30c3d Mon Sep 17 00:00:00 2001 From: u634434 Date: Wed, 30 Apr 2025 18:01:31 -0300 Subject: [PATCH 2/5] feat: Agregada de path and query --- Backend/FastAPI/clients.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/Backend/FastAPI/clients.py b/Backend/FastAPI/clients.py index f161ba17..1bbf166b 100644 --- a/Backend/FastAPI/clients.py +++ b/Backend/FastAPI/clients.py @@ -6,15 +6,16 @@ # Entidad client class Client(BaseModel): + id: int name: str lastname: str age: int location: str -clients_list = [Client(name="Gianni", lastname="Tibaldi", age=35, location="General Deheza"), - Client(name="Joaquín", lastname="Fernandez", age=65, location="Rio Cuarto"), - Client(name="Fernando", lastname="Gomez", age=44, location="Baigorria"), - Client(name="Roberto", lastname="Ramirez", age=39, location="Alcira Gigena")] +clients_list = [Client(id=1,name="Gianni", lastname="Tibaldi", age=35, location="General Deheza"), + Client(id=2,name="Joaquín", lastname="Fernandez", age=65, location="Rio Cuarto"), + Client(id=3,name="Fernando", lastname="Gomez", age=44, location="Baigorria"), + Client(id=4,name="Roberto", lastname="Ramirez", age=39, location="Alcira Gigena")] @app.get("/clientsjson") async def clientsjson(): @@ -23,6 +24,20 @@ async def clientsjson(): {"name": "Fernando", "lastname": "Gomez", "age": "44","location": "Baigorria"}, {"name": "Roberto", "lastname": "Ramirez", "age": "39","location": "Alcira Gigena"}] -@app.get("/clients") -async def clients(): - return clients_list \ No newline at end of file +#Path +@app.get("/client/{id}") +async def client(id: int): + return search_client(id) + +#Query +@app.get("/clientquery/") +async def client(id: int): + return search_client(id) + + +def search_client(id: int): + clients = filter(lambda client: client.id == id, clients_list) + try: + return list(clients)[0] + except: + return {"error": "Client not found"} \ No newline at end of file From 947ec7b887257785d7df1ff8511b21af41949926 Mon Sep 17 00:00:00 2001 From: u634434 Date: Thu, 1 May 2025 11:40:19 -0300 Subject: [PATCH 3/5] feat: Agregado de raise HTTP Code --- Backend/FastAPI/clients.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Backend/FastAPI/clients.py b/Backend/FastAPI/clients.py index 1bbf166b..d2b8e64d 100644 --- a/Backend/FastAPI/clients.py +++ b/Backend/FastAPI/clients.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException from pydantic import BaseModel app = FastAPI() @@ -40,4 +40,4 @@ def search_client(id: int): try: return list(clients)[0] except: - return {"error": "Client not found"} \ No newline at end of file + raise HTTPException(status_code=404, detail="No hay un cliente actualmente") \ No newline at end of file From d5ce304db0d807744b91d53e8c204a33f5439f33 Mon Sep 17 00:00:00 2001 From: u634434 Date: Mon, 5 May 2025 18:06:44 -0300 Subject: [PATCH 4/5] feat: Se agrego metodo post --- Backend/FastAPI/clients.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Backend/FastAPI/clients.py b/Backend/FastAPI/clients.py index d2b8e64d..0c2d29a2 100644 --- a/Backend/FastAPI/clients.py +++ b/Backend/FastAPI/clients.py @@ -12,10 +12,10 @@ class Client(BaseModel): age: int location: str -clients_list = [Client(id=1,name="Gianni", lastname="Tibaldi", age=35, location="General Deheza"), - Client(id=2,name="Joaquín", lastname="Fernandez", age=65, location="Rio Cuarto"), - Client(id=3,name="Fernando", lastname="Gomez", age=44, location="Baigorria"), - Client(id=4,name="Roberto", lastname="Ramirez", age=39, location="Alcira Gigena")] +clients_list = [Client(id=1, name="Gianni", lastname="Tibaldi", age=35, location="General Deheza"), + Client(id=2, name="Joaquín", lastname="Fernandez", age=65, location="Rio Cuarto"), + Client(id=3, name="Fernando", lastname="Gomez", age=44, location="Baigorria"), + Client(id=4, name="Roberto", lastname="Ramirez", age=39, location="Alcira Gigena")] @app.get("/clientsjson") async def clientsjson(): @@ -30,14 +30,30 @@ async def client(id: int): return search_client(id) #Query -@app.get("/clientquery/") +@app.get("/client/") async def client(id: int): return search_client(id) +#Operacion POST +@app.post("/client/") +async def create_client(client: Client): + if search_client_silent(client.id): + raise HTTPException(status_code=400, detail="Ya existe un cliente con ese ID") + clients_list.append(client) + return client + + + + def search_client(id: int): clients = filter(lambda client: client.id == id, clients_list) try: return list(clients)[0] except: - raise HTTPException(status_code=404, detail="No hay un cliente actualmente") \ No newline at end of file + raise HTTPException(status_code=404, detail="No hay un cliente actualmente") + + +def search_client_silent(id: int): + clients = list(filter(lambda client: client.id == id, clients_list)) + return clients[0] if clients else None \ No newline at end of file From e6672a7306c849f436269eac087f6ca1441f0754 Mon Sep 17 00:00:00 2001 From: u634434 Date: Sat, 10 May 2025 12:52:42 -0300 Subject: [PATCH 5/5] feat: Agregado de routers --- Backend/FastAPI/main.py | 7 +++- Backend/FastAPI/{ => routers}/clients.py | 43 ++++++++++++++++------ Backend/FastAPI/routers/products.py | 47 ++++++++++++++++++------ 3 files changed, 74 insertions(+), 23 deletions(-) rename Backend/FastAPI/{ => routers}/clients.py (59%) diff --git a/Backend/FastAPI/main.py b/Backend/FastAPI/main.py index 395fffb8..ff3607c8 100644 --- a/Backend/FastAPI/main.py +++ b/Backend/FastAPI/main.py @@ -7,11 +7,16 @@ # Instala FastAPI: pip install "fastapi[all]" from fastapi import FastAPI -#from routers import products, users, basic_auth_users, jwt_auth_users, users_db +from routers import products, clients #from fastapi.staticfiles import StaticFiles app = FastAPI() +#Routers +app.include_router(products.router) +app.include_router(clients.router) + + # Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=12475 #app.include_router(products.router) #app.include_router(users.router) diff --git a/Backend/FastAPI/clients.py b/Backend/FastAPI/routers/clients.py similarity index 59% rename from Backend/FastAPI/clients.py rename to Backend/FastAPI/routers/clients.py index 0c2d29a2..fc05f690 100644 --- a/Backend/FastAPI/clients.py +++ b/Backend/FastAPI/routers/clients.py @@ -1,8 +1,10 @@ -from fastapi import FastAPI, HTTPException +from fastapi import APIRouter, HTTPException from pydantic import BaseModel -app = FastAPI() -# Inicia el server: uvicorn users:app --reload +router = APIRouter(prefix="/clients", + responses={404: {"description": "Not found"}}, + tags=["clients"]) + # Entidad client class Client(BaseModel): @@ -17,35 +19,54 @@ class Client(BaseModel): Client(id=3, name="Fernando", lastname="Gomez", age=44, location="Baigorria"), Client(id=4, name="Roberto", lastname="Ramirez", age=39, location="Alcira Gigena")] -@app.get("/clientsjson") +@router.get("/clientsjson") async def clientsjson(): return [{"name": "Gianni", "lastname": "Tibaldi", "age": "35","location": "General Deheza"}, {"name": "Joaquín", "lastname": "Fernandez", "age": "65", "location": "Rio Cuarto"}, {"name": "Fernando", "lastname": "Gomez", "age": "44","location": "Baigorria"}, {"name": "Roberto", "lastname": "Ramirez", "age": "39","location": "Alcira Gigena"}] -#Path -@app.get("/client/{id}") -async def client(id: int): - return search_client(id) -#Query -@app.get("/client/") +@router.get("/clients") +async def clients(): + return clients_list + + +@router.get("/client/") async def client(id: int): return search_client(id) #Operacion POST -@app.post("/client/") +@router.post("/client/", response_model=Client, status_code=201) async def create_client(client: Client): if search_client_silent(client.id): raise HTTPException(status_code=400, detail="Ya existe un cliente con ese ID") clients_list.append(client) return client +#Operacion PUT +@router.put("/client/") +async def update_client(client: Client): + client_found = search_client_silent(client.id) + if not client_found: + raise HTTPException(status_code=404, detail="No existe un cliente con ese ID") + clients_list.remove(client_found) + clients_list.append(client) + return client +#Operacion DELETE +@router.delete("/client/{id}") +async def delete_client(id: int): + client_found = search_client_silent(id) + if not client_found: + raise HTTPException(status_code=404, detail="No existe un cliente con ese ID") + clients_list.remove(client_found) + raise HTTPException(status_code=200, detail="Se ha eliminado el cliente") + return client_found +#Funciones def search_client(id: int): clients = filter(lambda client: client.id == id, clients_list) try: diff --git a/Backend/FastAPI/routers/products.py b/Backend/FastAPI/routers/products.py index 6d85b336..4d8bec53 100644 --- a/Backend/FastAPI/routers/products.py +++ b/Backend/FastAPI/routers/products.py @@ -1,22 +1,47 @@ -# Clase en vídeo: https://youtu.be/_y9qQZXE24A?t=12475 +from fastapi import APIRouter +from pydantic import BaseModel -### Products API ### +router = APIRouter(prefix="/products", + responses={404: {"description": "Not found"}}, + tags=["products"]) + +class Client(BaseModel): + id: int + name: str + price: int -from fastapi import APIRouter -router = APIRouter(prefix="/products", - tags=["products"], - responses={404: {"message": "No encontrado"}}) +products_list = [ + Client(id=1, name="Galletitas", price=100), + Client(id=2, name="Galletas", price=200), + Client(id=3, name="Galletitas de agua", price=300), + Client(id=4, name="Galletas de chocolate", price=400), + Client(id=5, name="Galletas de vainilla", price=500),] -products_list = ["Producto 1", "Producto 2", - "Producto 3", "Producto 4", "Producto 5"] +@router.get("/{id}") +async def products(id: int): + return products_list[id] @router.get("/") async def products(): return products_list -@router.get("/{id}") -async def products(id: int): - return products_list[id] +@router.get("/product/") +async def product(id: int): + return search_product(id) + + +#Funciones +def search_product(id: int): + products = filter(lambda product: product.id == id, products_list) + try: + return list(products)[0] + except: + raise HTTPException(status_code=404, detail="No existe este producto actualmente") + + +def search_product_silent(id: int): + products = list(filter(lambda product: product.id == id, products_list)) + return products[0] if products else None