Skip to content

Commit 4edbd28

Browse files
committed
urlの検索クエリを追加した + 微改善
1 parent c96dd03 commit 4edbd28

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

apps/server/src/handlers/users.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ export const registerUsersHandlers = (
100100
{ ...query, limit: query.limit + 1 },
101101
readStatus,
102102
c.req.query('text'),
103+
c.req.query('url'),
103104
);
104105

105106
const clips = selectedClips.map((clip) => ({
106-
...clip.clips,
107-
article: clip.articles,
107+
...clip.clip,
108+
article: clip.article,
108109
}));
109110

110111
return c.json(
@@ -312,15 +313,20 @@ export const registerUsersHandlers = (
312313
return c.json({ error: message }, 400);
313314
}
314315

315-
const inboxItems = await findInboxItemsByUserId(user.id, {
316+
const selectedItems = await findInboxItemsByUserId(user.id, {
316317
...query,
317318
limit: query.limit + 1,
318319
});
319320

321+
const items = selectedItems.map((item) => ({
322+
...item.item,
323+
article: item.article,
324+
}));
325+
320326
return c.json(
321327
{
322-
items: inboxItems.slice(0, query.limit),
323-
finished: inboxItems.length <= query.limit,
328+
items: items.slice(0, query.limit),
329+
finished: items.length <= query.limit,
324330
},
325331
200,
326332
);

packages/database/src/repositories/clip.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ export const findClipsByUserIdAndReadStatus = async (
7575
query: SearchQuery,
7676
readStatus: 'all' | 'read' | 'unread' = 'all',
7777
text = '',
78+
url = '',
7879
) => {
7980
const { params, condition } = converter(query);
8081

8182
const selectedClips = await db
8283
.select({
83-
clips,
84-
articles: {
84+
clip: clips,
85+
article: {
8586
id: articles.id,
8687
title: articles.title,
8788
body: sql`left(${articles.body}, 200)`,
@@ -105,6 +106,7 @@ export const findClipsByUserIdAndReadStatus = async (
105106
sql`to_tsvector(${articles.body}) @@ to_tsquery(${text})`,
106107
sql`to_tsvector(${articles.title}) @@ to_tsquery(${text})`,
107108
),
109+
url !== '' && eq(articles.url, url),
108110
]),
109111
),
110112
)

packages/database/src/repositories/inbox.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { and, desc, eq } from 'drizzle-orm';
1+
import { and, desc, eq, sql } from 'drizzle-orm';
22

33
import { db } from '@/database/drizzleClient';
4-
import { inboxes } from '@/models';
4+
import { articles, inboxes } from '@/models';
55
import {
66
type SearchQuery,
77
convertSearchQuery,
@@ -66,14 +66,26 @@ export const findInboxItemsByUserId = async (
6666
query: SearchQuery,
6767
) => {
6868
const { condition, params } = converter(query);
69-
const items = await db.query.inboxes.findMany({
70-
where: and(eq(inboxes.userId, userId), condition),
71-
...params,
72-
orderBy: desc(params.orderBy),
73-
with: {
74-
article: true,
75-
},
76-
});
69+
const items = await db
70+
.select({
71+
item: inboxes,
72+
article: {
73+
id: articles.id,
74+
title: articles.title,
75+
body: sql`left(${articles.body}, 200)`,
76+
ogImageUrl: articles.ogImageUrl,
77+
createdAt: articles.createdAt,
78+
updatedAt: articles.updatedAt,
79+
summary: articles.summary,
80+
url: articles.url,
81+
},
82+
})
83+
.from(inboxes)
84+
.where(and(eq(inboxes.userId, userId), condition))
85+
.orderBy(desc(params.orderBy))
86+
.limit(params.limit)
87+
.offset(params.offset)
88+
.execute();
7789

7890
return items;
7991
};

packages/openapi/src/routes/users/getUserClips.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export const getClipsRequestQuerySchema = z
3535
},
3636
example: 'TypeScript',
3737
}),
38+
url: z.string().openapi({
39+
param: {
40+
name: 'url',
41+
in: 'query',
42+
required: false,
43+
description: 'クリップのURL',
44+
},
45+
example: 'https://example.com',
46+
}),
3847
limit: z
3948
.string()
4049
.default('20')

0 commit comments

Comments
 (0)