ajuste de cruds incompleto
This commit is contained in:
Binary file not shown.
BIN
app/modules/clients/__pycache__/repository.cpython-311.pyc
Normal file
BIN
app/modules/clients/__pycache__/repository.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/modules/clients/__pycache__/route.cpython-311.pyc
Normal file
BIN
app/modules/clients/__pycache__/route.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/modules/clients/__pycache__/schema.cpython-311.pyc
Normal file
BIN
app/modules/clients/__pycache__/schema.cpython-311.pyc
Normal file
Binary file not shown.
BIN
app/modules/clients/__pycache__/service.cpython-311.pyc
Normal file
BIN
app/modules/clients/__pycache__/service.cpython-311.pyc
Normal file
Binary file not shown.
@@ -74,3 +74,4 @@ class Client(Base):
|
||||
return f"<Client(id={self.id}, rfc={self.rfc}, razon_social={self.razon_social})>"
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
from app.core.baseRepository import BaseRepository
|
||||
from app.modules.clients.models import Client
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
class ClientRepository(BaseRepository[Client]):
|
||||
def __init__(self, db: Session):
|
||||
super().__init__(db, Client)
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from database import get_db
|
||||
from app.core.auth import get_current_user
|
||||
from app.modules.clients.models import Client
|
||||
from app.modules.clients.repository import ClientRepository
|
||||
from app.modules.clients.schema import ClientCreate, ClientUpdate, ClientResponse
|
||||
from app.modules.clients.service import ClientService
|
||||
from app.pipelines.crud import CreatePipe, ReadPipe, UpdatePipe, SoftDelete, ListPipe
|
||||
from typing import List
|
||||
|
||||
router = APIRouter(prefix="/clients", tags=["clients"])
|
||||
|
||||
@router.post("/", response_model=ClientResponse)
|
||||
async def create_client(
|
||||
client_data: ClientCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
result = await ClientService.create_client(db, client_data, current_user)
|
||||
if result["success"]:
|
||||
return result["data"]
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
|
||||
@router.get("/{client_id}", response_model=ClientResponse)
|
||||
async def get_client(
|
||||
client_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
repo = ClientRepository(db)
|
||||
pipeline = ReadPipe(db, current_user, Client, repo)
|
||||
pipeline.set_input_data({"id": client_id})
|
||||
result = await pipeline.execute()
|
||||
if result["success"]:
|
||||
return result["data"]
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail=result["error"])
|
||||
|
||||
@router.put("/{client_id}", response_model=ClientResponse)
|
||||
async def update_client(
|
||||
client_id: int,
|
||||
client_data: ClientUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
repo = ClientRepository(db)
|
||||
pipeline = UpdatePipe(db, current_user, Client, repo)
|
||||
pipeline.set_input_data({"id": client_id, **client_data.dict(exclude_unset=True)})
|
||||
result = await pipeline.execute()
|
||||
if result["success"]:
|
||||
return result["data"]
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
|
||||
@router.delete("/{client_id}")
|
||||
async def delete_client(
|
||||
client_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
repo = ClientRepository(db)
|
||||
pipeline = SoftDelete(db, current_user, Client, repo)
|
||||
pipeline.set_input_data({"id": client_id})
|
||||
result = await pipeline.execute()
|
||||
if result["success"]:
|
||||
return result["data"]
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
|
||||
@router.get("/", response_model=List[ClientResponse])
|
||||
async def list_clients(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user)
|
||||
):
|
||||
repo = ClientRepository(db)
|
||||
pipeline = ListPipe(db, current_user, Client, repo)
|
||||
pipeline.set_input_data({"skip": skip, "limit": limit})
|
||||
result = await pipeline.execute()
|
||||
if result["success"]:
|
||||
return result["data"]
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=result["error"])
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from typing import Optional
|
||||
from app.modules.clients.models import Medio, Tercero, Operacion
|
||||
|
||||
class ClientBase(BaseModel):
|
||||
rfc: str
|
||||
email: EmailStr
|
||||
short_name: Optional[str] = None
|
||||
razon_social: str
|
||||
fiscal_number: str
|
||||
cellphone: str
|
||||
medio: Medio = Medio.MANUAL
|
||||
third_type: Tercero = Tercero.GLOBAL
|
||||
operation_type: Operacion = Operacion.OTROS
|
||||
is_foreign: bool = False
|
||||
user_id: int
|
||||
location_id: Optional[int] = None
|
||||
|
||||
class ClientCreate(ClientBase):
|
||||
pass
|
||||
|
||||
class ClientUpdate(BaseModel):
|
||||
short_name: Optional[str] = None
|
||||
razon_social: Optional[str] = None
|
||||
fiscal_number: Optional[str] = None
|
||||
cellphone: Optional[str] = None
|
||||
medio: Optional[Medio] = None
|
||||
third_type: Optional[Tercero] = None
|
||||
operation_type: Optional[Operacion] = None
|
||||
is_foreign: Optional[bool] = None
|
||||
location_id: Optional[int] = None
|
||||
|
||||
class ClientResponse(ClientBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
49
app/modules/clients/service.py
Normal file
49
app/modules/clients/service.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from app.modules.clients.schema import ClientCreate
|
||||
from app.modules.clients.models import Client
|
||||
#
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.dependencies import fiscal_number_exists, email_exist
|
||||
|
||||
|
||||
|
||||
|
||||
class ClientService:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
@staticmethod
|
||||
def create_client(db:Session, client_data: ClientCreate) -> Client:
|
||||
|
||||
|
||||
if fiscal_number_exists(db, client_data.get("fiscal_number")):
|
||||
raise ValueError("el numero fiscal ya existe")
|
||||
|
||||
if email_exist(db, client_data.get("email")):
|
||||
raise ValueError("este email ya existe")
|
||||
|
||||
new_client = Client(
|
||||
rfc = client_data.get("rfc"),
|
||||
email = client_data.get("email"),
|
||||
short_name = client_data.get("short_name"),
|
||||
razon_social = client_data.get("razon_social"),
|
||||
fiscal_number = client_data.get("fiscal_number"),
|
||||
cellphone = client_data.get("cellphone"),
|
||||
medio = client_data.get("medio"),
|
||||
third_type = client_data.get("third_type"),
|
||||
operation_type = client_data.get("operation_type"),
|
||||
is_foreign = client_data.get("is_foreign", False),
|
||||
user_id = client_data.get("user_id"),
|
||||
location_id = client_data.get("location_id"),
|
||||
)
|
||||
try:
|
||||
db.add(new_client)
|
||||
db.commit()
|
||||
db.refresh(new_client)
|
||||
|
||||
except IntegrityError as e:
|
||||
db.rollback()
|
||||
raise ValueError(f"Error al crear el cliente: {e}")
|
||||
return new_client
|
||||
|
||||
Reference in New Issue
Block a user