Spaces:
Sleeping
Sleeping
ktsn-ud commited on
Commit ·
b1ae71d
1
Parent(s): 0349071
統合されたテキスト情報の取得方法を実装し、SQLクエリとリポジトリのロジックを更新
Browse files- SQL_IMPLEMENTATION.md +9 -0
- app/repositories/projects_repository.py +26 -8
- app/repositories/query.sql +0 -26
SQL_IMPLEMENTATION.md
CHANGED
|
@@ -75,3 +75,12 @@ model ProjectMaterialUpdateRequest {
|
|
| 75 |
}
|
| 76 |
```
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
```
|
| 77 |
|
| 78 |
+
# 修正
|
| 79 |
+
## query.sqlの変更
|
| 80 |
+
現状,最終的にSQLによって得られるカラムのうち,「description」「prSummary」「prDetail」「remark」ですが,これをProjectTextテーブルから取ってきています。現状のようにdescriptionカラムとdescriptionUpdatesカラムの2種類にするのではなく,ProjectTextテーブルとProjectMaterialUpdateRequestテーブルを左結合し,ProjectMaterialUpdateRequestに対応するエントリがない場合はupdateStatusをnullにしてSQLの結果としてください。つまり,descriptionカラムとdescriptionUpdatesカラムを統合し,descriptionUpdatesカラム1本にしてください。prSummary, prDetail, remarkでも同様です。
|
| 81 |
+
|
| 82 |
+
## projects_repository.pyの変更
|
| 83 |
+
`app/repositories/projects_repository.py`内の`_transform_row`においては,以下の優先順位で採用してdescription, prSummary, ...としてください。
|
| 84 |
+
1. updateStatus="APPROVED"のうち最新のもの
|
| 85 |
+
2. updateStatus=nullで最新のもの
|
| 86 |
+
なお,方針としては,created_atの降順で並び替え,順に見ていき見つかれば採用すると良いでしょう。
|
app/repositories/projects_repository.py
CHANGED
|
@@ -80,24 +80,42 @@ class ProjectsRepository:
|
|
| 80 |
items.append(item)
|
| 81 |
return items
|
| 82 |
|
| 83 |
-
def
|
| 84 |
updates = sorted(
|
| 85 |
_coerce_dict_list(row.get(updates_key)),
|
| 86 |
-
key=lambda item: item.get("createdAt") or "",
|
| 87 |
reverse=True,
|
| 88 |
)
|
|
|
|
| 89 |
for update in updates:
|
| 90 |
status = update.get("updateStatus")
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
content = update.get("content")
|
| 93 |
if isinstance(content, str):
|
| 94 |
return content
|
| 95 |
-
return row.get(default_key)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
return Project(
|
| 103 |
projectId=row["projectId"],
|
|
|
|
| 80 |
items.append(item)
|
| 81 |
return items
|
| 82 |
|
| 83 |
+
def _resolve_text_from_updates(updates_key: str) -> str | None:
|
| 84 |
updates = sorted(
|
| 85 |
_coerce_dict_list(row.get(updates_key)),
|
| 86 |
+
key=lambda item: str(item.get("createdAt") or ""),
|
| 87 |
reverse=True,
|
| 88 |
)
|
| 89 |
+
|
| 90 |
for update in updates:
|
| 91 |
status = update.get("updateStatus")
|
| 92 |
+
normalized_status = (
|
| 93 |
+
status.strip().upper() if isinstance(status, str) else None
|
| 94 |
+
)
|
| 95 |
+
if normalized_status == "APPROVED":
|
| 96 |
content = update.get("content")
|
| 97 |
if isinstance(content, str):
|
| 98 |
return content
|
|
|
|
| 99 |
|
| 100 |
+
for update in updates:
|
| 101 |
+
status = update.get("updateStatus")
|
| 102 |
+
if status is None:
|
| 103 |
+
content = update.get("content")
|
| 104 |
+
if isinstance(content, str):
|
| 105 |
+
return content
|
| 106 |
+
if isinstance(status, str):
|
| 107 |
+
stripped = status.strip().upper()
|
| 108 |
+
if stripped in ("", "NULL"):
|
| 109 |
+
content = update.get("content")
|
| 110 |
+
if isinstance(content, str):
|
| 111 |
+
return content
|
| 112 |
+
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
description_value = _resolve_text_from_updates("descriptionUpdates")
|
| 116 |
+
pr_summary_value = _resolve_text_from_updates("prSummaryUpdates")
|
| 117 |
+
pr_detail_value = _resolve_text_from_updates("prDetailUpdates")
|
| 118 |
+
remark_value = _resolve_text_from_updates("remarkUpdates")
|
| 119 |
|
| 120 |
return Project(
|
| 121 |
projectId=row["projectId"],
|
app/repositories/query.sql
CHANGED
|
@@ -9,10 +9,6 @@ SELECT
|
|
| 9 |
cp.`secondDay` AS `day2`,
|
| 10 |
cp.`thirdDay` AS `day3`,
|
| 11 |
cp.location AS `location`,
|
| 12 |
-
texts.description AS `description`,
|
| 13 |
-
texts.prSummary AS `prSummary`,
|
| 14 |
-
texts.prDetail AS `prDetail`,
|
| 15 |
-
texts.remark AS `remark`,
|
| 16 |
COALESCE(textUpdates.descriptionUpdates, '[]') AS `descriptionUpdates`,
|
| 17 |
COALESCE(textUpdates.prSummaryUpdates, '[]') AS `prSummaryUpdates`,
|
| 18 |
COALESCE(textUpdates.prDetailUpdates, '[]') AS `prDetailUpdates`,
|
|
@@ -27,28 +23,6 @@ FROM `CircleProject` AS cp
|
|
| 27 |
JOIN `Circle` AS c
|
| 28 |
ON c.`circleId` = cp.`circleId`
|
| 29 |
|
| 30 |
-
-- テキスト情報を結合
|
| 31 |
-
LEFT JOIN (
|
| 32 |
-
SELECT
|
| 33 |
-
pt.circleProjectId,
|
| 34 |
-
MAX(CASE WHEN pt.textType = 'DESCRIPTION' THEN pt.content END) AS description,
|
| 35 |
-
MAX(CASE WHEN pt.textType = 'PRSUMMARY' THEN pt.content END) AS prSummary,
|
| 36 |
-
MAX(CASE WHEN pt.textType = 'PRDETAIL' THEN pt.content END) AS prDetail,
|
| 37 |
-
MAX(CASE WHEN pt.textType = 'REMARK' THEN pt.content END) AS remark
|
| 38 |
-
FROM (
|
| 39 |
-
SELECT
|
| 40 |
-
*,
|
| 41 |
-
ROW_NUMBER() OVER (
|
| 42 |
-
PARTITION BY circleProjectId, textType
|
| 43 |
-
ORDER BY updatedAt DESC
|
| 44 |
-
) AS rn
|
| 45 |
-
FROM ProjectText
|
| 46 |
-
) pt
|
| 47 |
-
WHERE pt.rn = 1
|
| 48 |
-
GROUP BY pt.circleProjectId
|
| 49 |
-
) AS texts
|
| 50 |
-
ON texts.circleProjectId = cp.id
|
| 51 |
-
|
| 52 |
-- テキストの更新履歴を結合
|
| 53 |
LEFT JOIN (
|
| 54 |
SELECT
|
|
|
|
| 9 |
cp.`secondDay` AS `day2`,
|
| 10 |
cp.`thirdDay` AS `day3`,
|
| 11 |
cp.location AS `location`,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
COALESCE(textUpdates.descriptionUpdates, '[]') AS `descriptionUpdates`,
|
| 13 |
COALESCE(textUpdates.prSummaryUpdates, '[]') AS `prSummaryUpdates`,
|
| 14 |
COALESCE(textUpdates.prDetailUpdates, '[]') AS `prDetailUpdates`,
|
|
|
|
| 23 |
JOIN `Circle` AS c
|
| 24 |
ON c.`circleId` = cp.`circleId`
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
-- テキストの更新履歴を結合
|
| 27 |
LEFT JOIN (
|
| 28 |
SELECT
|