
- Complete FastAPI application with JWT authentication
- SQLite database with SQLAlchemy ORM and Alembic migrations
- User registration/login with secure password hashing
- Multi-cryptocurrency wallet system with balance tracking
- Advertisement system for buy/sell listings with fund locking
- Order management with automatic payment integration
- Payment provider API integration with mock fallback
- Automatic crypto release after payment confirmation
- Health monitoring endpoint and CORS configuration
- Comprehensive API documentation with OpenAPI/Swagger
- Database models for users, wallets, ads, orders, and payments
- Complete CRUD operations for all entities
- Security features including fund locking and order expiration
- Detailed README with setup and usage instructions
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
157 lines
5.2 KiB
Python
157 lines
5.2 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import get_current_active_user, get_db
|
|
from app.models.user import User
|
|
from app.models.advertisement import Advertisement, AdType, AdStatus
|
|
from app.models.cryptocurrency import Cryptocurrency
|
|
from app.models.wallet import Wallet
|
|
from app.schemas.advertisement import Advertisement as AdvertisementSchema, AdvertisementCreate, AdvertisementUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[AdvertisementSchema])
|
|
def read_advertisements(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
ad_type: Optional[AdType] = None,
|
|
cryptocurrency_id: Optional[int] = None,
|
|
db: Session = Depends(get_db),
|
|
) -> Any:
|
|
query = db.query(Advertisement).filter(Advertisement.status == AdStatus.ACTIVE)
|
|
|
|
if ad_type:
|
|
query = query.filter(Advertisement.ad_type == ad_type)
|
|
if cryptocurrency_id:
|
|
query = query.filter(Advertisement.cryptocurrency_id == cryptocurrency_id)
|
|
|
|
advertisements = query.offset(skip).limit(limit).all()
|
|
return advertisements
|
|
|
|
|
|
@router.get("/my", response_model=List[AdvertisementSchema])
|
|
def read_my_advertisements(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
advertisements = db.query(Advertisement).filter(
|
|
Advertisement.user_id == current_user.id
|
|
).offset(skip).limit(limit).all()
|
|
return advertisements
|
|
|
|
|
|
@router.post("/", response_model=AdvertisementSchema)
|
|
def create_advertisement(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
advertisement_in: AdvertisementCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
# Verify cryptocurrency exists
|
|
crypto = db.query(Cryptocurrency).filter(
|
|
Cryptocurrency.id == advertisement_in.cryptocurrency_id
|
|
).first()
|
|
if not crypto:
|
|
raise HTTPException(status_code=404, detail="Cryptocurrency not found")
|
|
|
|
# For sell advertisements, check if user has sufficient balance
|
|
if advertisement_in.ad_type == AdType.SELL:
|
|
wallet = db.query(Wallet).filter(
|
|
Wallet.user_id == current_user.id,
|
|
Wallet.cryptocurrency_id == advertisement_in.cryptocurrency_id
|
|
).first()
|
|
|
|
if not wallet or wallet.available_balance < advertisement_in.available_amount:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Insufficient balance for sell advertisement"
|
|
)
|
|
|
|
# Lock the funds for the advertisement
|
|
wallet.available_balance -= advertisement_in.available_amount
|
|
wallet.locked_balance += advertisement_in.available_amount
|
|
db.add(wallet)
|
|
|
|
advertisement = Advertisement(
|
|
user_id=current_user.id,
|
|
**advertisement_in.dict()
|
|
)
|
|
db.add(advertisement)
|
|
db.commit()
|
|
db.refresh(advertisement)
|
|
return advertisement
|
|
|
|
|
|
@router.get("/{advertisement_id}", response_model=AdvertisementSchema)
|
|
def read_advertisement(
|
|
advertisement_id: int,
|
|
db: Session = Depends(get_db),
|
|
) -> Any:
|
|
advertisement = db.query(Advertisement).filter(
|
|
Advertisement.id == advertisement_id
|
|
).first()
|
|
if not advertisement:
|
|
raise HTTPException(status_code=404, detail="Advertisement not found")
|
|
return advertisement
|
|
|
|
|
|
@router.put("/{advertisement_id}", response_model=AdvertisementSchema)
|
|
def update_advertisement(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
advertisement_id: int,
|
|
advertisement_in: AdvertisementUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
advertisement = db.query(Advertisement).filter(
|
|
Advertisement.id == advertisement_id,
|
|
Advertisement.user_id == current_user.id
|
|
).first()
|
|
if not advertisement:
|
|
raise HTTPException(status_code=404, detail="Advertisement not found")
|
|
|
|
update_data = advertisement_in.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(advertisement, field, value)
|
|
|
|
db.add(advertisement)
|
|
db.commit()
|
|
db.refresh(advertisement)
|
|
return advertisement
|
|
|
|
|
|
@router.delete("/{advertisement_id}")
|
|
def delete_advertisement(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
advertisement_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
advertisement = db.query(Advertisement).filter(
|
|
Advertisement.id == advertisement_id,
|
|
Advertisement.user_id == current_user.id
|
|
).first()
|
|
if not advertisement:
|
|
raise HTTPException(status_code=404, detail="Advertisement not found")
|
|
|
|
# If it's a sell ad, unlock the funds
|
|
if advertisement.ad_type == AdType.SELL:
|
|
wallet = db.query(Wallet).filter(
|
|
Wallet.user_id == current_user.id,
|
|
Wallet.cryptocurrency_id == advertisement.cryptocurrency_id
|
|
).first()
|
|
|
|
if wallet:
|
|
wallet.locked_balance -= advertisement.available_amount
|
|
wallet.available_balance += advertisement.available_amount
|
|
db.add(wallet)
|
|
|
|
advertisement.status = AdStatus.CANCELLED
|
|
db.add(advertisement)
|
|
db.commit()
|
|
return {"message": "Advertisement cancelled successfully"} |