-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
primary implementation of endpoints, sql database connection/session …
…configuration
- Loading branch information
1 parent
de16df2
commit 9e7a0a9
Showing
5 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""module for CURD operations on Database.""" | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from epseon_gui import models, schemas | ||
|
||
|
||
def insert_workspace_in_to_db(db: Session, workspace: schemas.Workspace): | ||
new_workspace = models.Workspace( | ||
id=workspace.Workspace_id, | ||
type=workspace.Workspace_type, | ||
name=workspace.Workspace_name, | ||
data=workspace.Workspace_data, | ||
) | ||
db.add(new_workspace) | ||
db.commit() | ||
db.delete | ||
|
||
|
||
def get_all_workspaces_of_db(db: Session) -> List[schemas.Workspace]: | ||
workspaces = db.query(schemas.Workspace).all() | ||
return workspaces |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""module for handling database session.""" | ||
from __future__ import annotations | ||
|
||
from os import getenv | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import declarative_base, sessionmaker | ||
|
||
DATABASE_URL = f"postgresql://{getenv('POSTGRES_USER')}:localhost:5432@{getenv('SQL_HOST')}:5432/{getenv('POSTGRES_DB')}" | ||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) | ||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
Base = declarative_base() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,79 @@ | ||
"""Backend of EPSEON GUI.""" | ||
from __future__ import annotations | ||
|
||
from typing import Dict | ||
from typing import Dict, List | ||
from uuid import uuid4 | ||
|
||
from fastapi import FastAPI | ||
from fastapi import FastAPI, HTTPException | ||
|
||
from epseon_gui import models, schemas | ||
from epseon_gui.database import engine | ||
|
||
models.Base.metadata.create_all(bind=engine) | ||
|
||
app = FastAPI() | ||
|
||
|
||
workspaces: List[schemas.Workspace] = [] # temporary | ||
|
||
|
||
@app.get("/") | ||
def read_root() -> Dict[str, str]: | ||
async def root() -> str: | ||
"""Root.""" | ||
return {"Test": "test"} | ||
return "root" | ||
|
||
|
||
@app.post("/generate") | ||
async def generate_data(request: schemas.GenerationData) -> Dict[str, str]: | ||
"""Placeholder for generating output data based on input.""" | ||
return {"generatedData": "UNKNOWN FOR NOW"} | ||
|
||
|
||
@app.post("/workspaces/") | ||
async def create_workspace(workspace: schemas.WorkspaceGeneration) -> Dict[str, str]: | ||
"""Adds workspace to workspaces; returns id of added workspace""" | ||
workspace_data = workspace.model_dump() | ||
workspace_id = str(uuid4()) | ||
generated_workspace = schemas.Workspace( | ||
Workspace_id=workspace_id, | ||
Workspace_type=workspace_data["type"], | ||
Workspace_name=workspace_data["name"], | ||
Workspace_data=None, | ||
) | ||
workspaces.append(generated_workspace) | ||
|
||
return {"id": workspace_id} # return id of generated workspace | ||
|
||
|
||
@app.get("/workspaces/", response_model=List[schemas.Workspace]) | ||
async def get_all_workspaces() -> List[schemas.Workspace]: | ||
"""Returns list of workspaces.""" | ||
return workspaces | ||
|
||
|
||
@app.get("/workspace/") | ||
async def get_workspace(workspace_id: str) -> schemas.Workspace: | ||
"""Returns specific workspace based on workspace id.""" | ||
index: int = -1 | ||
for i, workspace in enumerate(workspaces): | ||
if workspace.Workspace_id == workspace_id: | ||
index = i | ||
if index == -1: | ||
raise HTTPException(status_code=404, detail="Workspace not found") | ||
else: | ||
workspace = workspaces[index] | ||
|
||
return workspace | ||
|
||
|
||
@app.delete("/workspaces/") | ||
async def delete_workspace(workspace_id: str) -> None: | ||
"""Deletes specific workspace based on workspace id.""" | ||
index: int = -1 | ||
for i, workspace in enumerate(workspaces): | ||
if workspace.Workspace_id == workspace_id: | ||
index = i | ||
if index == -1: | ||
raise HTTPException(status_code=404, detail="Workspace not found") | ||
else: | ||
workspaces.pop(index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""module containing orm models.""" | ||
from __future__ import annotations | ||
|
||
from sqlalchemy import Column, String | ||
from sqlalchemy.dialects.postgresql import JSONB | ||
|
||
from epseon_gui.database import Base | ||
|
||
|
||
class Workspace(Base): | ||
__tablename__ = "Workspaces" | ||
Workspace_id = Column(String, primary_key=True) | ||
Workspace_type = Column(String) | ||
Workspace_name = Column(String) | ||
Workspace_data = Column(JSONB, nullable=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""module containing Pydantic models.""" | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class GenerationData(BaseModel): | ||
firstLevel: int | ||
lastLevel: int | ||
firstAtomMass: float | ||
secondAtomMass: float | ||
epsilon: float | ||
h: float | ||
dispatchCount: int | ||
groupSize: int | ||
floatingPointPrecision: int | ||
deviceId: int | ||
|
||
|
||
class WorkspaceGeneration(BaseModel): | ||
WorkspaceGeneration_type: str | ||
WorkspaceGeneration_name: str | ||
WorkspaceGeneration_workdata: Optional[GenerationData] | ||
|
||
|
||
class Workspace(BaseModel): | ||
Workspace_id: str | ||
Workspace_type: str | ||
Workspace_name: str | ||
Workspace_data: Optional[GenerationData] |