|
10 | 10 |
|
11 | 11 | from core.database import SessionLocal |
12 | 12 | from models.article import Article |
| 13 | +from schemas.articles import ArticleUpdate |
13 | 14 |
|
14 | 15 |
|
15 | 16 | def list_articles_for_user(db: Session, user_id: int) -> list[Article]: |
@@ -40,6 +41,10 @@ def article_to_dict(article: Article) -> dict[str, Any]: |
40 | 41 | "set_code": article.set_code, |
41 | 42 | "card_number": article.card_number, |
42 | 43 | "condition": article.condition, |
| 44 | + "is_graded": bool(article.is_graded), |
| 45 | + "graded_grader_value_id": article.graded_grader_value_id, |
| 46 | + "graded_grade_value_id": article.graded_grade_value_id, |
| 47 | + "graded_cert_number": article.graded_cert_number, |
43 | 48 | "purchase_price": float(article.purchase_price), |
44 | 49 | "sell_price": float(article.sell_price) if article.sell_price is not None else None, |
45 | 50 | "sold_price": float(article.sold_price) if article.sold_price is not None else None, |
@@ -105,31 +110,41 @@ def delete_articles_by_ids(db: Session, user_id: int, ids: list[int]) -> int: |
105 | 110 | return int(deleted) |
106 | 111 |
|
107 | 112 |
|
108 | | -def update_article_fields( |
109 | | - article: Article, |
110 | | - *, |
111 | | - title: str | None = None, |
112 | | - description: str | None = None, |
113 | | - pokemon_name: str | None = None, |
114 | | - set_code: str | None = None, |
115 | | - card_number: str | None = None, |
116 | | - condition: str | None = None, |
117 | | - purchase_price: Decimal | None = None, |
118 | | - sell_price: Decimal | None = None, |
119 | | -) -> None: |
120 | | - if title is not None: |
121 | | - article.title = title |
122 | | - if description is not None: |
123 | | - article.description = description |
124 | | - if pokemon_name is not None: |
125 | | - article.pokemon_name = pokemon_name |
126 | | - if set_code is not None: |
127 | | - article.set_code = set_code |
128 | | - if card_number is not None: |
129 | | - article.card_number = card_number |
130 | | - if condition is not None: |
131 | | - article.condition = condition |
132 | | - if purchase_price is not None: |
133 | | - article.purchase_price = purchase_price |
134 | | - if sell_price is not None: |
135 | | - article.sell_price = sell_price |
| 113 | +def update_article_from_body(article: Article, body: ArticleUpdate) -> None: |
| 114 | + """Apply a partial update from ``ArticleUpdate`` (only set fields are changed).""" |
| 115 | + data = body.model_dump(exclude_unset=True) |
| 116 | + if "is_graded" in data: |
| 117 | + article.is_graded = bool(data["is_graded"]) |
| 118 | + if not article.is_graded: |
| 119 | + article.graded_grader_value_id = None |
| 120 | + article.graded_grade_value_id = None |
| 121 | + article.graded_cert_number = None |
| 122 | + if "title" in data and data["title"] is not None: |
| 123 | + article.title = data["title"] |
| 124 | + if "description" in data and data["description"] is not None: |
| 125 | + article.description = data["description"] |
| 126 | + if "pokemon_name" in data: |
| 127 | + article.pokemon_name = data["pokemon_name"] |
| 128 | + if "set_code" in data: |
| 129 | + article.set_code = data["set_code"] |
| 130 | + if "card_number" in data: |
| 131 | + article.card_number = data["card_number"] |
| 132 | + if "condition" in data and data["condition"] is not None: |
| 133 | + article.condition = data["condition"] |
| 134 | + if "purchase_price" in data and data["purchase_price"] is not None: |
| 135 | + article.purchase_price = data["purchase_price"] |
| 136 | + if "sell_price" in data: |
| 137 | + article.sell_price = data["sell_price"] |
| 138 | + if "graded_grader_value_id" in data: |
| 139 | + gid = data["graded_grader_value_id"] |
| 140 | + article.graded_grader_value_id = (gid.strip() if isinstance(gid, str) else None) or None |
| 141 | + if "graded_grade_value_id" in data: |
| 142 | + tid = data["graded_grade_value_id"] |
| 143 | + article.graded_grade_value_id = (tid.strip() if isinstance(tid, str) else None) or None |
| 144 | + if "graded_cert_number" in data: |
| 145 | + cert = data["graded_cert_number"] |
| 146 | + if cert is None: |
| 147 | + article.graded_cert_number = None |
| 148 | + else: |
| 149 | + c = str(cert).strip()[:30] |
| 150 | + article.graded_cert_number = c or None |
0 commit comments