Add search handler

This commit is contained in:
LoRiot 2022-10-21 21:06:20 +03:00
parent aaf3330d22
commit 8f77438b7d
4 changed files with 49 additions and 8 deletions

View file

@ -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
)

View file

@ -1,9 +1,8 @@
from loguru import logger from loguru import logger
import re import re
from aiogram import types, Dispatcher from aiogram import types
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from asyncpg import BitString from asyncpg import BitString
from states.project import ProjectStates from states.project import ProjectStates
@ -33,6 +32,7 @@ async def ask_for_project_fields(message: types.Message, state: FSMContext):
async def create_project( async def create_project(
message: types.Message, repo: Repos, state: FSMContext message: types.Message, repo: Repos, state: FSMContext
): ):
await state.reset_state()
field_patterns = { field_patterns = {
"Название проекта": str, "Описание": str, "Контакты": str, "Название проекта": str, "Описание": str, "Контакты": str,
"Кол-во участников": int, "Статус": BitString, "Теги": str, "Кол-во участников": int, "Статус": BitString, "Теги": str,
@ -53,9 +53,3 @@ async def create_project(
await repo.get_repo(ProjectRepo).add( await repo.get_repo(ProjectRepo).add(
*field_values, user_id=message.from_user.id *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)

View file

@ -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"<b>{project['name']}</b>\n\n {project['description']}",
parse_mode="html"
)

View file

@ -3,3 +3,4 @@ from aiogram.dispatcher.filters.state import State, StatesGroup
class ProjectStates(StatesGroup): class ProjectStates(StatesGroup):
set_fields = State() set_fields = State()
search = State()