API Pedidos - Estructura + Modelos
This commit is contained in:
42
README.md
Normal file
42
README.md
Normal file
@@ -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
|
||||
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
0
app/api/__init__.py
Normal file
0
app/api/__init__.py
Normal file
0
app/core/__init__.py
Normal file
0
app/core/__init__.py
Normal file
11
app/core/config.py
Normal file
11
app/core/config.py
Normal file
@@ -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
|
||||
)
|
||||
4
app/models/__init__.py
Normal file
4
app/models/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
# Task Models
|
||||
from .order import Order, OrderItem, Base
|
||||
|
||||
__all__ = ["Order", "OrderItem", "Base"]
|
||||
33
app/models/order.py
Normal file
33
app/models/order.py
Normal file
@@ -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")
|
||||
0
app/repositories/__init__.py
Normal file
0
app/repositories/__init__.py
Normal file
0
app/schemas/__init__.py
Normal file
0
app/schemas/__init__.py
Normal file
10
requirements.txt
Normal file
10
requirements.txt
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user