From e873b902ad5a137053d9d590e3cd15ca45bcefd9 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:22:43 -0800 Subject: [PATCH 1/2] feat: protein basic name search endpoint --- backend/src/server.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backend/src/server.py b/backend/src/server.py index fc74b0b2..f6415f39 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -35,6 +35,30 @@ def get_all_entries(): log.error(e) +@app.get("/search-entries/{query:str}", response_model=list[ProteinEntry] | None) +def search_entries(query: str): + """Gets a list of protein entries by a search string + Returns: list[ProteinEntry] if found | None if not found + """ + with Database() as db: + try: + entries_sql = db.execute_return( + """SELECT name, length, mass FROM proteins + WHERE name LIKE %%%s%""", + [query] + ) + log.warn(entries_sql) + + # if we got a result back + if entries_sql is not None: + return [ + 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_name:str}", response_model=ProteinEntry | None) def get_protein_entry(protein_name: str): """Get a single protein entry by its id From 12f6ed626930ad9733a010f1d87940e6a140b86d Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Tue, 28 Nov 2023 16:19:24 -0800 Subject: [PATCH 2/2] Fixed endpoint for search functionality. Created test page at /searchtest/[query] to test search. --- backend/src/server.py | 4 +- frontend/src/openapi/models/EditBody.ts | 1 - .../src/openapi/models/HTTPValidationError.ts | 1 - frontend/src/openapi/models/ProteinEntry.ts | 1 - frontend/src/openapi/models/UploadBody.ts | 1 - .../src/openapi/models/ValidationError.ts | 1 - .../src/openapi/services/DefaultService.ts | 51 ++++++--- .../routes/searchtest/[query]/+page.svelte | 101 ++++++++++++++++++ .../src/routes/searchtest/[query]/+page.ts | 7 ++ 9 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 frontend/src/routes/searchtest/[query]/+page.svelte create mode 100644 frontend/src/routes/searchtest/[query]/+page.ts diff --git a/backend/src/server.py b/backend/src/server.py index f6415f39..684a75d8 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -44,9 +44,9 @@ def search_entries(query: str): try: entries_sql = db.execute_return( """SELECT name, length, mass FROM proteins - WHERE name LIKE %%%s%""", - [query] + WHERE name ILIKE \'%{}%\'""".format(query) ) + log.warn("log test") log.warn(entries_sql) # if we got a result back diff --git a/frontend/src/openapi/models/EditBody.ts b/frontend/src/openapi/models/EditBody.ts index 0c7f5aac..aa1acdab 100644 --- a/frontend/src/openapi/models/EditBody.ts +++ b/frontend/src/openapi/models/EditBody.ts @@ -8,4 +8,3 @@ export type EditBody = { newName: string; newContent?: (string | null); }; - diff --git a/frontend/src/openapi/models/HTTPValidationError.ts b/frontend/src/openapi/models/HTTPValidationError.ts index c0bcc87c..e218a9f3 100644 --- a/frontend/src/openapi/models/HTTPValidationError.ts +++ b/frontend/src/openapi/models/HTTPValidationError.ts @@ -8,4 +8,3 @@ import type { ValidationError } from './ValidationError'; export type HTTPValidationError = { detail?: Array; }; - diff --git a/frontend/src/openapi/models/ProteinEntry.ts b/frontend/src/openapi/models/ProteinEntry.ts index 6f42077b..0e5a3142 100644 --- a/frontend/src/openapi/models/ProteinEntry.ts +++ b/frontend/src/openapi/models/ProteinEntry.ts @@ -9,4 +9,3 @@ export type ProteinEntry = { mass: number; content?: (string | null); }; - diff --git a/frontend/src/openapi/models/UploadBody.ts b/frontend/src/openapi/models/UploadBody.ts index 1ec03121..43f1d50c 100644 --- a/frontend/src/openapi/models/UploadBody.ts +++ b/frontend/src/openapi/models/UploadBody.ts @@ -8,4 +8,3 @@ export type UploadBody = { content: string; pdbFileBase64: string; }; - diff --git a/frontend/src/openapi/models/ValidationError.ts b/frontend/src/openapi/models/ValidationError.ts index 18997ec7..0a3e90e9 100644 --- a/frontend/src/openapi/models/ValidationError.ts +++ b/frontend/src/openapi/models/ValidationError.ts @@ -8,4 +8,3 @@ export type ValidationError = { msg: string; type: string; }; - diff --git a/frontend/src/openapi/services/DefaultService.ts b/frontend/src/openapi/services/DefaultService.ts index 3d0cf8bf..b2f66a1a 100644 --- a/frontend/src/openapi/services/DefaultService.ts +++ b/frontend/src/openapi/services/DefaultService.ts @@ -16,7 +16,7 @@ export class DefaultService { /** * Get All Entries * Gets all protein entries from the database - * Returns: list[ProteinEntry] if found | None if not found + * Returns: list[ProteinEntry] if found | None if not found * @returns any Successful Response * @throws ApiError */ @@ -27,17 +27,40 @@ export class DefaultService { }); } + /** + * Search Entries + * Gets a list of protein entries by a search string + * Returns: list[ProteinEntry] if found | None if not found + * @param query + * @returns any Successful Response + * @throws ApiError + */ + public static searchEntries( +query: string, +): CancelablePromise<(Array | null)> { + return __request(OpenAPI, { + method: 'GET', + url: '/search-entries/{query}', + path: { + 'query': query, + }, + errors: { + 422: `Validation Error`, + }, + }); + } + /** * Get Protein Entry * Get a single protein entry by its id - * Returns: ProteinEntry if found | None if not found - * @param proteinName + * Returns: ProteinEntry if found | None if not found + * @param proteinName * @returns any Successful Response * @throws ApiError */ public static getProteinEntry( - proteinName: string, - ): CancelablePromise<(ProteinEntry | null)> { +proteinName: string, +): CancelablePromise<(ProteinEntry | null)> { return __request(OpenAPI, { method: 'GET', url: '/protein-entry/{protein_name}', @@ -52,13 +75,13 @@ export class DefaultService { /** * Delete Protein Entry - * @param proteinName + * @param proteinName * @returns any Successful Response * @throws ApiError */ public static deleteProteinEntry( - proteinName: string, - ): CancelablePromise { +proteinName: string, +): CancelablePromise { return __request(OpenAPI, { method: 'DELETE', url: '/protein-entry/{protein_name}', @@ -73,13 +96,13 @@ export class DefaultService { /** * Upload Protein Entry - * @param requestBody + * @param requestBody * @returns any Successful Response * @throws ApiError */ public static uploadProteinEntry( - requestBody: UploadBody, - ): CancelablePromise<(UploadError | null)> { +requestBody: UploadBody, +): CancelablePromise<(UploadError | null)> { return __request(OpenAPI, { method: 'POST', url: '/protein-upload', @@ -93,13 +116,13 @@ export class DefaultService { /** * Edit Protein Entry - * @param requestBody + * @param requestBody * @returns any Successful Response * @throws ApiError */ public static editProteinEntry( - requestBody: EditBody, - ): CancelablePromise<(UploadError | null)> { +requestBody: EditBody, +): CancelablePromise<(UploadError | null)> { return __request(OpenAPI, { method: 'PUT', url: '/protein-edit', diff --git a/frontend/src/routes/searchtest/[query]/+page.svelte b/frontend/src/routes/searchtest/[query]/+page.svelte new file mode 100644 index 00000000..6f9a1623 --- /dev/null +++ b/frontend/src/routes/searchtest/[query]/+page.svelte @@ -0,0 +1,101 @@ + + + + + + Home + + +
+ + + + + Protein name + Length + Mass (Da) + + + {#if allEntries} + {#each allEntries as entry} + { + goto(`/protein/${entry.name}`); + }} + > + {humanReadableProteinName(entry.name)} + {entry.length} + {numberWithCommas(entry.mass)} + + {/each} + {/if} + +
+
+ +
+ {#if allEntries} + {#each allEntries as entry} + goto(`/protein/${entry.name}`)} + > +
+ {humanReadableProteinName(entry.name)} +
+
+ Length: {entry.length}, Mass (Da): {numberWithCommas( + entry.mass + )} +
+
+ {/each} + {/if} +
+
+
+
+ + diff --git a/frontend/src/routes/searchtest/[query]/+page.ts b/frontend/src/routes/searchtest/[query]/+page.ts new file mode 100644 index 00000000..1764b7d5 --- /dev/null +++ b/frontend/src/routes/searchtest/[query]/+page.ts @@ -0,0 +1,7 @@ +/** + * This scrapes the data from the route + * ie. /protein/your_mom -> { proteinName: "your_mom" } + */ +export function load({ params }) { + return { query: params.query }; +}