Fixed formatting and type definitions
This commit is contained in:
parent
bbe0df9546
commit
d97335e0c7
5 changed files with 112 additions and 65 deletions
|
|
@ -1,27 +1,38 @@
|
|||
from sqlmodel import SQLModel, create_engine, Session, select
|
||||
from sqlmodel import Session, SQLModel, create_engine, select
|
||||
|
||||
from app.models import example_model, project
|
||||
from app.config import settings
|
||||
|
||||
engine = create_engine(
|
||||
settings.database_url, echo=True,
|
||||
settings.database_url,
|
||||
echo=True,
|
||||
connect_args={"check_same_thread": False},
|
||||
)
|
||||
|
||||
|
||||
def create_database():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
def add_to_database(input: any):
|
||||
|
||||
|
||||
def add_to_database(input: SQLModel):
|
||||
with Session(engine) as session:
|
||||
session.add(input)
|
||||
session.commit()
|
||||
|
||||
def get_from_database(query: any) -> list:
|
||||
|
||||
|
||||
def get_from_database(query: type[SQLModel]) -> list[SQLModel]:
|
||||
with Session(engine) as session:
|
||||
results = session.exec(select(query)).all()
|
||||
return results
|
||||
|
||||
def get_from_database_where(query: any, variable: any, value: any, first_only: bool = False, offset: int | None = None, limit: int | None = None) -> list:
|
||||
|
||||
def get_from_database_where(
|
||||
query: type[SQLModel],
|
||||
variable: object,
|
||||
value: int | str | None,
|
||||
first_only: bool = False,
|
||||
offset: int | None = None,
|
||||
limit: int | None = None,
|
||||
) -> list[SQLModel]:
|
||||
with Session(engine) as session:
|
||||
statement = select(query).where(variable == value)
|
||||
if offset:
|
||||
|
|
@ -35,25 +46,47 @@ def get_from_database_where(query: any, variable: any, value: any, first_only: b
|
|||
results = results.all()
|
||||
return results
|
||||
|
||||
def get_from_database_where_id(query: any, value: int):
|
||||
|
||||
def get_from_database_where_id(
|
||||
query: type[SQLModel],
|
||||
value: int,
|
||||
) -> SQLModel | None:
|
||||
with Session(engine) as session:
|
||||
result = session.get(query, value)
|
||||
return result
|
||||
|
||||
def get_from_database_where_join_filter(query: any, joined: any, variable: any, value: any):
|
||||
|
||||
def get_from_database_where_join_filter(
|
||||
query: type[SQLModel],
|
||||
joined: SQLModel,
|
||||
variable: object,
|
||||
value: int | str | None,
|
||||
):
|
||||
with Session(engine) as session:
|
||||
results = session.exec(select(query).join(joined).where(variable == value))
|
||||
return results
|
||||
|
||||
def get_and_join_from_database(query: any, joined: any, isouter: bool = False):
|
||||
|
||||
def get_and_join_from_database(
|
||||
query: type[SQLModel],
|
||||
joined: SQLModel,
|
||||
isouter: bool = False,
|
||||
):
|
||||
with Session(engine) as session:
|
||||
results = session.exec(select(query, joined).join(joined, isouter)).all()
|
||||
return results
|
||||
|
||||
def update_database_where(query: any, variable: any, value: any, attribute: any, new_value: any):
|
||||
|
||||
def update_database_where(
|
||||
query: type[SQLModel],
|
||||
variable: object,
|
||||
value: int | str | None,
|
||||
attribute: object,
|
||||
new_value: int | str | None,
|
||||
):
|
||||
with Session(engine) as session:
|
||||
results = session.exec(select(query).where(variable == value))
|
||||
|
||||
|
||||
for instance in results:
|
||||
instance.setAttribute(attribute, new_value)
|
||||
session.add(instance)
|
||||
|
|
@ -61,18 +94,27 @@ def update_database_where(query: any, variable: any, value: any, attribute: any,
|
|||
session.refresh(instance)
|
||||
return results
|
||||
|
||||
def delete_from_database_where(query: any, variable: any, value: any):
|
||||
|
||||
def delete_from_database_where(
|
||||
query: type[SQLModel],
|
||||
variable: object,
|
||||
value: str | int | None,
|
||||
):
|
||||
with Session(engine) as session:
|
||||
results = session.exec(select(query).where(variable == value))
|
||||
|
||||
|
||||
for instance in results:
|
||||
session.delete(instance)
|
||||
session.commit()
|
||||
|
||||
|
||||
remainder = session.exec(select(query).where(variable == value))
|
||||
if remainder is None:
|
||||
return {"message": "Successfully deleted matching query(ies)", "deleted": results.all()}
|
||||
return {
|
||||
"message": "Successfully deleted matching query(ies)",
|
||||
"deleted": results.all(),
|
||||
}
|
||||
else:
|
||||
return {"message": "Failed to delete all matching queries", "remaining": remainder.all()}
|
||||
|
||||
|
||||
return {
|
||||
"message": "Failed to delete all matching queries",
|
||||
"remaining": remainder.all(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,32 @@
|
|||
from app.database import (
|
||||
add_to_database,
|
||||
get_from_database,
|
||||
get_from_database_where,
|
||||
get_from_database_where_id,
|
||||
)
|
||||
from app.models.project import Project
|
||||
from app.database import add_to_database, get_from_database, get_from_database_where, get_from_database_where_id
|
||||
|
||||
# For now, this is a manifest for the project model architecture:
|
||||
# For now, this is a manifest for the project model architecture:
|
||||
|
||||
# A project consists of:
|
||||
# ID (Autoincrement num, required),
|
||||
# A project consists of:
|
||||
# ID (Autoincrement num, required),
|
||||
# Parent (Num, optional, if this field contains a number (not none or 0), it is considered a child project of project with ID = num)
|
||||
# Title (Required, text),
|
||||
# Title (Required, text),
|
||||
# Content (Optional, for now is string text but needs to store full Markdown including images in the future)
|
||||
|
||||
|
||||
def get_all():
|
||||
return {"projects": get_from_database(Project)}
|
||||
return get_from_database(Project)
|
||||
|
||||
def get_by_id(project_id):
|
||||
return {"project": get_from_database_where_id(Project, project_id)[0]}
|
||||
|
||||
def get_with_parent(project_id):
|
||||
return {"projects": get_from_database_where(Project, Project.parent_id, project_id)}
|
||||
def get_by_id(project_id: int):
|
||||
return get_from_database_where_id(Project, project_id)
|
||||
|
||||
|
||||
def get_with_parent(project_id: int):
|
||||
return get_from_database_where(Project, Project.parent_id, project_id)
|
||||
|
||||
|
||||
def create(project: Project):
|
||||
add_to_database(project)
|
||||
return project
|
||||
return project
|
||||
|
|
|
|||
27
app/main.py
27
app/main.py
|
|
@ -1,22 +1,27 @@
|
|||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.config import settings
|
||||
from app.database import create_database
|
||||
from app.routers import example_route, project_route
|
||||
|
||||
app = FastAPI(
|
||||
title=settings.app_name,
|
||||
version=settings.app_version
|
||||
)
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup():
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
create_database()
|
||||
# Stuff that runs once before the app starts :D
|
||||
yield
|
||||
# Stuff that runs when it shuts down :>
|
||||
|
||||
app.include_router(
|
||||
example_route.router
|
||||
|
||||
app = FastAPI(
|
||||
lifespan=lifespan,
|
||||
title=settings.app_name,
|
||||
version=settings.app_version,
|
||||
)
|
||||
|
||||
app.include_router(
|
||||
project_route.router
|
||||
)
|
||||
|
||||
app.include_router(example_route.router)
|
||||
|
||||
app.include_router(project_route.router)
|
||||
|
|
|
|||
|
|
@ -1,19 +1,11 @@
|
|||
from sqlmodel import SQLModel, Field
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
|
||||
class Project(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
id: int | None = Field(
|
||||
default=None,
|
||||
primary_key=True
|
||||
)
|
||||
|
||||
parent_id: int | None = Field(
|
||||
default=None,
|
||||
foreign_key="project.id"
|
||||
)
|
||||
parent_id: int | None = Field(default=None, foreign_key="project.id")
|
||||
|
||||
title: str
|
||||
|
||||
content: str | None = Field(default=None)
|
||||
|
||||
|
||||
|
|
@ -1,29 +1,28 @@
|
|||
from app.models.project import Project
|
||||
from fastapi import APIRouter
|
||||
from app.logic.project_logic import create, get_all, get_by_id, get_with_parent
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/projects",
|
||||
tags=["projects"]
|
||||
)
|
||||
from app.logic.project_logic import create, get_all, get_by_id, get_with_parent
|
||||
from app.models.project import Project
|
||||
|
||||
router = APIRouter(prefix="/projects", tags=["projects"])
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def get_default():
|
||||
return {"message": "Root for Projects API"}
|
||||
|
||||
|
||||
return {
|
||||
"message": "Root for Projects API"
|
||||
}
|
||||
|
||||
@router.get("/all")
|
||||
def get_all_projects(parent: int | None = None):
|
||||
if parent:
|
||||
return get_with_parent(parent)
|
||||
return get_all()
|
||||
|
||||
|
||||
|
||||
@router.get("/project/{project_id}")
|
||||
def get_project_by_id(project_id: int):
|
||||
return get_by_id(project_id)
|
||||
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def create_project(project: Project):
|
||||
return create(project)
|
||||
return create(project)
|
||||
|
|
|
|||
Loading…
Reference in a new issue