Skip to content

Commit 15161e1

Browse files
committed
If user is provided but project query is empty, just list projects by the user
1 parent 10416b9 commit 15161e1

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/Share/Postgres/Queries.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,22 @@ searchUsersByNameOrHandlePrefix (Query prefix) (Limit limit) = do
261261
-- The PG.queryListRows accepts strings as web search queries, see
262262
-- https://www.postgresql.org/docs/current/textsearch-controls.html
263263
searchProjects :: Maybe UserId -> Maybe UserId -> Query -> Limit -> PG.Transaction e [(Project, UserHandle)]
264+
-- Don't search with an empty query
265+
searchProjects _caller Nothing (Query "") _limit = pure []
266+
searchProjects caller (Just userId) (Query "") limit = do
267+
-- If we have a userId filter but no query, just return all the projects owned by that user
268+
-- which the caller has access to.
269+
PG.queryListRows @(Project PG.:. PG.Only UserHandle)
270+
[PG.sql|
271+
SELECT p.id, p.owner_user_id, p.slug, p.summary, p.tags, p.private, p.created_at, p.updated_at, owner.handle
272+
FROM projects p
273+
JOIN users owner ON p.owner_user_id = owner.id
274+
WHERE p.owner_user_id = #{userId}
275+
AND (NOT p.private OR (#{caller} IS NOT NULL AND EXISTS (SELECT FROM accessible_private_projects WHERE user_id = #{caller} AND project_id = p.id)))
276+
ORDER BY p.created_at DESC
277+
LIMIT #{limit}
278+
|]
279+
<&> fmap \(project PG.:. PG.Only handle) -> (project, handle)
264280
searchProjects caller userIdFilter (Query query) limit = do
265281
let prefixQuery =
266282
query
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"body": [
3+
{
4+
"projectRef": "@test/publictestproject",
5+
"summary": "test project summary",
6+
"tag": "Project",
7+
"visibility": "public"
8+
}
9+
],
10+
"status": [
11+
{
12+
"status_code": 200
13+
}
14+
]
15+
}

transcripts/share-apis/projects-flow/run.zsh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ fetch "$transcript_user" GET project-catalog-get '/catalog'
7979
fetch "$transcript_user" GET project-search '/search?query=test'
8080

8181
# Should filter project search by user if provided a full valid handle:
82-
fetch "$transcript_user" GET project-search-with-user '/search?query=@test/public'
82+
fetch "$transcript_user" GET project-search-with-user-and-project-query '/search?query=@test/public'
83+
84+
# Should return all projects in a user if provided a full valid handle, but no project query:
85+
fetch "$transcript_user" GET project-search-with-only-user '/search?query=@test/'
8386

8487
# Transcript user should not find 'test' user's private project
8588
fetch "$transcript_user" GET project-search-inaccessible '/search?query=privatetestproject'

0 commit comments

Comments
 (0)