From 6798b638b0349948393170ffccc75902169ab358 Mon Sep 17 00:00:00 2001 From: aabhinavvvvvvv Date: Sat, 7 Mar 2026 21:17:43 +0000 Subject: [PATCH] feat: add cell name search to GET /api/cell/ endpoint --- backend/api/models/cell.py | 5 ++++- backend/api/resources/cell.py | 5 ++++- backend/tests/test_cell.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/backend/api/models/cell.py b/backend/api/models/cell.py index 7ccf1833..adca867e 100644 --- a/backend/api/models/cell.py +++ b/backend/api/models/cell.py @@ -1,6 +1,5 @@ from ..models import db - """"This is the reference; we stole this from commenter https://stackoverflow.com/questions/5756559/how-to-build-many-to-many-relations-using-sqlalchemy-a-good-example""" @@ -81,6 +80,10 @@ def get(id): def find_by_name(name): return Cell.query.filter_by(name=name).first() + @staticmethod + def search_by_name(pattern): + return Cell.query.filter(Cell.name.ilike(f"%{pattern}%")).all() + @staticmethod def get_cells_by_user_id(id): """Get cells that a user has access to via the cell_user relationship""" diff --git a/backend/api/resources/cell.py b/backend/api/resources/cell.py index 6a6dacd4..2edd6502 100644 --- a/backend/api/resources/cell.py +++ b/backend/api/resources/cell.py @@ -25,9 +25,12 @@ def get(self, user): userCells = json_data.get("user") tag_ids = json_data.get("tags") # Comma-separated tag IDs: "1,2,3" category = json_data.get("category") # Filter by tag category + name = json_data.get("name") # Search by name substring # Base query - if userCells: + if name: + cells = CellModel.search_by_name(name) + elif userCells: cells = CellModel.get_cells_by_user_id(user.id) else: cells = CellModel.get_all() diff --git a/backend/tests/test_cell.py b/backend/tests/test_cell.py index 5597825b..cdfc6a16 100644 --- a/backend/tests/test_cell.py +++ b/backend/tests/test_cell.py @@ -1,4 +1,22 @@ from api.models.user import User +from api.models.cell import Cell + + +def test_cell_search_by_name(setup_cells): + """ + GIVEN cells exist in the database + WHEN searching cells by name pattern + THEN only cells whose names match the pattern are returned + """ + results = Cell.search_by_name("cell") + assert len(results) == 2 + + results = Cell.search_by_name("cell_1") + assert len(results) == 1 + assert results[0].name == "cell_1" + + results = Cell.search_by_name("nonexistent") + assert len(results) == 0 def test_cell_post_returns_id_and_name(init_database):