89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
from typing import List
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.bot_purchase import BotPurchase, BotPurchaseStatus
|
|
from app.schemas.bot_purchase import BotPurchaseCreate, BotPurchaseUpdate
|
|
|
|
|
|
class CRUDBotPurchase(CRUDBase[BotPurchase, BotPurchaseCreate, BotPurchaseUpdate]):
|
|
def get_by_user(
|
|
self, db: Session, *, user_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[BotPurchase]:
|
|
return (
|
|
db.query(BotPurchase)
|
|
.filter(BotPurchase.user_id == user_id)
|
|
.order_by(BotPurchase.created_at.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_by_bot(
|
|
self, db: Session, *, bot_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[BotPurchase]:
|
|
return (
|
|
db.query(BotPurchase)
|
|
.filter(BotPurchase.bot_id == bot_id)
|
|
.order_by(BotPurchase.created_at.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_by_status(
|
|
self, db: Session, *, status: BotPurchaseStatus, skip: int = 0, limit: int = 100
|
|
) -> List[BotPurchase]:
|
|
return (
|
|
db.query(BotPurchase)
|
|
.filter(BotPurchase.status == status)
|
|
.order_by(BotPurchase.created_at.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_completed_due(
|
|
self, db: Session, *, skip: int = 0, limit: int = 100
|
|
) -> List[BotPurchase]:
|
|
now = datetime.utcnow()
|
|
return (
|
|
db.query(BotPurchase)
|
|
.filter(
|
|
BotPurchase.status == BotPurchaseStatus.RUNNING,
|
|
BotPurchase.end_time <= now
|
|
)
|
|
.order_by(BotPurchase.end_time)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def complete(
|
|
self, db: Session, *, db_obj: BotPurchase
|
|
) -> BotPurchase:
|
|
update_data = {"status": BotPurchaseStatus.COMPLETED}
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
def cancel(
|
|
self, db: Session, *, db_obj: BotPurchase
|
|
) -> BotPurchase:
|
|
update_data = {"status": BotPurchaseStatus.CANCELLED}
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
|
|
bot_purchase = CRUDBotPurchase(BotPurchase)
|
|
|
|
|
|
# Aliases for convenience
|
|
get_bot_purchase = bot_purchase.get
|
|
create_bot_purchase = bot_purchase.create
|
|
update_bot_purchase = bot_purchase.update
|
|
get_bot_purchases_by_user = bot_purchase.get_by_user
|
|
get_bot_purchases_by_bot = bot_purchase.get_by_bot
|
|
get_bot_purchases_by_status = bot_purchase.get_by_status
|
|
get_completed_due_bot_purchases = bot_purchase.get_completed_due
|
|
complete_bot_purchase = bot_purchase.complete
|
|
cancel_bot_purchase = bot_purchase.cancel |