feat: crud and backups are working
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from app import models, schemas
|
||||
|
||||
|
||||
def get_item(db: Session, item_id: int):
|
||||
return db.query(models.Item).filter(models.Item.id == item_id).first()
|
||||
|
||||
|
||||
def get_items(db: Session, skip: int = 0, limit: int = 10):
|
||||
return db.query(models.Item).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
def create_item(db: Session, item: schemas.ItemCreate):
|
||||
db_item = models.Item(**item.model_dump())
|
||||
db.add(db_item)
|
||||
db.commit()
|
||||
db.refresh(db_item)
|
||||
return db_item
|
||||
|
||||
|
||||
def delete_item(db: Session, item_id: int):
|
||||
item = db.query(models.Item).filter(models.Item.id == item_id).first()
|
||||
if item:
|
||||
db.delete(item)
|
||||
db.commit()
|
||||
return item
|
||||
@@ -0,0 +1,11 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
import os
|
||||
|
||||
DATABASE_URL = os.getenv(
|
||||
"DATABASE_URL", "postgresql://user:password@db:5432/mydatabase"
|
||||
)
|
||||
|
||||
engine = create_engine(DATABASE_URL)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
+44
-4
@@ -1,8 +1,48 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from . import schemas, crud
|
||||
from .database import SessionLocal, engine, Base
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def read_root():
|
||||
return {"hello": "world"}
|
||||
# Dependency
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@app.get("/check-health")
|
||||
def health_check():
|
||||
return {"Health": "Super Healthy!"}
|
||||
|
||||
|
||||
@app.post("/items/", response_model=schemas.Item)
|
||||
def create_item(item: schemas.ItemCreate, db: Session = Depends(get_db)):
|
||||
return crud.create_item(db, item)
|
||||
|
||||
|
||||
@app.get("/items/", response_model=list[schemas.Item])
|
||||
def read_items(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
|
||||
return crud.get_items(db, skip, limit)
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=schemas.Item)
|
||||
def read_item(item_id: int, db: Session = Depends(get_db)):
|
||||
db_item = crud.get_item(db, item_id)
|
||||
if db_item is None:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return db_item
|
||||
|
||||
|
||||
@app.delete("/items/{item_id}", response_model=schemas.Item)
|
||||
def delete_item(item_id: int, db: Session = Depends(get_db)):
|
||||
item = crud.delete_item(db, item_id)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return item
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from sqlalchemy import JSON, Column, Integer, String
|
||||
from .database import Base
|
||||
|
||||
|
||||
class Item(Base):
|
||||
__tablename__ = "items"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, index=True)
|
||||
description = Column(String, nullable=True)
|
||||
body = Column(JSON, nullable=False)
|
||||
@@ -0,0 +1,17 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ItemBase(BaseModel):
|
||||
name: str
|
||||
description: str | None = None
|
||||
|
||||
|
||||
class ItemCreate(ItemBase):
|
||||
pass
|
||||
|
||||
|
||||
class Item(ItemBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
Reference in New Issue
Block a user