essentials working
This commit is contained in:
parent
dade6d5330
commit
8c5513d6c8
13 changed files with 64 additions and 54 deletions
BIN
backend/dev.db
BIN
backend/dev.db
Binary file not shown.
|
|
@ -1 +1,5 @@
|
|||
[tool.pyrefly]
|
||||
[tool.pyrefly]
|
||||
|
||||
project-includes = [
|
||||
"./**/*.py",
|
||||
]
|
||||
Binary file not shown.
BIN
backend/src/backend/__pycache__/dependencies.cpython-314.pyc
Normal file
BIN
backend/src/backend/__pycache__/dependencies.cpython-314.pyc
Normal file
Binary file not shown.
Binary file not shown.
13
backend/src/backend/dependencies.py
Normal file
13
backend/src/backend/dependencies.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from typing import Annotated
|
||||
|
||||
from fastapi import Header, HTTPException
|
||||
|
||||
|
||||
async def get_token_header(x_token: Annotated[str, Header()]):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
async def get_query_token(token: str):
|
||||
if token != "jessica":
|
||||
raise HTTPException(status_code=400, detail="No Jessica token provided")
|
||||
Binary file not shown.
Binary file not shown.
16
backend/src/backend/internal/database.py
Normal file
16
backend/src/backend/internal/database.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from collections.abc import Generator
|
||||
from sqlmodel import Session
|
||||
from sqlmodel import SQLModel
|
||||
from sqlmodel import create_engine
|
||||
sqlite_file = "dev.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file}"
|
||||
|
||||
connect_args = {"check_same_thread": False}
|
||||
engine = create_engine(sqlite_url, connect_args=connect_args)
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
def get_session() -> Generator[Session, None, None]:
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
|
@ -1,56 +1,19 @@
|
|||
from email.policy import default
|
||||
from typing import Annotated
|
||||
from .routers import subscriptions
|
||||
from backend.internal.database import create_db_and_tables
|
||||
from fastapi import APIRouter
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import Depends
|
||||
from fastapi import FastAPI
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
|
||||
|
||||
# TODO: sql zeugs richtig implementieren nach FastApi docs
|
||||
# TODO: Integrationtests schreiben
|
||||
# TODO: Api Specs definieren (Subscription, Comment, Like)
|
||||
# TODO: Settings hinzufügen
|
||||
|
||||
class Subscription(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
email: str = Field(index=True, unique=True)
|
||||
active: bool = Field()
|
||||
|
||||
sqlite_file = "dev.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file}"
|
||||
|
||||
connect_args = {"check_same_thread": False}
|
||||
engine = create_engine(sqlite_url, connect_args=connect_args)
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
def get_session():
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
SessionDep = Annotated[Session, Depends(get_session)]
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/subscription/")
|
||||
def get_sub():
|
||||
return {"name":"TestSub"}
|
||||
|
||||
@router.post("/subscription/")
|
||||
def create_subscription(subscription: Subscription, session: SessionDep) -> Subscription:
|
||||
session.add(subscription)
|
||||
session.commit()
|
||||
session.refresh(subscription)
|
||||
return subscription
|
||||
|
||||
|
||||
app.include_router(router)
|
||||
app.include_router(subscriptions.router)
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
|
|
@ -1,20 +1,34 @@
|
|||
from sqlmodel import Field
|
||||
from sqlmodel import SQLModel
|
||||
from backend.main import SessionDep
|
||||
from ..internal.database import get_session
|
||||
from fastapi import Depends
|
||||
from fastapi import APIRouter
|
||||
from sqlmodel import Field
|
||||
from sqlmodel import SQLModel, Session
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class Subscription(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
email: str = Field(index=True, unique=True)
|
||||
active: bool = Field()
|
||||
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/subscriptions",
|
||||
tags=["subscriptions"],
|
||||
dependencies=[],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
# @router.get("/subscription/")
|
||||
# def get_sub():
|
||||
# return {"name":"TestSub"}
|
||||
@router.get("/")
|
||||
def get_sub():
|
||||
return {"name":"TestSub"}
|
||||
|
||||
# @router.post("/subscription/")
|
||||
# def create_subscription(subscription: Subscription, session: SessionDep) -> Subscription:
|
||||
# session.add(subscription)
|
||||
# session.commit()
|
||||
# session.refresh(subscription)
|
||||
# return subscription
|
||||
@router.post("/")
|
||||
def create_subscription(
|
||||
subscription: Subscription,
|
||||
session: Session = Depends(get_session),
|
||||
) -> Subscription:
|
||||
session.add(subscription)
|
||||
session.commit()
|
||||
session.refresh(subscription)
|
||||
return subscription
|
||||
Loading…
Add table
Reference in a new issue