Advertisement
ivandev

Untitled

Jan 11th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. from data import config
  2. from utils.db_api.models.bookfield import BookField
  3. from utils.db_api.models.field import Field
  4. from utils.db_api.models.game import Game
  5. from asyncpg import UniqueViolationError
  6. from utils.db_api.db_gino import db
  7. from utils.db_api.models.paidplayers import PaidPlayer
  8. from utils.db_api.models.user import User
  9. from utils.db_api.models.window import Window
  10.  
  11. """FIELDS COMMANDS"""
  12.  
  13.  
  14. async def select_all_fields():
  15.     fields = await Field.query.gino.all()
  16.     return list(fields)
  17.  
  18.  
  19. async def add_field(name: str, phone_number: str, address: str, photo_url: str, url: str):
  20.     field = await Field.create(name=name, phone_number=phone_number, address=address, photo_url=photo_url, url=url)
  21.     return field
  22.  
  23.  
  24. async def select_field_by_id(_id: int):
  25.     # select by id
  26.     field = await Field.query.where(Field.id == _id).gino.first()
  27.     return field
  28.  
  29.  
  30. async def select_field_by_name(name: str):
  31.     # select by name
  32.     field = await Field.query.where(Field.name == name).gino.first()
  33.     return field
  34.  
  35.  
  36. async def drop_all_():
  37.     await db.set_bind(config.POSTGRES_URI)
  38.     await db.gino.drop_all()
  39.     await db.gino.create_all()
  40.  
  41.  
  42. """GAMES COMMANDS"""
  43.  
  44.  
  45. async def add_game(place: str, date: str, time: str, players_number: int, price: int):
  46.     game = await Game.create(place=place, date=date, time=time, players_number=players_number, price=price)
  47.     return game
  48.  
  49.  
  50. async def select_game(_id: int):
  51.     user = await Game.query.where(Game.id == _id).gino.first()
  52.     return user
  53.  
  54.  
  55. async def select_all_games():
  56.     games = await Game.query.gino.all()
  57.     return list(games)
  58.  
  59.  
  60. """USER COMMANDS"""
  61.  
  62.  
  63. async def add_user(id: int, name: str, phone_number: str = None):
  64.     try:
  65.         user = User(id=id, name=name, phone_number=phone_number)
  66.         await user.create()
  67.  
  68.     except UniqueViolationError:
  69.         pass
  70.  
  71.  
  72. async def select_all_users():
  73.     users = await User.query.gino.all()
  74.     return list(users)
  75.  
  76.  
  77. async def select_user(id: int):
  78.     user = await User.query.where(User.id == id).gino.first()
  79.     return user
  80.  
  81.  
  82. async def select_user_by_phone_number(phone_number: str):
  83.     user = await User.query.where(User.phone_number == phone_number).gino.first()
  84.     return user
  85.  
  86.  
  87. async def count_users():
  88.     total = await db.func.count(User.id).gino.scalar()
  89.     return total
  90.  
  91.  
  92. async def update_user_phone(id, phone_number):
  93.     user = await User.get(id)
  94.     await user.update(phone_number=phone_number).apply()
  95.  
  96.  
  97. async def update_user_name(id, name):
  98.     user = await User.get(id)
  99.     await user.update(name=name).apply()
  100.  
  101.  
  102. async def withdraw_user_balance(id, amount):
  103.     user = await User.get(id)
  104.     balance = user.balance - amount
  105.     await user.update(balance=balance).apply()
  106.  
  107.  
  108. async def refill_user_balance(id, amount):
  109.     user = await User.get(id)
  110.     balance = user.balance + amount
  111.     await user.update(balance=balance).apply()
  112.  
  113.  
  114. """PaidPlayers commands"""
  115.  
  116.  
  117. async def add_paid_player(game_id: int, user_id: int):
  118.     try:
  119.         paid_player = PaidPlayer(game_id=game_id, user_id=user_id)
  120.         await paid_player.create()
  121.  
  122.     except UniqueViolationError:
  123.         pass
  124.  
  125.  
  126. async def select_all_players_by_game(game_id):
  127.     players = await PaidPlayer.query.where(PaidPlayer.game_id == game_id).gino.all()
  128.     return list(players)
  129.  
  130.  
  131. """WINDOW COMMANDS"""
  132.  
  133.  
  134. async def add_window(place: str, date: str, _from: str, _to: str):
  135.     try:
  136.         window = Window(place=place, date=date, _from=_from, _to=_to)
  137.         await window.create()
  138.  
  139.     except UniqueViolationError:
  140.         pass
  141.  
  142.  
  143. async def select_all_windows():
  144.     users = await Window.query.gino.all()
  145.     return list(users)
  146.  
  147.  
  148. async def select_window(_id: int):
  149.     window = await Window.query.where(Window.id == _id).gino.first()
  150.     return window
  151.  
  152.  
  153. async def count_windows():
  154.     total = await db.func.count(Window.id).gino.scalar()
  155.     return total
  156.  
  157.  
  158. async def delete_window(date: str, _from: str):
  159.     await Window.delete.where(Window.date == date and Window._from == _from).gino.status()
  160.  
  161.  
  162. """Booking fields commands"""
  163.  
  164.  
  165. async def add_bookfield(place: str, date: str, time: str, price: int):
  166.     bookfield = await BookField.create(place=place, date=date, time=time, price=price)
  167.     return bookfield
  168.  
  169.  
  170. async def select_bookfield(_id: int):
  171.     bookfield = await BookField.query.where(BookField.id == _id).gino.first()
  172.     return bookfield
  173.  
  174.  
  175. async def update_booking_user(_id, user_id):
  176.     bookfield = await BookField.get(_id)
  177.     await bookfield.update(user=user_id).apply()
  178.  
  179.  
  180. async def change_booking_status(_id):
  181.     bookfield = await BookField.get(_id)
  182.     if bookfield.paid:
  183.         await bookfield.update(paid=False).apply()
  184.     else:
  185.         await bookfield.update(paid=True).apply()
  186.  
  187.  
  188. async def delete_bookfield(book_id):
  189.     await BookField.delete.where(Window.id == book_id).gino.status()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement