diff --git a/UI/src/main.py b/UI/src/main.py index dcb1f62..322a51f 100644 --- a/UI/src/main.py +++ b/UI/src/main.py @@ -1,23 +1,90 @@ import flet as ft -from requests.board_games import get_sections - +from requests.board_games import get_sections, get_games def main(page: ft.Page): + def update_games(section): + current_section.value = section + games = get_games(None if section == "Все" else section) + grid.controls.clear() + for game in games: + grid.controls.append( + ft.Column([ + ft.Text(str(game)), + ft.Row([ + ft.IconButton(icon=ft.icons.SHOPPING_CART), + ft.IconButton(icon=ft.icons.FAVORITE), + ]), + ]) + ) + page.update() + print("Rerender!") + page.title = "Весёлые кубики" - sections = get_sections() + sections = [*get_sections(), "Все"] - sections_buttons = [ft.Button(section) for section in sections] + sections_buttons = [ft.Button(section, on_click=lambda e, s=section: update_games(s)) for section in sections] + + games = get_games() + + current_section = ft.Ref[str]() + + grid = ft.GridView( + expand=1, + runs_count=5, + max_extent=200, + child_aspect_ratio=1.0, + spacing=5, + run_spacing=5, + ) + + for game in games: + grid.controls.append( + ft.Column([ + ft.Text(str(game)), + ft.Row([ + ft.IconButton( + icon=ft.Icons.SHOPPING_CART + ), + ft.IconButton( + icon=ft.Icons.FAVORITE + ) + ]) + ]) + ) page.add( - ft.SafeArea( - ft.Column ( - [ft.Row(sections_buttons, alignment=ft.MainAxisAlignment.CENTER)], - alignment=ft.MainAxisAlignment.START - ) - ) + ft.Row( + [ + ft.Text( + "Весёлые кубики", + color="#59799f", + size=24, + weight=ft.FontWeight.BOLD, + ), + ft.Row([ + ft.Button( + text="Вход", + on_click=lambda e: print("Button 1 clicked"), + ), + ft.Button( + text="Регистрация", + on_click=lambda e: print("Button 2 clicked"), + ), + ]) + ], + alignment=ft.MainAxisAlignment.SPACE_BETWEEN, # Adjust alignment if needed + ), + ft.Column ( + [ft.Row(sections_buttons, alignment=ft.MainAxisAlignment.CENTER)], + alignment=ft.MainAxisAlignment.START + ), + grid ) ft.app(main) + + + diff --git a/UI/src/models/__init__.py b/UI/src/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/UI/src/models/game.py b/UI/src/models/game.py new file mode 100644 index 0000000..40772ef --- /dev/null +++ b/UI/src/models/game.py @@ -0,0 +1,10 @@ +class Game: + def __init__(self, board_game_id, name, genre, publisher, price): + self.board_game_id = board_game_id + self.name = name + self.genre = genre + self.publisher = publisher + self.price = price + + def __str__(self): + return f"{self.name}\nЖанр: {self.genre}\nИздатель: {self.publisher}\n{self.price}" diff --git a/UI/src/requests/board_games.py b/UI/src/requests/board_games.py index 12f07fe..ca3a317 100644 --- a/UI/src/requests/board_games.py +++ b/UI/src/requests/board_games.py @@ -1,3 +1,6 @@ +import psycopg2 + +from models.game import Game from requests.connection import get_cursor @@ -7,3 +10,25 @@ def get_sections(): cursor.execute("select name from sections") return [elem[0] for elem in cursor.fetchall()] + + +def get_games(section = None): + cursor = get_cursor() + + cursor.execute("SET lc_monetary TO 'ru_RU.UTF-8'") + + if section is None: + cursor.execute(""" + select * + from board_games join sections_board_games using(board_game_id) join sections using(section_id) + """) + else: + cursor.execute(f""" + select * + from board_games join sections_board_games using(board_game_id) join sections using(section_id) + where sections.name='{section}' + """) + + res = [Game(game[1], game[2], game[3], game[4], game[5]) for game in cursor.fetchall()] + + return res diff --git a/populate.sql b/populate.sql index f80bafb..7bd079f 100644 --- a/populate.sql +++ b/populate.sql @@ -105,8 +105,8 @@ INSERT INTO bags_board_games (bag_id, board_game_id, count) VALUES INSERT INTO wishlists (client_id) VALUES (1), -(1), -(1), +(2), +(3), (4), (5), (6),