From 8f77438b7d7bdf7e238c84f9a25dcbc575fc807b Mon Sep 17 00:00:00 2001 From: LoRiot Date: Fri, 21 Oct 2022 21:06:20 +0300 Subject: [PATCH] Add search handler --- handlers/projects/__init__.py | 21 ++++++++++++++++ handlers/{projects.py => projects/create.py} | 10 ++------ handlers/projects/search.py | 25 ++++++++++++++++++++ states/project.py | 1 + 4 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 handlers/projects/__init__.py rename handlers/{projects.py => projects/create.py} (87%) create mode 100644 handlers/projects/search.py diff --git a/handlers/projects/__init__.py b/handlers/projects/__init__.py new file mode 100644 index 0000000..f3e5b5a --- /dev/null +++ b/handlers/projects/__init__.py @@ -0,0 +1,21 @@ +from aiogram import Dispatcher +from aiogram.dispatcher.filters import Text + +from states.project import ProjectStates +from . import create, search + + +def register_projects_handlers(dp: Dispatcher): + dp.register_message_handler( + create.ask_for_project_fields, Text("Создать проект") + ) + dp.register_message_handler( + create.create_project, state=ProjectStates.set_fields + ) + + dp.register_message_handler( + search.ask_for_project_name, Text("Найти проект") + ) + dp.register_message_handler( + search.search_for_projects, state=ProjectStates.search + ) diff --git a/handlers/projects.py b/handlers/projects/create.py similarity index 87% rename from handlers/projects.py rename to handlers/projects/create.py index 3396bc1..12da2c5 100644 --- a/handlers/projects.py +++ b/handlers/projects/create.py @@ -1,9 +1,8 @@ from loguru import logger import re -from aiogram import types, Dispatcher +from aiogram import types from aiogram.dispatcher import FSMContext -from aiogram.dispatcher.filters import Text from asyncpg import BitString from states.project import ProjectStates @@ -33,6 +32,7 @@ async def ask_for_project_fields(message: types.Message, state: FSMContext): async def create_project( message: types.Message, repo: Repos, state: FSMContext ): + await state.reset_state() field_patterns = { "Название проекта": str, "Описание": str, "Контакты": str, "Кол-во участников": int, "Статус": BitString, "Теги": str, @@ -53,9 +53,3 @@ async def create_project( await repo.get_repo(ProjectRepo).add( *field_values, user_id=message.from_user.id ) - await state.reset_state() - - -def register_projects_handlers(dp: Dispatcher): - dp.register_message_handler(ask_for_project_fields, Text("Создать проект")) - dp.register_message_handler(create_project, state=ProjectStates.set_fields) diff --git a/handlers/projects/search.py b/handlers/projects/search.py new file mode 100644 index 0000000..2dc4b1c --- /dev/null +++ b/handlers/projects/search.py @@ -0,0 +1,25 @@ +from loguru import logger + +from aiogram import types +from aiogram.dispatcher import FSMContext + +from states.project import ProjectStates +from services.repositories import Repos, ProjectRepo + + +async def ask_for_project_name(message: types.Message, state: FSMContext): + await state.set_state(ProjectStates.search) + await message.answer("Введите название проекта") + + +async def search_for_projects( + message: types.Message, state: FSMContext, repo: Repos +): + await state.reset_state() + projects = await repo.get_repo(ProjectRepo).get(message.text) + + for project in projects: + await message.answer( + f"{project['name']}\n\n {project['description']}", + parse_mode="html" + ) diff --git a/states/project.py b/states/project.py index 4ca5520..bb67e48 100644 --- a/states/project.py +++ b/states/project.py @@ -3,3 +3,4 @@ from aiogram.dispatcher.filters.state import State, StatesGroup class ProjectStates(StatesGroup): set_fields = State() + search = State()