Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: findings #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions epiviz-ws_api/app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@
sqlalchemy.Column('workspace_uuid', sqlalchemy.String, unique=True, index=True)
)

findings = sqlalchemy.Table(
'findings',
metadata,
sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True, index=True),
sqlalchemy.Column('user_id', sqlalchemy.String, index=True),
sqlalchemy.Column('title', sqlalchemy.String, index=True),
sqlalchemy.Column('description', sqlalchemy.String, index=True),
sqlalchemy.Column('gene', sqlalchemy.ARRAY(sqlalchemy.String), nullable=True, index=True),
sqlalchemy.Column('genes_in_view', sqlalchemy.ARRAY(sqlalchemy.String), nullable=True),
sqlalchemy.Column('chart_markers', sqlalchemy.ARRAY(sqlalchemy.String), nullable=True),
sqlalchemy.Column('datasets', sqlalchemy.ARRAY(sqlalchemy.String), nullable=True),
sqlalchemy.Column('chr', sqlalchemy.String, index=True),
sqlalchemy.Column('start', sqlalchemy.Integer, index=True),
sqlalchemy.Column('end', sqlalchemy.Integer, index=True),
sqlalchemy.Column('workspace_id', sqlalchemy.Integer, index=True),
sqlalchemy.Column('workspace_uuid', sqlalchemy.String, index=True)
)

# sqlalchemy.Column('workspace_uuid', sqlalchemy.String)

engine = sqlalchemy.create_engine(DATABASE_URI)
metadata.create_all(engine)

# https://alembic.sqlalchemy.org/en/latest/cookbook.html#building-an-up-to-date-database-from-scratch
from alembic.config import Config
from alembic import command
alembic_cfg = Config("alembic.ini")
command.stamp(alembic_cfg, "head")
# from alembic.config import Config
# from alembic import command
# alembic_cfg = Config("alembic.ini")
# command.stamp(alembic_cfg, "head")
43 changes: 41 additions & 2 deletions epiviz-ws_api/app/db_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from app.db import database, workspaces
from app.db import database, workspaces, findings
from app import models
from uuid import uuid4

# Workspaces
async def get_all_workspaces():
query = workspaces.select()
return await database.fetch_all(query)
Expand Down Expand Up @@ -43,4 +44,42 @@ async def update_workspace(id: int, ws: models.WorkspaceUpdate):
.where(workspaces.c.id == id)
.values(**ws.dict())
)
return await database.execute(query=query)
return await database.execute(query=query)

async def get_workspace_findings(id:int):
query = findings.select(findings.c.workspace_id == id)
res = await database.fetch_all(query)
print(res)
return res

# Findings
async def get_all_findings():
query = findings.select()
return await database.fetch_all(query)

async def get_user_findings(user_id: str):
query = findings.select(findings.c.user_id == user_id)
return await database.fetch_all(query)

async def get_finding(id: int):
query = findings.select(findings.c.id == id)
return await database.fetch_one(query)

async def add_finding(fd: models.FindingCreate):
tfd = fd.dict()
query = findings.insert().values(tfd)
return await database.execute(query)

async def delete_finding(id: int):
query = findings.delete().where(findings.c.id == id)
return await database.execute(query)

async def update_finding(id: int, fd: models.FindingUpdate):
query = (
findings
.update()
.where(findings.c.id == id)
.values(**fd.dict())
)
return await database.execute(query=query)

59 changes: 59 additions & 0 deletions epiviz-ws_api/app/findings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import List, Optional
from datetime import datetime
from fastapi import Header, APIRouter, Depends, HTTPException

from app import models
from app import db_manager
from app.auth import get_user_from_header

findings = APIRouter()

@findings.get('/', response_model=List[models.Finding])
async def index():
return await db_manager.get_all_findings()

@findings.get('/user/{user_id}', response_model=List[models.Finding])
async def get_user_findings(user_id: str, user: models.User = Depends(get_user_from_header)):
if user_id == user.username:
return await db_manager.get_user_findings(user_id)
else:
raise HTTPException(status_code=404, detail="UnixID does not match token")

@findings.get('/{id}', response_model=models.Finding)
async def get_finding(id: int):
return await db_manager.get_finding(id)

@findings.post('/', response_model=models.Finding)
async def add_finding(payload: models.FindingCreate):
id = await db_manager.add_finding(payload)
response = {
'id': id,
**payload.dict()
}

return response

@findings.put('/{id}')
async def update_finding(id: int, payload: models.FindingUpdate, user: models.User = Depends(get_user_from_header)):
fd = await db_manager.get_finding(id)
if not fd:
raise HTTPException(status_code=404, detail="Finding not found")

if fd.get("user_id") == user.username or "admin" in user.roles:
update_data = payload.dict(exclude_unset=True)
fd_in_db = models.findingUpdate(**fd)
updated_fd = fd_in_db.copy(update=update_data)
return await db_manager.update_finding(id, updated_fd)
else:
raise HTTPException(status_code=404, detail="UnixID does not match token")

@findings.delete('/{id}')
async def delete_finding(id: int, user: models.User = Depends(get_user_from_header)):
fd = await db_manager.get_finding(id)
if not fd:
raise HTTPException(status_code=404, detail="finding not found")

if fd.get("user_id") == user.username:
return await db_manager.delete_finding(id)
else:
raise HTTPException(status_code=404, detail="UnixID does not match token")
2 changes: 2 additions & 0 deletions epiviz-ws_api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import FastAPI, Depends
from app.db import database
from app.workspaces import workspaces
from app.findings import findings
from fastapi.middleware.cors import CORSMiddleware
import os

Expand Down Expand Up @@ -30,5 +31,6 @@ async def shutdown():
await database.disconnect()

app.include_router(workspaces, prefix='/api/v1', tags=['workspaces'])
app.include_router(findings, prefix='/api/v1/findings', tags=['findings'])


26 changes: 25 additions & 1 deletion epiviz-ws_api/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,28 @@ class User(BaseModel):
username: str
email: Optional[str] = None
full_name: Optional[str] = None
roles: Optional[List[str]] = None
roles: Optional[List[str]] = None

class FindingBase(BaseModel):
user_id: str
title: str
description: str
gene: Optional[List[str]]
genes_in_view: Optional[List[str]]
chart_markers: Optional[List[str]]
datasets: Optional[List[str]]
chr: str
start: int
end: int
workspace_id: int
workspace_uuid: str

class FindingCreate(FindingBase):
pass

class Finding(FindingBase):
id: int

class FindingUpdate(FindingBase):
title: Optional[str] = None
description: Optional[str] = None
4 changes: 4 additions & 0 deletions epiviz-ws_api/app/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ async def get_user_workspaces(user_id: str, user: models.User = Depends(get_user
async def get_workspace(id: int):
return await db_manager.get_workspace(id)

@workspaces.get('/{id}/findings', response_model=List[models.Finding])
async def get_workspace_findings(id: int):
return await db_manager.get_workspace_findings(id)

@workspaces.get('/uuid/{uuid}', response_model=models.Workspace)
async def get_workspace_uuid(uuid: str):
return await db_manager.get_workspace_uuid(uuid)
Expand Down