commit 4d97545599cd3e95b57fa72cd8c72d668849f24c Author: Daniel Arroyo Date: Sat Apr 11 03:45:33 2026 +0000 API Pedidos - Estructura + Modelos diff --git a/README.md b/README.md new file mode 100644 index 0000000..bdf576d --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# API Pedidos + +API REST para gestionar pedidos con Python FastAPI. + +## Stack + +- **Framework:** FastAPI +- **ORM:** SQLAlchemy +- **Database:** PostgreSQL +- **Migrations:** Alembic +- **Validation:** Pydantic +- **Testing:** pytest + +## Structure + +``` +api-pedidos/ +├── app/ +│ ├── api/ # Endpoints +│ ├── models/ # SQLAlchemy models +│ ├── schemas/ # Pydantic schemas +│ ├── repositories/ # Data access layer +│ └── core/ # Configuración +├── tests/ # Tests unitarios +├── alembic/ # Migraciones +└── Dockerfile +``` + +## Running + +```bash +pip install -r requirements.txt +uvicorn app.main:app --reload +``` + +## API Endpoints + +- `GET /pedidos` - Listar pedidos +- `POST /pedidos` - Crear pedido +- `GET /pedidos/{id}` - Obtener pedido +- `PUT /pedidos/{id}` - Actualizar pedido +- `DELETE /pedidos/{id}` - Eliminar pedido diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/__init__.py b/app/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/core/__init__.py b/app/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 0000000..65a3a53 --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,11 @@ +from pydantic_settings import BaseSettings +from functools import lru_cache + + +@lru_cache() +def get_settings(): + return BaseSettings( + app_name="API Pedidos", + database_url="postgresql://user:password@localhost:5432/pedidos", + debug=True + ) diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 0000000..5c16a05 --- /dev/null +++ b/app/models/__init__.py @@ -0,0 +1,4 @@ +# Task Models +from .order import Order, OrderItem, Base + +__all__ = ["Order", "OrderItem", "Base"] diff --git a/app/models/order.py b/app/models/order.py new file mode 100644 index 0000000..26969dd --- /dev/null +++ b/app/models/order.py @@ -0,0 +1,33 @@ +from sqlalchemy import Column, String, Float, Integer, DateTime, ForeignKey +from sqlalchemy.orm import relationship +from sqlalchemy.ext.declarative import declarative_base +from datetime import datetime + +Base = declarative_base() + + +class Order(Base): + __tablename__ = "orders" + + id = Column(Integer, primary_key=True, index=True) + customer_name = Column(String, nullable=False) + customer_email = Column(String, nullable=False) + status = Column(String, default="pending") + total = Column(Float, default=0.0) + created_at = Column(DateTime, default=datetime.now) + updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) + + items = relationship("OrderItem", back_populates="order", cascade="all, delete-orphan") + + +class OrderItem(Base): + __tablename__ = "order_items" + + id = Column(Integer, primary_key=True, index=True) + order_id = Column(Integer, ForeignKey("orders.id"), nullable=False) + product_name = Column(String, nullable=False) + quantity = Column(Integer, default=1) + price = Column(Float, nullable=False) + created_at = Column(DateTime, default=datetime.now) + + order = relationship("Order", back_populates="items") diff --git a/app/repositories/__init__.py b/app/repositories/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e9bfcf5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +fastapi==0.109.2 +uvicorn[standard]==0.27.1 +sqlalchemy==2.0.25 +alembic==1.13.1 +psycopg2-binary==2.9.9 +pydantic==2.6.1 +pydantic-settings==2.1.0 +pytest==8.0.0 +pytest-asyncio==0.23.3 +httpx==0.26.0