ff7ba51e84
Backend: PUT /purchases/{id} endpoint accepts updated amount_eur,
price_eur, and created_at. Frontend: inline edit row in PurchaseList
with date picker and number inputs, Save/Cancel actions.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel
|
|
from typing import List
|
|
from datetime import datetime
|
|
|
|
from ..database import get_db
|
|
from .. import models
|
|
from ..dependencies import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class PurchaseCreate(BaseModel):
|
|
amount_eur: float
|
|
price_eur: float
|
|
|
|
|
|
class PurchaseUpdate(BaseModel):
|
|
amount_eur: float
|
|
price_eur: float
|
|
created_at: datetime
|
|
|
|
|
|
class PurchaseOut(BaseModel):
|
|
id: int
|
|
amount_eur: float
|
|
price_eur: float
|
|
created_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
@router.get("/purchases", response_model=List[PurchaseOut])
|
|
def list_purchases(
|
|
db: Session = Depends(get_db),
|
|
current_user: models.User = Depends(get_current_user),
|
|
):
|
|
return db.query(models.Purchase).filter(models.Purchase.user_id == current_user.id).all()
|
|
|
|
|
|
@router.post("/purchases", response_model=PurchaseOut, status_code=status.HTTP_201_CREATED)
|
|
def add_purchase(
|
|
purchase_in: PurchaseCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: models.User = Depends(get_current_user),
|
|
):
|
|
purchase = models.Purchase(
|
|
amount_eur=purchase_in.amount_eur,
|
|
price_eur=purchase_in.price_eur,
|
|
user_id=current_user.id,
|
|
)
|
|
db.add(purchase)
|
|
db.commit()
|
|
db.refresh(purchase)
|
|
return purchase
|
|
|
|
|
|
@router.put("/purchases/{purchase_id}", response_model=PurchaseOut)
|
|
def update_purchase(
|
|
purchase_id: int,
|
|
purchase_in: PurchaseUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: models.User = Depends(get_current_user),
|
|
):
|
|
purchase = db.query(models.Purchase).filter(
|
|
models.Purchase.id == purchase_id,
|
|
models.Purchase.user_id == current_user.id,
|
|
).first()
|
|
if not purchase:
|
|
raise HTTPException(status_code=404, detail="Purchase not found")
|
|
purchase.amount_eur = purchase_in.amount_eur
|
|
purchase.price_eur = purchase_in.price_eur
|
|
purchase.created_at = purchase_in.created_at
|
|
db.commit()
|
|
db.refresh(purchase)
|
|
return purchase
|
|
|
|
|
|
@router.delete("/purchases/{purchase_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_purchase(
|
|
purchase_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: models.User = Depends(get_current_user),
|
|
):
|
|
purchase = db.query(models.Purchase).filter(
|
|
models.Purchase.id == purchase_id,
|
|
models.Purchase.user_id == current_user.id,
|
|
).first()
|
|
if not purchase:
|
|
raise HTTPException(status_code=404, detail="Purchase not found")
|
|
db.delete(purchase)
|
|
db.commit()
|