initial commit
This commit is contained in:
BIN
backend/__pycache__/crud.cpython-313.pyc
Normal file
BIN
backend/__pycache__/crud.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/database.cpython-313.pyc
Normal file
BIN
backend/__pycache__/database.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/main.cpython-313.pyc
Normal file
BIN
backend/__pycache__/main.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/models.cpython-313.pyc
Normal file
BIN
backend/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/schemas.cpython-313.pyc
Normal file
BIN
backend/__pycache__/schemas.cpython-313.pyc
Normal file
Binary file not shown.
32
backend/crud.py
Normal file
32
backend/crud.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from models import Item
|
||||||
|
import random
|
||||||
|
|
||||||
|
def get_items(db: Session):
|
||||||
|
return db.query(Item).all()
|
||||||
|
|
||||||
|
def create_item(db: Session, title: str):
|
||||||
|
item = Item(title=title)
|
||||||
|
|
||||||
|
db.add(item)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(item)
|
||||||
|
|
||||||
|
return item
|
||||||
|
|
||||||
|
def delete_item(db: Session, item_id: int):
|
||||||
|
item = db.query(Item).filter(Item.id == item_id).first()
|
||||||
|
|
||||||
|
if item:
|
||||||
|
db.delete(item)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return item
|
||||||
|
|
||||||
|
def spin_wheel(db: Session):
|
||||||
|
items = db.query(Item).all()
|
||||||
|
|
||||||
|
if not items:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return random.choice(items)
|
||||||
18
backend/database.py
Normal file
18
backend/database.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
DATABASE_URL = "sqlite:///./wheel.db"
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
DATABASE_URL,
|
||||||
|
connect_args={"check_same_thread": False}
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(
|
||||||
|
autocommit=False,
|
||||||
|
autoflush=False,
|
||||||
|
bind=engine
|
||||||
|
)
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
79
backend/main.py
Normal file
79
backend/main.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
8
backend/models.py
Normal file
8
backend/models.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
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)
|
||||||
11
backend/schemas.py
Normal file
11
backend/schemas.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class ItemCreate(BaseModel):
|
||||||
|
title: str
|
||||||
|
|
||||||
|
class ItemResponse(BaseModel):
|
||||||
|
id: int
|
||||||
|
title: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
BIN
backend/wheel.db
Normal file
BIN
backend/wheel.db
Normal file
Binary file not shown.
0
frontend-admin/admin.html
Normal file
0
frontend-admin/admin.html
Normal file
0
frontend-admin/app.js
Normal file
0
frontend-admin/app.js
Normal file
0
frontend-admin/index.html
Normal file
0
frontend-admin/index.html
Normal file
BIN
requirements.txt
Normal file
BIN
requirements.txt
Normal file
Binary file not shown.
Reference in New Issue
Block a user