부동산 청약 단지 관리 API 추가

- realestate_complexes 테이블 추가 (lotto.db)
- CRUD 엔드포인트 4개: GET/POST /api/realestate/complexes, PUT/DELETE /api/realestate/complexes/:id
- status: 청약예정|청약중|결과발표|완료, priority: high|normal|low 검증

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-16 01:23:28 +09:00
parent 197d451d5f
commit 2926770d6f
2 changed files with 242 additions and 0 deletions

View File

@@ -181,6 +181,38 @@ def init_db() -> None:
"CREATE INDEX IF NOT EXISTS idx_blog_date ON blog_posts(date DESC);"
)
# ── realestate_complexes 테이블 ────────────────────────────────────────
conn.execute(
"""
CREATE TABLE IF NOT EXISTS realestate_complexes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
address TEXT NOT NULL DEFAULT '',
lat REAL,
lng REAL,
units INTEGER,
types TEXT NOT NULL DEFAULT '[]',
avg_price_per_pyeong INTEGER,
subscription_start TEXT,
subscription_end TEXT,
result_date TEXT,
status TEXT NOT NULL DEFAULT '청약예정'
CHECK(status IN ('청약예정','청약중','결과발표','완료')),
priority TEXT NOT NULL DEFAULT 'normal'
CHECK(priority IN ('high','normal','low')),
tags TEXT NOT NULL DEFAULT '[]',
naver_url TEXT NOT NULL DEFAULT '',
floor_plan_url TEXT NOT NULL DEFAULT '',
memo TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
);
"""
)
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_realestate_status ON realestate_complexes(status);"
)
# ── todos CRUD ───────────────────────────────────────────────────────────────
@@ -667,3 +699,132 @@ def get_simulation_candidates(run_id: int, limit: int = 100) -> List[Dict[str, A
for r in rows
]
# ── realestate_complexes CRUD ─────────────────────────────────────────────────
def _complex_row_to_dict(r) -> Dict[str, Any]:
return {
"id": r["id"],
"name": r["name"],
"address": r["address"],
"lat": r["lat"],
"lng": r["lng"],
"units": r["units"],
"types": json.loads(r["types"]) if r["types"] else [],
"avgPricePerPyeong": r["avg_price_per_pyeong"],
"subscriptionStart": r["subscription_start"],
"subscriptionEnd": r["subscription_end"],
"resultDate": r["result_date"],
"status": r["status"],
"priority": r["priority"],
"tags": json.loads(r["tags"]) if r["tags"] else [],
"naverUrl": r["naver_url"],
"floorPlanUrl": r["floor_plan_url"],
"memo": r["memo"],
"created_at": r["created_at"],
"updated_at": r["updated_at"],
}
def get_all_complexes() -> List[Dict[str, Any]]:
with _conn() as conn:
rows = conn.execute(
"SELECT * FROM realestate_complexes ORDER BY id DESC"
).fetchall()
return [_complex_row_to_dict(r) for r in rows]
def get_complex(complex_id: int) -> Optional[Dict[str, Any]]:
with _conn() as conn:
r = conn.execute(
"SELECT * FROM realestate_complexes WHERE id = ?", (complex_id,)
).fetchone()
return _complex_row_to_dict(r) if r else None
def create_complex(data: Dict[str, Any]) -> Dict[str, Any]:
with _conn() as conn:
conn.execute(
"""
INSERT INTO realestate_complexes
(name, address, lat, lng, units, types, avg_price_per_pyeong,
subscription_start, subscription_end, result_date,
status, priority, tags, naver_url, floor_plan_url, memo)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
data["name"],
data.get("address", ""),
data.get("lat"),
data.get("lng"),
data.get("units"),
json.dumps(data.get("types", [])),
data.get("avgPricePerPyeong"),
data.get("subscriptionStart"),
data.get("subscriptionEnd"),
data.get("resultDate"),
data.get("status", "청약예정"),
data.get("priority", "normal"),
json.dumps(data.get("tags", [])),
data.get("naverUrl", ""),
data.get("floorPlanUrl", ""),
data.get("memo", ""),
),
)
row = conn.execute(
"SELECT * FROM realestate_complexes WHERE rowid = last_insert_rowid()"
).fetchone()
return _complex_row_to_dict(row)
def update_complex(complex_id: int, data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
field_map = {
"name": "name",
"address": "address",
"lat": "lat",
"lng": "lng",
"units": "units",
"avgPricePerPyeong": "avg_price_per_pyeong",
"subscriptionStart": "subscription_start",
"subscriptionEnd": "subscription_end",
"resultDate": "result_date",
"status": "status",
"priority": "priority",
"naverUrl": "naver_url",
"floorPlanUrl": "floor_plan_url",
"memo": "memo",
}
json_fields = {"types", "tags"}
updates: Dict[str, Any] = {}
for camel, snake in field_map.items():
if camel in data:
updates[snake] = data[camel]
for f in json_fields:
if f in data:
updates[f] = json.dumps(data[f])
if not updates:
return get_complex(complex_id)
set_clauses = ", ".join(f"{k} = ?" for k in updates)
set_clauses += ", updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')"
args = list(updates.values()) + [complex_id]
with _conn() as conn:
conn.execute(
f"UPDATE realestate_complexes SET {set_clauses} WHERE id = ?", args
)
row = conn.execute(
"SELECT * FROM realestate_complexes WHERE id = ?", (complex_id,)
).fetchone()
return _complex_row_to_dict(row) if row else None
def delete_complex(complex_id: int) -> bool:
with _conn() as conn:
cur = conn.execute(
"DELETE FROM realestate_complexes WHERE id = ?", (complex_id,)
)
return cur.rowcount > 0