fix promo #3
This commit is contained in:
@ -9,6 +9,16 @@ promo_codes = db["promo_codes"]
|
||||
promo_redemptions = db["promo_redemptions"]
|
||||
|
||||
class PromoService:
|
||||
|
||||
@staticmethod
|
||||
def _to_json(doc: dict | None):
|
||||
if not doc:
|
||||
return doc
|
||||
doc = dict(doc)
|
||||
if "_id" in doc:
|
||||
doc["_id"] = str(doc["_id"])
|
||||
return doc
|
||||
|
||||
def __init__(self):
|
||||
self.coins = CoinsService()
|
||||
|
||||
@ -42,11 +52,12 @@ class PromoService:
|
||||
# можно точнее ловить DuplicateKeyError
|
||||
raise HTTPException(status_code=409, detail="Promo code already exists")
|
||||
doc["_id"] = r.inserted_id
|
||||
return doc
|
||||
return self._to_json(doc)
|
||||
|
||||
async def list(self, limit: int = 50, skip: int = 0):
|
||||
cursor = promo_codes.find({}).sort("created_at", -1).skip(skip).limit(limit)
|
||||
return await cursor.to_list(length=limit)
|
||||
items = await cursor.to_list(length=limit)
|
||||
return [self._to_json(x) for x in items]
|
||||
|
||||
async def update(self, promo_id: str, payload):
|
||||
try:
|
||||
@ -59,7 +70,7 @@ class PromoService:
|
||||
doc = await promo_codes.find_one({"_id": oid})
|
||||
if not doc:
|
||||
raise HTTPException(status_code=404, detail="Promo not found")
|
||||
return doc
|
||||
return self._to_json(doc)
|
||||
|
||||
update_data["updated_at"] = datetime.utcnow()
|
||||
|
||||
@ -70,7 +81,7 @@ class PromoService:
|
||||
)
|
||||
if not doc:
|
||||
raise HTTPException(status_code=404, detail="Promo not found")
|
||||
return doc
|
||||
return self._to_json(doc)
|
||||
|
||||
async def delete(self, promo_id: str):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user