add endpoint create bonus
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
from fastapi import APIRouter, Query, Body
|
from fastapi import APIRouter, Query, Body
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from app.models.bonus import PurchaseBonus
|
from app.models.bonus import CreateBonusType, PurchaseBonus
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
@ -9,6 +9,12 @@ router = APIRouter(
|
|||||||
tags=["Bonuses"]
|
tags=["Bonuses"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@router.post("/create")
|
||||||
|
async def create_bonus_type(bonus: CreateBonusType):
|
||||||
|
"""Создание нового типа бонуса (админ)"""
|
||||||
|
from app.services.bonus import BonusService
|
||||||
|
return await BonusService().create_bonus_type(bonus)
|
||||||
|
|
||||||
@router.get("/effects")
|
@router.get("/effects")
|
||||||
async def get_user_effects(username: str):
|
async def get_user_effects(username: str):
|
||||||
"""Получить активные эффекты пользователя для плагина"""
|
"""Получить активные эффекты пользователя для плагина"""
|
||||||
|
|||||||
@ -2,6 +2,17 @@ from pydantic import BaseModel
|
|||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
class CreateBonusType(BaseModel):
|
||||||
|
name: str
|
||||||
|
description: str
|
||||||
|
effect_type: str
|
||||||
|
base_effect_value: float
|
||||||
|
effect_increment: float
|
||||||
|
price: int
|
||||||
|
upgrade_price: int
|
||||||
|
duration: int # в секундах
|
||||||
|
max_level: int = 0
|
||||||
|
|
||||||
class PurchaseBonus(BaseModel):
|
class PurchaseBonus(BaseModel):
|
||||||
username: str
|
username: str
|
||||||
bonus_type_id: str
|
bonus_type_id: str
|
||||||
|
|||||||
@ -10,6 +10,37 @@ bonus_types_collection = db.bonus_types
|
|||||||
user_bonuses_collection = db.user_bonuses
|
user_bonuses_collection = db.user_bonuses
|
||||||
|
|
||||||
class BonusService:
|
class BonusService:
|
||||||
|
|
||||||
|
async def create_bonus_type(self, bonus_data):
|
||||||
|
"""Создание нового типа бонуса"""
|
||||||
|
bonus_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
bonus = {
|
||||||
|
"id": bonus_id,
|
||||||
|
"name": bonus_data.name,
|
||||||
|
"description": bonus_data.description,
|
||||||
|
"effect_type": bonus_data.effect_type,
|
||||||
|
"base_effect_value": bonus_data.base_effect_value,
|
||||||
|
"effect_increment": bonus_data.effect_increment,
|
||||||
|
"price": bonus_data.price,
|
||||||
|
"upgrade_price": bonus_data.upgrade_price,
|
||||||
|
"duration": bonus_data.duration,
|
||||||
|
"max_level": bonus_data.max_level
|
||||||
|
}
|
||||||
|
|
||||||
|
# Проверка на дубликат имени
|
||||||
|
existing = await bonus_types_collection.find_one({"name": bonus_data.name})
|
||||||
|
if existing:
|
||||||
|
raise HTTPException(status_code=400, detail="Бонус с таким именем уже существует")
|
||||||
|
|
||||||
|
await bonus_types_collection.insert_one(bonus)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"message": "Тип бонуса успешно создан",
|
||||||
|
"bonus_id": bonus_id
|
||||||
|
}
|
||||||
|
|
||||||
async def get_user_active_effects(self, username: str):
|
async def get_user_active_effects(self, username: str):
|
||||||
"""Получить активные эффекты пользователя для плагина"""
|
"""Получить активные эффекты пользователя для плагина"""
|
||||||
from app.db.database import users_collection
|
from app.db.database import users_collection
|
||||||
|
|||||||
Reference in New Issue
Block a user