from data import config from utils.db_api.models.bookfield import BookField from utils.db_api.models.field import Field from utils.db_api.models.game import Game from asyncpg import UniqueViolationError from utils.db_api.db_gino import db from utils.db_api.models.paidplayers import PaidPlayer from utils.db_api.models.user import User from utils.db_api.models.window import Window """FIELDS COMMANDS""" async def select_all_fields(): fields = await Field.query.gino.all() return list(fields) async def add_field(name: str, phone_number: str, address: str, photo_url: str, url: str): field = await Field.create(name=name, phone_number=phone_number, address=address, photo_url=photo_url, url=url) return field async def select_field_by_id(_id: int): # select by id field = await Field.query.where(Field.id == _id).gino.first() return field async def select_field_by_name(name: str): # select by name field = await Field.query.where(Field.name == name).gino.first() return field async def drop_all_(): await db.set_bind(config.POSTGRES_URI) await db.gino.drop_all() await db.gino.create_all() """GAMES COMMANDS""" async def add_game(place: str, date: str, time: str, players_number: int, price: int): game = await Game.create(place=place, date=date, time=time, players_number=players_number, price=price) return game async def select_game(_id: int): user = await Game.query.where(Game.id == _id).gino.first() return user async def select_all_games(): games = await Game.query.gino.all() return list(games) """USER COMMANDS""" async def add_user(id: int, name: str, phone_number: str = None): try: user = User(id=id, name=name, phone_number=phone_number) await user.create() except UniqueViolationError: pass async def select_all_users(): users = await User.query.gino.all() return list(users) async def select_user(id: int): user = await User.query.where(User.id == id).gino.first() return user async def select_user_by_phone_number(phone_number: str): user = await User.query.where(User.phone_number == phone_number).gino.first() return user async def count_users(): total = await db.func.count(User.id).gino.scalar() return total async def update_user_phone(id, phone_number): user = await User.get(id) await user.update(phone_number=phone_number).apply() async def update_user_name(id, name): user = await User.get(id) await user.update(name=name).apply() async def withdraw_user_balance(id, amount): user = await User.get(id) balance = user.balance - amount await user.update(balance=balance).apply() async def refill_user_balance(id, amount): user = await User.get(id) balance = user.balance + amount await user.update(balance=balance).apply() """PaidPlayers commands""" async def add_paid_player(game_id: int, user_id: int): try: paid_player = PaidPlayer(game_id=game_id, user_id=user_id) await paid_player.create() except UniqueViolationError: pass async def select_all_players_by_game(game_id): players = await PaidPlayer.query.where(PaidPlayer.game_id == game_id).gino.all() return list(players) """WINDOW COMMANDS""" async def add_window(place: str, date: str, _from: str, _to: str): try: window = Window(place=place, date=date, _from=_from, _to=_to) await window.create() except UniqueViolationError: pass async def select_all_windows(): users = await Window.query.gino.all() return list(users) async def select_window(_id: int): window = await Window.query.where(Window.id == _id).gino.first() return window async def count_windows(): total = await db.func.count(Window.id).gino.scalar() return total async def delete_window(date: str, _from: str): await Window.delete.where(Window.date == date and Window._from == _from).gino.status() """Booking fields commands""" async def add_bookfield(place: str, date: str, time: str, price: int): bookfield = await BookField.create(place=place, date=date, time=time, price=price) return bookfield async def select_bookfield(_id: int): bookfield = await BookField.query.where(BookField.id == _id).gino.first() return bookfield async def update_booking_user(_id, user_id): bookfield = await BookField.get(_id) await bookfield.update(user=user_id).apply() async def change_booking_status(_id): bookfield = await BookField.get(_id) if bookfield.paid: await bookfield.update(paid=False).apply() else: await bookfield.update(paid=True).apply() async def delete_bookfield(book_id): await BookField.delete.where(Window.id == book_id).gino.status()