From 6db6dba13d5eb7dc7b5ed51425f476d5926a1214 Mon Sep 17 00:00:00 2001 From: DEsimas Date: Thu, 19 Dec 2024 23:12:11 +0300 Subject: [PATCH] feat: UI profile --- UI/src/main.py | 6 ++-- UI/src/models/game.py | 5 +-- UI/src/models/order.py | 10 ++++++ UI/src/models/user.py | 9 +++++ UI/src/pages/__init__.py | 0 UI/src/pages/profile.py | 61 ++++++++++++++++++++++++++++++++++ UI/src/requests/auth.py | 15 +++++++++ UI/src/requests/clients.py | 67 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 UI/src/models/order.py create mode 100644 UI/src/models/user.py create mode 100644 UI/src/pages/__init__.py create mode 100644 UI/src/pages/profile.py create mode 100644 UI/src/requests/clients.py diff --git a/UI/src/main.py b/UI/src/main.py index c024a2a..714aa47 100644 --- a/UI/src/main.py +++ b/UI/src/main.py @@ -3,6 +3,7 @@ import re import flet as ft from flet.core.page import RouteChangeEvent +from pages.profile import profile from requests.board_games import get_sections, get_games from requests.auth import login as login_req, registration @@ -248,10 +249,6 @@ def register(page): ) -def profile(page): - page.add(ft.Text("uwu")) - - def main(page: ft.Page): def route_change(e): route = e.route @@ -273,3 +270,4 @@ def main(page: ft.Page): ft.app(main) + diff --git a/UI/src/models/game.py b/UI/src/models/game.py index 40772ef..b5e99a0 100644 --- a/UI/src/models/game.py +++ b/UI/src/models/game.py @@ -1,10 +1,11 @@ class Game: - def __init__(self, board_game_id, name, genre, publisher, price): + def __init__(self, board_game_id, name, genre, publisher, price, count = None): self.board_game_id = board_game_id self.name = name self.genre = genre self.publisher = publisher self.price = price + self.count = count def __str__(self): - return f"{self.name}\nЖанр: {self.genre}\nИздатель: {self.publisher}\n{self.price}" + return f"{self.name}\nЖанр: {self.genre}\nИздатель: {self.publisher}\n{self.price}" + (f"\n{self.count} шт." if self.count is not None else "") diff --git a/UI/src/models/order.py b/UI/src/models/order.py new file mode 100644 index 0000000..ba20e2e --- /dev/null +++ b/UI/src/models/order.py @@ -0,0 +1,10 @@ +class Order: + def __init__(self, id, pick_up, date, cost, games = []): + self.id = id + self.pick_up = pick_up + self.date = str(date)[:-7] + self.cost = cost + self.games = games + + def __str__(self): + return f"{self.pick_up}\n{self.date}\n{self.cost}" \ No newline at end of file diff --git a/UI/src/models/user.py b/UI/src/models/user.py new file mode 100644 index 0000000..4bd2d4a --- /dev/null +++ b/UI/src/models/user.py @@ -0,0 +1,9 @@ +class User: + def __init__(self, id, surname, forename, patronymic, age, phone_number, email): + self.id = id + self.surname = surname + self.forename = forename + self.patronymic = patronymic + self.age = age + self.phone_number = phone_number + self.email = email diff --git a/UI/src/pages/__init__.py b/UI/src/pages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/UI/src/pages/profile.py b/UI/src/pages/profile.py new file mode 100644 index 0000000..aa62f96 --- /dev/null +++ b/UI/src/pages/profile.py @@ -0,0 +1,61 @@ +import flet as ft +from alembic.command import history + +from requests.auth import get_user +from requests.clients import get_wishlist, get_cart, get_history + + +def profile(page: ft.Page): + def back_clicked(e): + page.go("/") + page.update() + + def order_clicked(e): + pass + + user = get_user(page.client_storage.get("user_id")) + wishlist = get_wishlist(user.id) + cart = get_cart(user.id) + history = get_history(user.id) + + page.add(ft.Column([ + ft.TextButton("Назад", on_click=back_clicked), + ft.Text(f"Здравствуйте, {user.forename} {user.patronymic}!", size=24), + ft.Text(f"Список желаний", size=20), + ft.Row([ + ft.Column([ + ft.Text(str(game)), + ft.Row([ + ft.IconButton( + icon=ft.Icons.SHOPPING_CART + ), + ft.IconButton( + icon=ft.Icons.DELETE + ) + ]) + ]) for game in wishlist + ]), + ft.Text(f"Корзина", size=20), + ft.Row([ + ft.Column([ + ft.Text(str(game)), + ft.Row([ + ft.IconButton( + icon=ft.Icons.DELETE + ) + ]) + ]) for game in cart + ]), + ft.Button("Заказать"), + ft.Text(f"История заказов", size=20), + ft.Row([ + ft.Column([ + ft.Text(f"Заказ №{order.id}", size=20), + ft.Text(str(order)), + ft.Text(f"Состав:", size=18), + ft.Row([ + ft.Text(str(game)) for game in order.games + ]) + ]) for order in history + ]) + ])) diff --git a/UI/src/requests/auth.py b/UI/src/requests/auth.py index 9d02caf..48f948d 100644 --- a/UI/src/requests/auth.py +++ b/UI/src/requests/auth.py @@ -1,3 +1,4 @@ +from models.user import User from requests.connection import get_cursor @@ -26,5 +27,19 @@ def registration(first_name, last_name, middle_name, age, phone, email, password insert into auth (client_id, password) values ('{user_id}', '{password}'); """) + cursor.execute(f""" + insert into bags (client_id) + values ('{user_id}'); + """) + cursor.execute(f""" + insert into wishlists (client_id) + values ('{user_id}'); + """) conn.commit() return user_id + +def get_user(user_id): + (cursor, conn) = get_cursor() + cursor.execute(f"select * from clients where client_id={user_id}") + res = cursor.fetchone() + return User(res[0], res[1], res[2], res[3], res[4], res[5], res[6]) \ No newline at end of file diff --git a/UI/src/requests/clients.py b/UI/src/requests/clients.py new file mode 100644 index 0000000..e6d8d53 --- /dev/null +++ b/UI/src/requests/clients.py @@ -0,0 +1,67 @@ +from models.game import Game +from models.order import Order +from requests.connection import get_cursor + + +def get_wishlist(user_id): + (cursor, conn) = get_cursor() + + cursor.execute("SET lc_monetary TO 'ru_RU.UTF-8'") + + cursor.execute(f""" + select * + from wishlists_games join wishlists using(wishlist_id) join board_games using(board_game_id) + where client_id = {user_id} + """) + + res = cursor.fetchall() + parsed = [] + + for game in res: + parsed.append(Game(game[0], game[3], game[4], game[5], game[6])) + + return parsed + + +def get_cart(user_id): + (cursor, conn) = get_cursor() + + cursor.execute("SET lc_monetary TO 'ru_RU.UTF-8'") + + cursor.execute(f""" + select * + from bags_board_games join bags using(bag_id) join board_games using(board_game_id) + where client_id = {user_id} + """) + + res = cursor.fetchall() + parsed = [] + + for game in res: + parsed.append(Game(game[0], game[4], game[5], game[6], game[7], game[2])) + + return parsed + +def get_history(user_id): + (cursor, conn) = get_cursor() + + cursor.execute("SET lc_monetary TO 'ru_RU.UTF-8'") + + cursor.execute(f"select * from orders where client_id = {user_id}") + + res = [Order(o[0], o[2], o[3], o[4]) for o in cursor.fetchall()] + + for order in res: + cursor.execute("SET lc_monetary TO 'ru_RU.UTF-8'") + + cursor.execute(f""" + select * + from orders_games join board_games using(board_game_id) + where order_id = {order.id} + """) + + games = [Game(g[0], g[4], g[5], g[6], g[7], g[3]) for g in cursor.fetchall()] + + order.games = games + + return res