add: endpoints for cancel and edit price item
All checks were successful
Build and Deploy / deploy (push) Successful in 22s
All checks were successful
Build and Deploy / deploy (push) Successful in 22s
This commit is contained in:
@ -61,3 +61,22 @@ async def submit_item_details(data: dict):
|
|||||||
"""Получить подробные данные о предмете"""
|
"""Получить подробные данные о предмете"""
|
||||||
from app.services.marketplace import MarketplaceService
|
from app.services.marketplace import MarketplaceService
|
||||||
return await MarketplaceService().update_item_details(data["operation_id"], data["item_data"])
|
return await MarketplaceService().update_item_details(data["operation_id"], data["item_data"])
|
||||||
|
|
||||||
|
@router.delete("/items/{item_id}")
|
||||||
|
async def cancel_item_sale(
|
||||||
|
item_id: str,
|
||||||
|
username: str = Query(...)
|
||||||
|
):
|
||||||
|
"""Снять предмет с продажи"""
|
||||||
|
from app.services.marketplace import MarketplaceService
|
||||||
|
return await MarketplaceService().cancel_item_sale(username, item_id)
|
||||||
|
|
||||||
|
@router.put("/items/{item_id}/price")
|
||||||
|
async def update_item_price(
|
||||||
|
item_id: str,
|
||||||
|
new_price: int = Body(..., gt=0),
|
||||||
|
username: str = Body(...)
|
||||||
|
):
|
||||||
|
"""Обновить цену предмета на торговой площадке"""
|
||||||
|
from app.services.marketplace import MarketplaceService
|
||||||
|
return await MarketplaceService().update_item_price(username, item_id, new_price)
|
||||||
|
@ -211,3 +211,68 @@ class MarketplaceService:
|
|||||||
"operation_id": operation_id,
|
"operation_id": operation_id,
|
||||||
"message": "Покупка в обработке. Предмет будет добавлен в ваш инвентарь."
|
"message": "Покупка в обработке. Предмет будет добавлен в ваш инвентарь."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async def cancel_item_sale(self, username: str, item_id: str):
|
||||||
|
"""Снять предмет с продажи"""
|
||||||
|
# Находим предмет
|
||||||
|
item = await marketplace_collection.find_one({"id": item_id})
|
||||||
|
if not item:
|
||||||
|
raise HTTPException(status_code=404, detail="Предмет не найден")
|
||||||
|
|
||||||
|
# Проверяем, что пользователь является владельцем предмета
|
||||||
|
if item["seller_name"] != username:
|
||||||
|
raise HTTPException(status_code=403, detail="Вы не можете снять с продажи чужой предмет")
|
||||||
|
|
||||||
|
# Создаем операцию возврата предмета
|
||||||
|
operation_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
operation = {
|
||||||
|
"id": operation_id,
|
||||||
|
"type": "cancel_sale",
|
||||||
|
"player_name": username,
|
||||||
|
"item_id": item_id,
|
||||||
|
"item_data": item["item_data"],
|
||||||
|
"server_ip": item["server_ip"],
|
||||||
|
"status": "pending",
|
||||||
|
"created_at": datetime.utcnow()
|
||||||
|
}
|
||||||
|
|
||||||
|
await marketplace_operations.insert_one(operation)
|
||||||
|
|
||||||
|
# Удаляем предмет с торговой площадки
|
||||||
|
await marketplace_collection.delete_one({"id": item_id})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "pending",
|
||||||
|
"operation_id": operation_id,
|
||||||
|
"message": "Предмет снят с продажи и будет возвращен в ваш инвентарь"
|
||||||
|
}
|
||||||
|
|
||||||
|
async def update_item_price(self, username: str, item_id: str, new_price: int):
|
||||||
|
"""Обновить цену предмета на торговой площадке"""
|
||||||
|
# Находим предмет
|
||||||
|
item = await marketplace_collection.find_one({"id": item_id})
|
||||||
|
if not item:
|
||||||
|
raise HTTPException(status_code=404, detail="Предмет не найден")
|
||||||
|
|
||||||
|
# Проверяем, что пользователь является владельцем предмета
|
||||||
|
if item["seller_name"] != username:
|
||||||
|
raise HTTPException(status_code=403, detail="Вы не можете изменить цену чужого предмета")
|
||||||
|
|
||||||
|
# Валидация новой цены
|
||||||
|
if new_price <= 0:
|
||||||
|
raise HTTPException(status_code=400, detail="Цена должна быть положительным числом")
|
||||||
|
|
||||||
|
# Обновляем цену предмета
|
||||||
|
result = await marketplace_collection.update_one(
|
||||||
|
{"id": item_id},
|
||||||
|
{"$set": {"price": new_price}}
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.modified_count == 0:
|
||||||
|
raise HTTPException(status_code=500, detail="Не удалось обновить цену предмета")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"message": f"Цена предмета обновлена на {new_price} монет"
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user