Show projects related to the user's chat

This commit is contained in:
LoRiot 2022-10-22 17:42:02 +03:00
parent d2307dba48
commit 2a85be2f6f
2 changed files with 12 additions and 7 deletions

View File

@ -4,7 +4,7 @@ from aiogram import types
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
from states.project import ProjectStates from states.project import ProjectStates
from services.repositories import Repos, ProjectRepo from services.repositories import Repos, ProjectRepo, UserRepo
async def ask_for_project_name(message: types.Message, state: FSMContext): async def ask_for_project_name(message: types.Message, state: FSMContext):
@ -16,7 +16,10 @@ async def search_for_projects(
message: types.Message, state: FSMContext, repo: Repos message: types.Message, state: FSMContext, repo: Repos
): ):
await state.reset_state() await state.reset_state()
projects = await repo.get_repo(ProjectRepo).get(message.text) user = await repo.get_repo(UserRepo).get(message.from_user.id)
projects = await repo.get_repo(ProjectRepo).get(
message.text, user["chat_id"]
)
for project in projects: for project in projects:
await message.answer( await message.answer(

View File

@ -27,8 +27,8 @@ class UserRepo(BaseRepo):
) )
async def get(self, user_id: int) -> Record: async def get(self, user_id: int) -> Record:
return await self.conn.fetchval( return await self.conn.fetchrow(
"SELECT user_id FROM users WHERE user_id = $1", user_id "SELECT user_id, chat_id FROM users WHERE user_id = $1", user_id
) )
@ -42,8 +42,10 @@ class ProjectRepo(BaseRepo):
user_id, *args user_id, *args
) )
async def get(self, name: str) -> Record: async def get(self, name: str, chat_id: int) -> Record:
return await self.conn.fetch( return await self.conn.fetch(
"SELECT id, name, description FROM projects WHERE name LIKE $1", "SELECT id, name, description FROM projects "
f"%{name}%" "INNER JOIN users ON projects.creator = users.user_id "
"WHERE users.chat_id = $1 AND name LIKE $2",
chat_id, f"%{name}%"
) )