From 583ffb893c1b681a2a2e8edf4b53d60f21e90035 Mon Sep 17 00:00:00 2001 From: xnought Date: Thu, 16 Nov 2023 21:19:25 -0800 Subject: [PATCH] feat: remove id as integer in place of name pk --- backend/init.sql | 4 +--- backend/src/api_types.py | 1 - backend/src/server.py | 32 ++++++++++++-------------------- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/backend/init.sql b/backend/init.sql index a922101c..a52d2497 100644 --- a/backend/init.sql +++ b/backend/init.sql @@ -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 ); diff --git a/backend/src/api_types.py b/backend/src/api_types.py index d66bfd25..ca954ad4 100644 --- a/backend/src/api_types.py +++ b/backend/src/api_types.py @@ -25,7 +25,6 @@ class CamelModel(BaseModel): class ProteinEntry(CamelModel): name: str - id: str length: int mass: float diff --git a/backend/src/server.py b/backend/src/server.py index d9e28389..2c16f7ec 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -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 @@ -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)