ZHIWEI666 commited on
Commit
71ce6d2
·
verified ·
1 Parent(s): 90d7999

Upload 2 files

Browse files
Files changed (2) hide show
  1. models.py +9 -1
  2. router_items.py +52 -3
models.py CHANGED
@@ -68,4 +68,12 @@ class PrivacySettings(BaseModel):
68
  follows: bool
69
  likes: bool
70
  favorites: bool
71
- downloads: bool
 
 
 
 
 
 
 
 
 
68
  follows: bool
69
  likes: bool
70
  favorites: bool
71
+ downloads: bool
72
+
73
+ class ItemUpdate(BaseModel):
74
+ title: Optional[str] = None
75
+ shortDesc: Optional[str] = None
76
+ fullDesc: Optional[str] = None
77
+ link: Optional[str] = None
78
+ coverUrl: Optional[str] = None
79
+ price: Optional[float] = None
router_items.py CHANGED
@@ -1,9 +1,10 @@
1
  # router_items.py
2
- from fastapi import APIRouter
3
  import time
4
  import uuid
5
  import 数据库连接 as db
6
- from models import ItemCreate
 
7
 
8
  router = APIRouter()
9
 
@@ -54,4 +55,52 @@ async def create_item(item: ItemCreate):
54
  }
55
  items_db.insert(0, new_item)
56
  db.save_data("items.json", items_db)
57
- return {"status": "success", "data": new_item}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # router_items.py
2
+ from fastapi import APIRouter, HTTPException
3
  import time
4
  import uuid
5
  import 数据库连接 as db
6
+ # 【核心修改】:在顶部引入我们刚才新增的 ItemUpdate 模型
7
+ from models import ItemCreate, ItemUpdate
8
 
9
  router = APIRouter()
10
 
 
55
  }
56
  items_db.insert(0, new_item)
57
  db.save_data("items.json", items_db)
58
+ return {"status": "success", "data": new_item}
59
+
60
+ # =========================================================
61
+ # 【核心新增】:以下为修改与删除相关接口
62
+ # =========================================================
63
+
64
+ @router.put("/api/items/{item_id}")
65
+ async def update_item(item_id: str, update_data: ItemUpdate, author: str):
66
+ items_db = db.load_data("items.json", default_data=[])
67
+ for item in items_db:
68
+ if item["id"] == item_id:
69
+ # 鉴权:只有作者本人可以修改
70
+ if item.get("author") != author:
71
+ raise HTTPException(status_code=403, detail="无权修改他人发布的内容")
72
+
73
+ # 只更新存在的字段,不影响浏览量点赞等数据
74
+ if update_data.title is not None: item["title"] = update_data.title
75
+ if update_data.shortDesc is not None: item["shortDesc"] = update_data.shortDesc
76
+ if update_data.fullDesc is not None: item["fullDesc"] = update_data.fullDesc
77
+ if update_data.link is not None: item["link"] = update_data.link
78
+ if update_data.coverUrl is not None: item["coverUrl"] = update_data.coverUrl
79
+ if update_data.price is not None: item["price"] = update_data.price
80
+
81
+ db.save_data("items.json", items_db)
82
+ return {"status": "success"}
83
+
84
+ raise HTTPException(status_code=404, detail="找不到该内容记录")
85
+
86
+ @router.delete("/api/items/{item_id}")
87
+ async def delete_item(item_id: str, author: str):
88
+ items_db = db.load_data("items.json", default_data=[])
89
+ target_idx = next((i for i, item in enumerate(items_db) if item["id"] == item_id), None)
90
+
91
+ if target_idx is None:
92
+ raise HTTPException(status_code=404, detail="找不到该内容记录")
93
+ if items_db[target_idx].get("author") != author:
94
+ raise HTTPException(status_code=403, detail="无权删除他人发布的内容")
95
+
96
+ # 1. 删除发布的内容
97
+ items_db.pop(target_idx)
98
+ db.save_data("items.json", items_db)
99
+
100
+ # 2. 同步清理该内容下的所有关联评论记录
101
+ comments_db = db.load_data("comments.json", default_data={})
102
+ if item_id in comments_db:
103
+ del comments_db[item_id]
104
+ db.save_data("comments.json", comments_db)
105
+
106
+ return {"status": "success"}