Skip to content

Commit

Permalink
feat: remove id as integer in place of name pk
Browse files Browse the repository at this point in the history
  • Loading branch information
xnought committed Nov 17, 2023
1 parent 11b9faf commit 583ffb8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
4 changes: 1 addition & 3 deletions backend/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
-- Note: "Numeric" data types have arbitrary precision which are good for calculations. This seems like what we want.
-- https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL
CREATE TABLE proteins (
-- todo: make the id meaningful, like Lb17_comp535_c2_seq1, discuss this
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, -- increment id for now
name text NOT NULL UNIQUE,
name text NOT NULL UNIQUE PRIMARY KEY,
length integer,
mass numeric
);
Expand Down
1 change: 0 additions & 1 deletion backend/src/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class CamelModel(BaseModel):

class ProteinEntry(CamelModel):
name: str
id: str
length: int
mass: float

Expand Down
32 changes: 12 additions & 20 deletions backend/src/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .setup import init_fastapi_app, disable_cors
from .api_types import ProteinEntry, UploadBody, UploadStatus, UploadError
from .api_types import ProteinEntry, UploadBody, UploadError
from .db import Database
from .protein import Protein
import logging as log
Expand All @@ -21,48 +21,40 @@ def get_all_entries():
with Database() as db:
try:
entries_sql = db.execute_return(
"""SELECT id, name, length, mass FROM proteins"""
"""SELECT name, length, mass FROM proteins"""
)
log.warn(entries_sql)

# if we got a result back
if entries_sql is not None:
return [
ProteinEntry(
id=str(entry[0]),
name=entry[1],
length=entry[2],
mass=entry[3],
)
for entry in entries_sql
ProteinEntry(name=name, length=length, mass=mass)
for name, length, mass in entries_sql
]
except Exception as e:
log.error(e)


@app.get("/protein-entry/{protein_id:str}", response_model=ProteinEntry | None)
def get_protein_entry(protein_id: str):
@app.get("/protein-entry/{protein_name:str}", response_model=ProteinEntry | None)
def get_protein_entry(protein_name: str):
"""Get a single protein entry by its id
Returns: ProteinEntry if found | None if not found
"""
with Database() as db:
try:
entry_sql = db.execute_return(
"""SELECT id, name, length, mass FROM proteins
WHERE id = %s""",
[protein_id],
"""SELECT name, length, mass FROM proteins
WHERE name = %s""",
[protein_name],
)
log.warn(entry_sql)

# if we got a result back
if entry_sql is not None and len(entry_sql) != 0:
# return the only entry
return ProteinEntry(
id=str(entry_sql[0][0]),
name=entry_sql[0][1],
length=entry_sql[0][2],
mass=entry_sql[0][3],
)
only_returned_entry = entry_sql[0]
name, length, mass = only_returned_entry
return ProteinEntry(name=name, length=length, mass=mass)

except Exception as e:
log.error(e)
Expand Down

0 comments on commit 583ffb8

Please sign in to comment.