Download test_router.py from WildOjisan/embeddinggemma_300m_fastapi: direct link, hf CLI and curl.
- Browser
- Download file 7.78 kB
-
https://huggingface.co/spaces/WildOjisan/embeddinggemma_300m_fastapi/resolve/d6e603b14fc2e95ed59fb700fca2c3e166505a4f/test_router.py
- Command line
-
hf download hf://spaces/WildOjisan/embeddinggemma_300m_fastapi@d6e603b14fc2e95ed59fb700fca2c3e166505a4f/test_router.py
-
curl -L -o test_router.py https://huggingface.co/spaces/WildOjisan/embeddinggemma_300m_fastapi/resolve/d6e603b14fc2e95ed59fb700fca2c3e166505a4f/test_router.py
7.78 kB
| # crud_router.py | |
| from fastapi import APIRouter, Depends, Query, status | |
| from pydantic import BaseModel | |
| from typing import Dict, Any, List, Optional | |
| import asyncpg | |
| from datetime import datetime | |
| # database_conn.py์ ์ ์๋ get_db_connection ํจ์๋ฅผ ์ํฌํธํฉ๋๋ค. | |
| from database_conn import get_db_connection | |
| # ---------------------------------------------------- | |
| # Pydantic ๋ชจ๋ธ ์ ์ | |
| # ---------------------------------------------------- | |
| class ItemCreate(BaseModel): | |
| name: str | |
| price: float | |
| class ItemUpdate(BaseModel): | |
| name: str | None = None | |
| price: float | None = None | |
| class ItemInDB(BaseModel): | |
| id: int | |
| name: str | |
| price: float | |
| created_at: datetime | |
| # API ์๋ต ํ์คํ ๋ชจ๋ธ | |
| class APIResponse(BaseModel): | |
| success: bool | |
| msg: str = "" | |
| data: Any = None | |
| # ---------------------------------------------------- | |
| # APIRouter ์ธ์คํด์ค ์์ฑ | |
| # ---------------------------------------------------- | |
| router = APIRouter( | |
| tags=["crud_t_test1"], | |
| ) | |
| # ---------------------------------------------------- | |
| # 1. CREATE (๋ฐ์ดํฐ ์์ฑ) - POST /crud/t_test1/ | |
| # ---------------------------------------------------- | |
| async def create_t_test1_item( | |
| item: ItemCreate, | |
| conn: asyncpg.Connection = Depends(get_db_connection) | |
| ): | |
| """ | |
| ์๋ก์ด ์์ดํ ์ 't_test1' ํ ์ด๋ธ์ ์ฝ์ ํฉ๋๋ค (INSERT). | |
| """ | |
| response_data = {"success": True, "msg": "", "data": None} | |
| query = """ | |
| INSERT INTO t_test1 (name, price) | |
| VALUES ($1, $2) | |
| RETURNING id, name, price, created_at; | |
| """ | |
| try: | |
| record = await conn.fetchrow(query, item.name, item.price) | |
| if record is None: | |
| response_data["success"] = False | |
| response_data["msg"] = "Item creation failed unexpectedly: No record returned." | |
| return response_data | |
| response_data["msg"] = "Item created successfully." | |
| response_data["data"] = ItemInDB(**dict(record)) | |
| return response_data | |
| except Exception as e: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Database INSERT error: {e!r}" | |
| return response_data | |
| # ---------------------------------------------------- | |
| # 2. READ (์ ์ฒด ์กฐํ ๋ฐ ๋จ์ผ ์กฐํ) - GET /crud/t_test1/?item_id={id} ๋๋ /crud/t_test1/ | |
| # ---------------------------------------------------- | |
| async def read_t_test1_items( | |
| # item_id๊ฐ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค. | |
| item_id: Optional[int] = None, | |
| conn: asyncpg.Connection = Depends(get_db_connection) | |
| ): | |
| """ | |
| item_id๊ฐ ์์ผ๋ฉด ๋จ์ผ ์์ดํ ์, ์์ผ๋ฉด ์ ์ฒด ๋ฆฌ์คํธ๋ฅผ ์กฐํํฉ๋๋ค (SELECT). | |
| """ | |
| response_data = {"success": True, "msg": "", "data": None} | |
| try: | |
| if item_id: | |
| # ๋จ์ผ ์กฐํ (WHERE ID) | |
| query = "SELECT id, name, price, created_at FROM t_test1 WHERE id = $1;" | |
| record = await conn.fetchrow(query, item_id) | |
| if record is None: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Read failed: Item with ID {item_id} not found." | |
| return response_data | |
| response_data["msg"] = "Item read successfully." | |
| response_data["data"] = ItemInDB(**dict(record)) | |
| else: | |
| # ์ ์ฒด ์กฐํ (SELECT *) | |
| query = "SELECT id, name, price, created_at FROM t_test1 ORDER BY id;" | |
| records = await conn.fetch(query) | |
| response_data["data"] = [ItemInDB(**dict(record)) for record in records] | |
| response_data["msg"] = f"Found {len(records)} items." | |
| return response_data | |
| except Exception as e: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Database SELECT error: {e!r}" | |
| return response_data | |
| # ---------------------------------------------------- | |
| # 3. UPDATE (๋ฐ์ดํฐ ์ ๋ฐ์ดํธ) - PUT /crud/t_test1/?item_id={id} | |
| # ---------------------------------------------------- | |
| async def update_t_test1_item( | |
| # item_id๊ฐ ํ์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค. | |
| item_id: int, | |
| item: ItemUpdate, | |
| conn: asyncpg.Connection = Depends(get_db_connection) | |
| ): | |
| """ | |
| ํน์ ID์ ์์ดํ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค (UPDATE). | |
| """ | |
| response_data = {"success": True, "msg": "", "data": None} | |
| # 1. ํ์ฌ ์์ดํ ์กด์ฌ ์ฌ๋ถ ํ์ธ (์ ๋ฐ์ดํธ ์ ํ์) | |
| select_query = "SELECT id FROM t_test1 WHERE id = $1;" | |
| current_record = await conn.fetchrow(select_query, item_id) | |
| if current_record is None: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Update failed: Item with ID {item_id} not found." | |
| return response_data | |
| # 2. ๋์ SQL ์ฟผ๋ฆฌ ์์ฑ | |
| updates = [] | |
| params = [] | |
| param_count = 1 | |
| if item.name is not None: | |
| updates.append(f"name = ${param_count}") | |
| params.append(item.name) | |
| param_count += 1 | |
| if item.price is not None: | |
| updates.append(f"price = ${param_count}") | |
| params.append(item.price) | |
| param_count += 1 | |
| if not updates: | |
| response_data["msg"] = "No updates provided. Returning current item data." | |
| # ์ ๋ฐ์ดํธํ ๋ด์ฉ์ด ์๋ ๊ฒฝ์ฐ, ํ์ฌ ์์ดํ ์ ์กฐํํ์ฌ ๋ฐํ | |
| return await read_t_test1_items(item_id=item_id, conn=conn) | |
| # 3. ์ต์ข UPDATE ์ฟผ๋ฆฌ ์คํ | |
| set_clause = ", ".join(updates) | |
| where_param_index = param_count | |
| update_query = f""" | |
| UPDATE t_test1 | |
| SET {set_clause} | |
| WHERE id = ${where_param_index} | |
| RETURNING id, name, price, created_at; | |
| """ | |
| params.append(item_id) # WHERE ์ ์ ์ฌ์ฉํ ID ์ถ๊ฐ | |
| try: | |
| updated_record = await conn.fetchrow(update_query, *params) | |
| if updated_record is None: | |
| response_data["success"] = False | |
| response_data["msg"] = "Update failed: No record was modified." | |
| return response_data | |
| response_data["msg"] = "Item updated successfully." | |
| response_data["data"] = ItemInDB(**dict(updated_record)) | |
| return response_data | |
| except Exception as e: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Database UPDATE error: {e!r}" | |
| return response_data | |
| # ---------------------------------------------------- | |
| # 4. DELETE (๋ฐ์ดํฐ ์ญ์ ) - DELETE /crud/t_test1/?item_id={id} | |
| # ---------------------------------------------------- | |
| async def delete_t_test1_item( | |
| # item_id๊ฐ ํ์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค. | |
| item_id: int, | |
| conn: asyncpg.Connection = Depends(get_db_connection) | |
| ): | |
| """ | |
| ํน์ ID์ ์์ดํ ์ ์ญ์ ํฉ๋๋ค (DELETE). | |
| """ | |
| response_data = {"success": True, "msg": "", "data": None} | |
| query = "DELETE FROM t_test1 WHERE id = $1;" | |
| try: | |
| command_tag = await conn.execute(query, item_id) | |
| deleted_rows = int(command_tag.split()[-1]) | |
| if deleted_rows == 0: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Delete failed: Item with ID {item_id} not found." | |
| return response_data | |
| response_data["msg"] = f"Item with ID {item_id} deleted successfully." | |
| return response_data | |
| except Exception as e: | |
| response_data["success"] = False | |
| response_data["msg"] = f"Database DELETE error: {e!r}" | |
| return response_data |