Commit
ยท
791bc85
1
Parent(s):
ba52225
- README.md +0 -1
- app.py +3 -1
- test_router.py +229 -0
README.md
CHANGED
|
@@ -12,4 +12,3 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
| 12 |
py -3.10 -m uv venv venv
|
| 13 |
|
| 14 |
.\venv\Scripts\Activate.ps1
|
| 15 |
-
...
|
|
|
|
| 12 |
py -3.10 -m uv venv venv
|
| 13 |
|
| 14 |
.\venv\Scripts\Activate.ps1
|
|
|
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import torch
|
|
| 8 |
from typing import List, Dict, Any, Union
|
| 9 |
|
| 10 |
from database_conn import connect_to_db, close_db_connection, get_db_connection
|
|
|
|
| 11 |
|
| 12 |
import os # ํ๊ฒฝ ๋ณ์๋ฅผ ์ฝ๊ธฐ ์ํด os ๋ชจ๋ ์ถ๊ฐ
|
| 13 |
|
|
@@ -74,7 +75,8 @@ def read_root():
|
|
| 74 |
result["success"] = False
|
| 75 |
result["msg"]=f"server error. {e!r}"
|
| 76 |
return result
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
class MakeTextEmbedding(BaseModel):
|
| 80 |
"""
|
|
|
|
| 8 |
from typing import List, Dict, Any, Union
|
| 9 |
|
| 10 |
from database_conn import connect_to_db, close_db_connection, get_db_connection
|
| 11 |
+
from test_router import router as test_router
|
| 12 |
|
| 13 |
import os # ํ๊ฒฝ ๋ณ์๋ฅผ ์ฝ๊ธฐ ์ํด os ๋ชจ๋ ์ถ๊ฐ
|
| 14 |
|
|
|
|
| 75 |
result["success"] = False
|
| 76 |
result["msg"]=f"server error. {e!r}"
|
| 77 |
return result
|
| 78 |
+
|
| 79 |
+
app.include_router(test_router, prefix="/api/test")
|
| 80 |
|
| 81 |
class MakeTextEmbedding(BaseModel):
|
| 82 |
"""
|
test_router.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# crud_router.py
|
| 2 |
+
|
| 3 |
+
from fastapi import APIRouter, Depends, Query, status
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import Dict, Any, List, Optional
|
| 6 |
+
import asyncpg
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
# database_conn.py์ ์ ์๋ get_db_connection ํจ์๋ฅผ ์ํฌํธํฉ๋๋ค.
|
| 10 |
+
from database_conn import get_db_connection
|
| 11 |
+
|
| 12 |
+
# ----------------------------------------------------
|
| 13 |
+
# Pydantic ๋ชจ๋ธ ์ ์
|
| 14 |
+
# ----------------------------------------------------
|
| 15 |
+
class ItemCreate(BaseModel):
|
| 16 |
+
name: str
|
| 17 |
+
price: float
|
| 18 |
+
|
| 19 |
+
class ItemUpdate(BaseModel):
|
| 20 |
+
name: str | None = None
|
| 21 |
+
price: float | None = None
|
| 22 |
+
|
| 23 |
+
class ItemInDB(BaseModel):
|
| 24 |
+
id: int
|
| 25 |
+
name: str
|
| 26 |
+
price: float
|
| 27 |
+
created_at: datetime
|
| 28 |
+
|
| 29 |
+
# API ์๋ต ํ์คํ ๋ชจ๋ธ
|
| 30 |
+
class APIResponse(BaseModel):
|
| 31 |
+
success: bool
|
| 32 |
+
msg: str = ""
|
| 33 |
+
data: Any = None
|
| 34 |
+
|
| 35 |
+
# ----------------------------------------------------
|
| 36 |
+
# APIRouter ์ธ์คํด์ค ์์ฑ
|
| 37 |
+
# ----------------------------------------------------
|
| 38 |
+
router = APIRouter(
|
| 39 |
+
prefix="/crud/t_test1",
|
| 40 |
+
tags=["crud_t_test1"],
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# ----------------------------------------------------
|
| 44 |
+
# 1. CREATE (๋ฐ์ดํฐ ์์ฑ) - POST /crud/t_test1/
|
| 45 |
+
# ----------------------------------------------------
|
| 46 |
+
@router.post("/", response_model=APIResponse)
|
| 47 |
+
async def create_t_test1_item(
|
| 48 |
+
item: ItemCreate,
|
| 49 |
+
conn: asyncpg.Connection = Depends(get_db_connection)
|
| 50 |
+
):
|
| 51 |
+
"""
|
| 52 |
+
์๋ก์ด ์์ดํ
์ 't_test1' ํ
์ด๋ธ์ ์ฝ์
ํฉ๋๋ค (INSERT).
|
| 53 |
+
"""
|
| 54 |
+
response_data = {"success": True, "msg": "", "data": None}
|
| 55 |
+
|
| 56 |
+
query = """
|
| 57 |
+
INSERT INTO t_test1 (name, price)
|
| 58 |
+
VALUES ($1, $2)
|
| 59 |
+
RETURNING id, name, price, created_at;
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
record = await conn.fetchrow(query, item.name, item.price)
|
| 64 |
+
|
| 65 |
+
if record is None:
|
| 66 |
+
response_data["success"] = False
|
| 67 |
+
response_data["msg"] = "Item creation failed unexpectedly: No record returned."
|
| 68 |
+
return response_data
|
| 69 |
+
|
| 70 |
+
response_data["msg"] = "Item created successfully."
|
| 71 |
+
response_data["data"] = ItemInDB(**dict(record))
|
| 72 |
+
return response_data
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
response_data["success"] = False
|
| 76 |
+
response_data["msg"] = f"Database INSERT error: {e!r}"
|
| 77 |
+
return response_data
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# ----------------------------------------------------
|
| 81 |
+
# 2. READ (์ ์ฒด ์กฐํ ๋ฐ ๋จ์ผ ์กฐํ) - GET /crud/t_test1/?item_id={id} ๋๋ /crud/t_test1/
|
| 82 |
+
# ----------------------------------------------------
|
| 83 |
+
@router.get("/", response_model=APIResponse)
|
| 84 |
+
async def read_t_test1_items(
|
| 85 |
+
# item_id๊ฐ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
|
| 86 |
+
item_id: Optional[int] = None,
|
| 87 |
+
conn: asyncpg.Connection = Depends(get_db_connection)
|
| 88 |
+
):
|
| 89 |
+
"""
|
| 90 |
+
item_id๊ฐ ์์ผ๋ฉด ๋จ์ผ ์์ดํ
์, ์์ผ๋ฉด ์ ์ฒด ๋ฆฌ์คํธ๋ฅผ ์กฐํํฉ๋๋ค (SELECT).
|
| 91 |
+
"""
|
| 92 |
+
response_data = {"success": True, "msg": "", "data": None}
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
if item_id:
|
| 96 |
+
# ๋จ์ผ ์กฐํ (WHERE ID)
|
| 97 |
+
query = "SELECT id, name, price, created_at FROM t_test1 WHERE id = $1;"
|
| 98 |
+
record = await conn.fetchrow(query, item_id)
|
| 99 |
+
|
| 100 |
+
if record is None:
|
| 101 |
+
response_data["success"] = False
|
| 102 |
+
response_data["msg"] = f"Read failed: Item with ID {item_id} not found."
|
| 103 |
+
return response_data
|
| 104 |
+
|
| 105 |
+
response_data["msg"] = "Item read successfully."
|
| 106 |
+
response_data["data"] = ItemInDB(**dict(record))
|
| 107 |
+
else:
|
| 108 |
+
# ์ ์ฒด ์กฐํ (SELECT *)
|
| 109 |
+
query = "SELECT id, name, price, created_at FROM t_test1 ORDER BY id;"
|
| 110 |
+
records = await conn.fetch(query)
|
| 111 |
+
|
| 112 |
+
response_data["data"] = [ItemInDB(**dict(record)) for record in records]
|
| 113 |
+
response_data["msg"] = f"Found {len(records)} items."
|
| 114 |
+
|
| 115 |
+
return response_data
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
response_data["success"] = False
|
| 119 |
+
response_data["msg"] = f"Database SELECT error: {e!r}"
|
| 120 |
+
return response_data
|
| 121 |
+
|
| 122 |
+
# ----------------------------------------------------
|
| 123 |
+
# 3. UPDATE (๋ฐ์ดํฐ ์
๋ฐ์ดํธ) - PUT /crud/t_test1/?item_id={id}
|
| 124 |
+
# ----------------------------------------------------
|
| 125 |
+
@router.put("/", response_model=APIResponse)
|
| 126 |
+
async def update_t_test1_item(
|
| 127 |
+
# item_id๊ฐ ํ์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
|
| 128 |
+
item_id: int,
|
| 129 |
+
item: ItemUpdate,
|
| 130 |
+
conn: asyncpg.Connection = Depends(get_db_connection)
|
| 131 |
+
):
|
| 132 |
+
"""
|
| 133 |
+
ํน์ ID์ ์์ดํ
์ ๋ณด๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค (UPDATE).
|
| 134 |
+
"""
|
| 135 |
+
response_data = {"success": True, "msg": "", "data": None}
|
| 136 |
+
|
| 137 |
+
# 1. ํ์ฌ ์์ดํ
์กด์ฌ ์ฌ๋ถ ํ์ธ (์
๋ฐ์ดํธ ์ ํ์)
|
| 138 |
+
select_query = "SELECT id FROM t_test1 WHERE id = $1;"
|
| 139 |
+
current_record = await conn.fetchrow(select_query, item_id)
|
| 140 |
+
|
| 141 |
+
if current_record is None:
|
| 142 |
+
response_data["success"] = False
|
| 143 |
+
response_data["msg"] = f"Update failed: Item with ID {item_id} not found."
|
| 144 |
+
return response_data
|
| 145 |
+
|
| 146 |
+
# 2. ๋์ SQL ์ฟผ๋ฆฌ ์์ฑ
|
| 147 |
+
updates = []
|
| 148 |
+
params = []
|
| 149 |
+
param_count = 1
|
| 150 |
+
|
| 151 |
+
if item.name is not None:
|
| 152 |
+
updates.append(f"name = ${param_count}")
|
| 153 |
+
params.append(item.name)
|
| 154 |
+
param_count += 1
|
| 155 |
+
|
| 156 |
+
if item.price is not None:
|
| 157 |
+
updates.append(f"price = ${param_count}")
|
| 158 |
+
params.append(item.price)
|
| 159 |
+
param_count += 1
|
| 160 |
+
|
| 161 |
+
if not updates:
|
| 162 |
+
response_data["msg"] = "No updates provided. Returning current item data."
|
| 163 |
+
# ์
๋ฐ์ดํธํ ๋ด์ฉ์ด ์๋ ๊ฒฝ์ฐ, ํ์ฌ ์์ดํ
์ ์กฐํํ์ฌ ๋ฐํ
|
| 164 |
+
return await read_t_test1_items(item_id=item_id, conn=conn)
|
| 165 |
+
|
| 166 |
+
# 3. ์ต์ข
UPDATE ์ฟผ๋ฆฌ ์คํ
|
| 167 |
+
set_clause = ", ".join(updates)
|
| 168 |
+
where_param_index = param_count
|
| 169 |
+
|
| 170 |
+
update_query = f"""
|
| 171 |
+
UPDATE t_test1
|
| 172 |
+
SET {set_clause}
|
| 173 |
+
WHERE id = ${where_param_index}
|
| 174 |
+
RETURNING id, name, price, created_at;
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
params.append(item_id) # WHERE ์ ์ ์ฌ์ฉํ ID ์ถ๊ฐ
|
| 178 |
+
|
| 179 |
+
try:
|
| 180 |
+
updated_record = await conn.fetchrow(update_query, *params)
|
| 181 |
+
|
| 182 |
+
if updated_record is None:
|
| 183 |
+
response_data["success"] = False
|
| 184 |
+
response_data["msg"] = "Update failed: No record was modified."
|
| 185 |
+
return response_data
|
| 186 |
+
|
| 187 |
+
response_data["msg"] = "Item updated successfully."
|
| 188 |
+
response_data["data"] = ItemInDB(**dict(updated_record))
|
| 189 |
+
return response_data
|
| 190 |
+
|
| 191 |
+
except Exception as e:
|
| 192 |
+
response_data["success"] = False
|
| 193 |
+
response_data["msg"] = f"Database UPDATE error: {e!r}"
|
| 194 |
+
return response_data
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
# ----------------------------------------------------
|
| 198 |
+
# 4. DELETE (๋ฐ์ดํฐ ์ญ์ ) - DELETE /crud/t_test1/?item_id={id}
|
| 199 |
+
# ----------------------------------------------------
|
| 200 |
+
@router.delete("/", response_model=APIResponse)
|
| 201 |
+
async def delete_t_test1_item(
|
| 202 |
+
# item_id๊ฐ ํ์ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
|
| 203 |
+
item_id: int,
|
| 204 |
+
conn: asyncpg.Connection = Depends(get_db_connection)
|
| 205 |
+
):
|
| 206 |
+
"""
|
| 207 |
+
ํน์ ID์ ์์ดํ
์ ์ญ์ ํฉ๋๋ค (DELETE).
|
| 208 |
+
"""
|
| 209 |
+
response_data = {"success": True, "msg": "", "data": None}
|
| 210 |
+
|
| 211 |
+
query = "DELETE FROM t_test1 WHERE id = $1;"
|
| 212 |
+
|
| 213 |
+
try:
|
| 214 |
+
command_tag = await conn.execute(query, item_id)
|
| 215 |
+
|
| 216 |
+
deleted_rows = int(command_tag.split()[-1])
|
| 217 |
+
|
| 218 |
+
if deleted_rows == 0:
|
| 219 |
+
response_data["success"] = False
|
| 220 |
+
response_data["msg"] = f"Delete failed: Item with ID {item_id} not found."
|
| 221 |
+
return response_data
|
| 222 |
+
|
| 223 |
+
response_data["msg"] = f"Item with ID {item_id} deleted successfully."
|
| 224 |
+
return response_data
|
| 225 |
+
|
| 226 |
+
except Exception as e:
|
| 227 |
+
response_data["success"] = False
|
| 228 |
+
response_data["msg"] = f"Database DELETE error: {e!r}"
|
| 229 |
+
return response_data
|