refactor: restructure project into layered architecture
This commit is contained in:
0
backend/app/db/__init__.py
Normal file
0
backend/app/db/__init__.py
Normal file
BIN
backend/app/db/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
backend/app/db/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/app/db/__pycache__/model.cpython-313.pyc
Normal file
BIN
backend/app/db/__pycache__/model.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/app/db/__pycache__/models.cpython-313.pyc
Normal file
BIN
backend/app/db/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/app/db/__pycache__/session.cpython-313.pyc
Normal file
BIN
backend/app/db/__pycache__/session.cpython-313.pyc
Normal file
Binary file not shown.
@@ -1,87 +0,0 @@
|
||||
from fastapi import FastAPI, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
import models
|
||||
import schemas
|
||||
import crud
|
||||
|
||||
from database import SessionLocal, engine
|
||||
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Database Session
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# GET ITEMS
|
||||
@app.get("/items", response_model=list[schemas.ItemResponse])
|
||||
def get_items(db: Session = Depends(get_db)):
|
||||
return crud.get_items(db)
|
||||
|
||||
# CREATE ITEM
|
||||
@app.post("/items", response_model=schemas.ItemResponse)
|
||||
def create_item(
|
||||
item: schemas.ItemCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
return crud.create_item(db, item.title)
|
||||
|
||||
# DELETE ITEM
|
||||
@app.delete("/items/{item_id}")
|
||||
def delete_item(
|
||||
item_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
item = crud.delete_item(db, item_id)
|
||||
|
||||
if not item:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Item not found"
|
||||
)
|
||||
|
||||
return {"message": "Item deleted"}
|
||||
|
||||
# SPIN
|
||||
@app.post("/spin")
|
||||
def spin(db: Session = Depends(get_db)):
|
||||
|
||||
winner = crud.spin_wheel(db)
|
||||
|
||||
if not winner:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="No items available"
|
||||
)
|
||||
|
||||
return {
|
||||
"winner": {
|
||||
"id": winner.id,
|
||||
"title": winner.title
|
||||
}
|
||||
}
|
||||
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from database import Base
|
||||
|
||||
class Item(Base):
|
||||
__tablename__ = "items"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String, nullable=False)
|
||||
21
backend/app/db/models.py
Normal file
21
backend/app/db/models.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy import Integer
|
||||
from sqlalchemy import String
|
||||
|
||||
from app.db.session import Base
|
||||
|
||||
|
||||
class Item(Base):
|
||||
|
||||
__tablename__ = "items"
|
||||
|
||||
id = Column(
|
||||
Integer,
|
||||
primary_key=True,
|
||||
index=True
|
||||
)
|
||||
|
||||
title = Column(
|
||||
String,
|
||||
nullable=False
|
||||
)
|
||||
@@ -1,11 +1,11 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
DATABASE_URL = "sqlite:///./wheel.db"
|
||||
from config import settings
|
||||
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
settings.DATABASE_URL,
|
||||
connect_args={"check_same_thread": False}
|
||||
)
|
||||
|
||||
@@ -15,13 +15,4 @@ SessionLocal = sessionmaker(
|
||||
bind=engine
|
||||
)
|
||||
|
||||
# Database Session
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Base = declarative_base()
|
||||
Reference in New Issue
Block a user