Skip to content

Commit

Permalink
urlの検索クエリを追加した + 微改善
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Jan 20, 2024
1 parent c96dd03 commit 4edbd28
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
16 changes: 11 additions & 5 deletions apps/server/src/handlers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ export const registerUsersHandlers = (
{ ...query, limit: query.limit + 1 },
readStatus,
c.req.query('text'),
c.req.query('url'),
);

const clips = selectedClips.map((clip) => ({
...clip.clips,
article: clip.articles,
...clip.clip,
article: clip.article,
}));

return c.json(
Expand Down Expand Up @@ -312,15 +313,20 @@ export const registerUsersHandlers = (
return c.json({ error: message }, 400);
}

const inboxItems = await findInboxItemsByUserId(user.id, {
const selectedItems = await findInboxItemsByUserId(user.id, {
...query,
limit: query.limit + 1,
});

const items = selectedItems.map((item) => ({
...item.item,
article: item.article,
}));

return c.json(
{
items: inboxItems.slice(0, query.limit),
finished: inboxItems.length <= query.limit,
items: items.slice(0, query.limit),
finished: items.length <= query.limit,
},
200,
);
Expand Down
6 changes: 4 additions & 2 deletions packages/database/src/repositories/clip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ export const findClipsByUserIdAndReadStatus = async (
query: SearchQuery,
readStatus: 'all' | 'read' | 'unread' = 'all',
text = '',
url = '',
) => {
const { params, condition } = converter(query);

const selectedClips = await db
.select({
clips,
articles: {
clip: clips,
article: {
id: articles.id,
title: articles.title,
body: sql`left(${articles.body}, 200)`,
Expand All @@ -105,6 +106,7 @@ export const findClipsByUserIdAndReadStatus = async (
sql`to_tsvector(${articles.body}) @@ to_tsquery(${text})`,
sql`to_tsvector(${articles.title}) @@ to_tsquery(${text})`,
),
url !== '' && eq(articles.url, url),
]),
),
)
Expand Down
32 changes: 22 additions & 10 deletions packages/database/src/repositories/inbox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { and, desc, eq } from 'drizzle-orm';
import { and, desc, eq, sql } from 'drizzle-orm';

import { db } from '@/database/drizzleClient';
import { inboxes } from '@/models';
import { articles, inboxes } from '@/models';
import {
type SearchQuery,
convertSearchQuery,
Expand Down Expand Up @@ -66,14 +66,26 @@ export const findInboxItemsByUserId = async (
query: SearchQuery,
) => {
const { condition, params } = converter(query);
const items = await db.query.inboxes.findMany({
where: and(eq(inboxes.userId, userId), condition),
...params,
orderBy: desc(params.orderBy),
with: {
article: true,
},
});
const items = await db
.select({
item: inboxes,
article: {
id: articles.id,
title: articles.title,
body: sql`left(${articles.body}, 200)`,
ogImageUrl: articles.ogImageUrl,
createdAt: articles.createdAt,
updatedAt: articles.updatedAt,
summary: articles.summary,
url: articles.url,
},
})
.from(inboxes)
.where(and(eq(inboxes.userId, userId), condition))
.orderBy(desc(params.orderBy))
.limit(params.limit)
.offset(params.offset)
.execute();

return items;
};
Expand Down
9 changes: 9 additions & 0 deletions packages/openapi/src/routes/users/getUserClips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export const getClipsRequestQuerySchema = z
},
example: 'TypeScript',
}),
url: z.string().openapi({
param: {
name: 'url',
in: 'query',
required: false,
description: 'クリップのURL',
},
example: 'https://example.com',
}),
limit: z
.string()
.default('20')
Expand Down

0 comments on commit 4edbd28

Please sign in to comment.