o2o-site-AEO/solution/backend/crud/place_content_crud.py
Mina Choi 11d30bb3d1 [chore] solution,admin,ontology: 코드 주석을 한 줄로 — 히스토리 주석 삭제
여러 줄 주석이 설명보다 경위(예전·실측·지적)를 적고 있어 읽는 사람이 결론을 찾기 어려웠다.

- ts·tsx·js·mjs·css·py 478개: 여러 줄 주석은 첫 문장 한 줄로, 과거형·날짜 문장은 삭제
- 주석 위치는 TypeScript 파서·파이썬 tokenize/ast 로 찾는다 — 문자열 안의 # · /* 는 건드리지 않는다
- eslint·ts·noqa·type: ignore 같은 지시 주석은 그대로 둔다

파이썬 275개 정리 전후 AST 동일, TS 298개 주석 뺀 토큰 동일(빈 JSX 주석 10곳만 차이).
site·frontend·admin tsc, site vitest 105 passed

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 16:05:19 +09:00

121 lines
5.0 KiB
Python

from sqlalchemy import and_, select, update
from sqlalchemy.dialects.postgresql import insert as pg_insert
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import area_contents, place_area_refs
from common.utils.gtime import GTime
class PlaceContentCRUD:
"""업장 주변의 지역 콘텐츠."""
async def list_by_place(self, db, place_id, *, include_hidden: bool = True):
"""이 업장 주변의 콘텐츠."""
conds = [
place_area_refs.place_id == place_id,
place_area_refs.deleted == False, # noqa: E712
area_contents.deleted == False, # noqa: E712
]
if not include_hidden:
conds.append(place_area_refs.hidden == False) # noqa: E712
return await DB_SESSION_MNG.execute(
db,
select(
area_contents.local_content_id,
area_contents.content_type,
area_contents.source,
area_contents.external_id,
area_contents.title,
area_contents.body,
area_contents.latitude,
area_contents.longitude,
area_contents.display_end_at,
# 이름을 옛 컬럼과 맞춘다 — 읽는 쪽(snapshot)이 행을 그대로 쓰던 모양이다.
place_area_refs.distance_m.label("distance_m"),
place_area_refs.hidden.label("hidden"),
)
.join(area_contents, area_contents.local_content_id == place_area_refs.local_content_id)
.where(and_(*conds))
.order_by(
area_contents.content_type.asc(),
place_area_refs.distance_m.asc(),
),
)
async def upsert_content(self, db, values: dict):
"""공용 콘텐츠 한 건."""
stmt = pg_insert(area_contents).values(**values)
stmt = stmt.on_conflict_do_update(
index_elements=[area_contents.source, area_contents.external_id],
index_where=and_(
area_contents.deleted == False, # noqa: E712
area_contents.external_id.isnot(None),
),
set_={
"title": stmt.excluded.title,
"body": stmt.excluded.body,
"latitude": stmt.excluded.latitude,
"longitude": stmt.excluded.longitude,
"region_code": stmt.excluded.region_code,
"display_end_at": stmt.excluded.display_end_at,
"collected_at": stmt.excluded.collected_at,
"updated_at": GTime.UTC(),
},
)
return await DB_SESSION_MNG.add(db, stmt)
async def find_content_id(self, db, source: int, external_id: str):
"""방금 upsert 한 공용 콘텐츠의 id."""
return await DB_SESSION_MNG.execute(
db,
select(area_contents.local_content_id).where(
area_contents.source == source,
area_contents.external_id == external_id,
area_contents.deleted == False, # noqa: E712
).limit(1),
)
async def upsert_ref(self, db, place_id, local_content_id, distance_m):
"""업장 ↔ 콘텐츠 관계."""
stmt = pg_insert(place_area_refs).values(
place_id=place_id, local_content_id=local_content_id, distance_m=distance_m, deleted=False,
)
stmt = stmt.on_conflict_do_update(
index_elements=[place_area_refs.place_id, place_area_refs.local_content_id],
set_={"distance_m": stmt.excluded.distance_m, "deleted": False, "updated_at": GTime.UTC()},
)
return await DB_SESSION_MNG.add(db, stmt)
async def soft_delete_missing(self, db, place_id, keep_ids: set):
"""이번 응답에 없는 **관계**를 끊는다."""
err, rows = await DB_SESSION_MNG.execute(
db,
select(place_area_refs.local_content_id).where(
place_area_refs.place_id == place_id,
place_area_refs.deleted == False, # noqa: E712
),
)
# 단일 컬럼 SELECT 라 행이 스칼라로 온다.
gone = [r for r in (rows or []) if r not in keep_ids]
if not gone:
return err, 0
return await DB_SESSION_MNG.add_with_rowcount(
db,
update(place_area_refs)
.where(place_area_refs.place_id == place_id, place_area_refs.local_content_id.in_(gone))
.values(deleted=True, updated_at=GTime.UTC()),
)
async def set_hidden(self, db, place_id, local_content_id, hidden: bool):
"""이 업장에서만 숨긴다."""
return await DB_SESSION_MNG.add_with_rowcount(
db,
update(place_area_refs)
.where(
place_area_refs.place_id == place_id,
place_area_refs.local_content_id == local_content_id,
place_area_refs.deleted == False, # noqa: E712
)
.values(hidden=hidden, updated_at=GTime.UTC()),
)