1
0
mirror of synced 2024-12-21 01:05:53 +01:00
artemis/titles/cxb/schema/item.py

43 lines
1.4 KiB
Python
Raw Normal View History

from typing import Optional, Dict, List
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_, case
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean
from sqlalchemy.schema import ForeignKey
from sqlalchemy.sql import func
from sqlalchemy.dialects.mysql import insert
from core.data.schema import BaseData, metadata
energy = Table(
"cxb_rev_energy",
metadata,
Column("id", Integer, primary_key=True, nullable=False),
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
Column("energy", Integer, nullable=False, server_default="0"),
UniqueConstraint("user", name="cxb_rev_energy_uk"),
2023-03-09 17:38:58 +01:00
mysql_charset="utf8mb4",
)
2023-03-09 17:38:58 +01:00
class CxbItemData(BaseData):
def put_energy(self, user_id: int, rev_energy: int) -> Optional[int]:
2023-03-09 17:38:58 +01:00
sql = insert(energy).values(user=user_id, energy=rev_energy)
2023-03-09 17:38:58 +01:00
conflict = sql.on_duplicate_key_update(energy=rev_energy)
result = self.execute(conflict)
if result is None:
2023-03-09 17:38:58 +01:00
self.logger.error(
f"{__name__} failed to insert item! user: {user_id}, energy: {rev_energy}"
)
return None
2023-03-09 17:38:58 +01:00
return result.lastrowid
2023-03-09 17:38:58 +01:00
def get_energy(self, user_id: int) -> Optional[Dict]:
2023-03-09 17:38:58 +01:00
sql = energy.select(and_(energy.c.user == user_id))
result = self.execute(sql)
2023-03-09 17:38:58 +01:00
if result is None:
return None
return result.fetchone()