<reponame>fecabrera/crowd-api<filename>app/routes/current_user/donations.py
from bson.objectid import ObjectId
from fastapi import APIRouter, Depends, HTTPException, status
from mongomapper.errors import DocumentNotFoundError

from app.models import User, Donation
from app.dependencies import get_current_user

router = APIRouter(prefix='/donations')

@router.get('/')
def list_donations(current_user: User = Depends(get_current_user)):
  """Lists donations made by the current user"""
  donations = Donation.all(user=current_user.reference)
  return [donation.json() for donation in donations]

@router.get('/{donation_id}')
def get_donation(donation_id: str, current_user: User = Depends(get_current_user)):
  """Get a donation made by the current user"""
  try:
    donation = Donation.get(donation_id, user=current_user.reference)
  except DocumentNotFoundError:
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
  
  return donation.json()
from bson.objectid import ObjectId
from fastapi import APIRouter, Depends, HTTPException, status
from mongomapper.errors import DocumentNotFoundError

from app.models import User, Donation
from app.dependencies import get_current_user

router = APIRouter(prefix='/donations')

@router.get('/')
def list_donations(current_user: User = Depends(get_current_user)):
  """Lists donations made by the current user"""
  donations = Donation.all(user=current_user.reference)
  return [donation.json() for donation in donations]

@router.get('/{donation_id}')
def get_donation(donation_id: str, current_user: User = Depends(get_current_user)):
  """Get a donation made by the current user"""
  try:
    donation = Donation.get(donation_id, user=current_user.reference)
  except DocumentNotFoundError:
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
  
  return donation.json()
Lists donations made by the current user Get a donation made by the current user