Skip to content

Refactor Lost/Found modules: eliminate ~900 lines of duplication#90

Draft
adhitht with Copilot wants to merge 6 commits into
developfrom
copilot/refactor-duplicated-code
Draft

Refactor Lost/Found modules: eliminate ~900 lines of duplication#90
adhitht with Copilot wants to merge 6 commits into
developfrom
copilot/refactor-duplicated-code

Conversation

Copilot AI commented Nov 1, 2025

Copy link
Copy Markdown

Lost and Found modules in both Python and Go backends contained near-identical implementations differing only in table names.

Changes

Python Backend

  • Created generic modules: lost_found_generic.py (queries) and generic_handlers.py (routes)
  • Consolidated logic: Reduced route handlers from 162/155 lines → 39 lines each (-76%)
  • Parameterized table names: Functions accept table_name parameter instead of hardcoding "lost"/"found"

Go Backend

  • Created generic modules: lostandfound_generic.go (db layer) and controller equivalent
  • Consolidated logic: Reduced controllers from 344/291 lines → 30/20 lines each (-91%)
  • Added schema: LostFoundItem type for shared item representation

Security

  • Table name validation: Whitelist enforcement prevents SQL injection via dynamic table names
  • Parameterized queries: All user inputs use bound parameters (e.g., %s in Python, $1 in Go)
  • CodeQL verified: 0 alerts

Example

Before (lost.py):

@router.post("/add_item")
async def add_item(request, form_data, images):
    user_id = get_user_id(request)
    with conn.cursor() as cur:
        cur.execute(insert_in_lost_table(form_data, user_id))
        # ... 20+ more lines of S3 upload logic

After:

@router.post("/add_item")
async def add_item(request, form_data, images):
    return await add_item_handler(
        request, form_data, images, 
        "lost", insert_in_lost_table, insert_lost_images
    )

All API endpoints remain unchanged.

Original prompt

Find and refactor duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits November 1, 2025 14:40
Co-authored-by: adhitht <46214482+adhitht@users.noreply.github.com>
Co-authored-by: adhitht <46214482+adhitht@users.noreply.github.com>
Co-authored-by: adhitht <46214482+adhitht@users.noreply.github.com>
Co-authored-by: adhitht <46214482+adhitht@users.noreply.github.com>
Co-authored-by: adhitht <46214482+adhitht@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for improved maintainability Refactor Lost/Found modules: eliminate ~900 lines of duplication Nov 1, 2025
Copilot AI requested a review from adhitht November 1, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants