Skip to content

Commit 2fbcf23

Browse files
committed
implemented req changes
1 parent 730c909 commit 2fbcf23

File tree

3 files changed

+92
-79
lines changed

3 files changed

+92
-79
lines changed

apps/server/src/routers/posts.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { eq } from "drizzle-orm";
2-
import type { FastifyInstance } from "fastify";
2+
import type { FastifyInstance , FastifyRequest} from "fastify";
33
import { DrizzleClient } from "@/db/index";
44
import { posts as postsTable } from "@/db/schema/post.schema";
55
import {
@@ -26,30 +26,33 @@ export async function postRoutes(fastify: FastifyInstance) {
2626
},
2727
},
2828
},
29-
async (request, reply) => {
29+
async (
30+
request: FastifyRequest<{
31+
Params: { id: string };
32+
Querystring: { page: number; limit: number };
33+
}>,
34+
reply
35+
) => {
36+
const { page, limit } = request.query;
37+
const offset = (page - 1) * limit;
38+
const params = threadIdParamsSchema.safeParse(request.params);
39+
if (!params.success)
40+
return reply
41+
.status(400)
42+
.send({ success: false, error: "Invalid thread ID" });
43+
const threadId = params.data.id;
3044
const userId = request.userId;
31-
if (!userId) {
45+
if (!userId)
3246
return reply
3347
.status(401)
3448
.send({ error: "Unauthorized", success: false });
35-
}
3649
const user = await DrizzleClient.query.users.findFirst({
3750
where: (u, { eq }) => eq(u.id, userId),
3851
});
39-
if (!user) {
52+
if (!user)
4053
return reply
4154
.status(404)
4255
.send({ error: "User not found", success: false });
43-
}
44-
const params = threadIdParamsSchema.safeParse(request.params);
45-
if (!params.success) {
46-
return reply
47-
.status(400)
48-
.send({ success: false, error: "Invalid thread ID" });
49-
}
50-
const threadId = params.data.id;
51-
const { page = 1, limit = 20 } = request.query as { page?: number; limit?: number };
52-
const offset = (page - 1) * limit;
5356
try {
5457
const threadPosts = await DrizzleClient.query.posts.findMany({
5558
where: (p, { eq }) => eq(p.threadId, threadId),

apps/server/src/routers/threads.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { eq } from "drizzle-orm";
2-
import type { FastifyInstance } from "fastify";
2+
import type { FastifyInstance , FastifyRequest} from "fastify";
33
import {
44
createThreadSchema,
55
threadIdParamsSchema,
@@ -25,31 +25,33 @@ export async function threadRoutes(fastify: FastifyInstance) {
2525
},
2626
},
2727
},
28-
async (request, reply) => {
28+
async (
29+
request: FastifyRequest<{
30+
Params: { id: string };
31+
Querystring: { page: number; limit: number };
32+
}>,
33+
reply
34+
) => {
35+
const { page, limit } = request.query;
36+
const offset = (page - 1) * limit;
37+
const params = topicIdParamsSchema.safeParse(request.params);
38+
if (!params.success)
39+
return reply
40+
.status(400)
41+
.send({ success: false, error: "Invalid topic ID" });
42+
const topicId = params.data.id;
2943
const userId = request.userId;
30-
if (!userId) {
44+
if (!userId)
3145
return reply
3246
.status(401)
3347
.send({ error: "Unauthorized", success: false });
34-
}
3548
const user = await DrizzleClient.query.users.findFirst({
3649
where: (u, { eq }) => eq(u.id, userId),
3750
});
38-
if (!user) {
51+
if (!user)
3952
return reply
4053
.status(404)
4154
.send({ error: "User not found", success: false });
42-
}
43-
const params = topicIdParamsSchema.safeParse(request.params);
44-
if (!params.success) {
45-
return reply
46-
.status(400)
47-
.send({ success: false, error: "Invalid topic ID" });
48-
}
49-
const topicId = params.data.id;
50-
const { page = 1, limit = 10 } =
51-
(request.query as { page?: number; limit?: number }) || {};
52-
const offset = (page - 1) * limit;
5355
try {
5456
const relatedThreads = await DrizzleClient.query.threads.findMany({
5557
where: (t, { eq }) => eq(t.topicId, topicId),
@@ -68,10 +70,9 @@ export async function threadRoutes(fastify: FastifyInstance) {
6870
});
6971
} catch (error) {
7072
fastify.log.error("Error fetching threads for topic:", error);
71-
return reply.status(500).send({
72-
success: false,
73-
error: "Failed to fetch threads",
74-
});
73+
return reply
74+
.status(500)
75+
.send({ success: false, error: "Failed to fetch threads" });
7576
}
7677
}
7778
);

apps/server/src/routers/topics.ts

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,59 @@ import { authenticateUser } from "./auth";
1212
export async function topicRoutes(fastify: FastifyInstance) {
1313

1414
fastify.get(
15-
"/topics",
16-
{
17-
preHandler: authenticateUser,
18-
schema: {
19-
querystring: {
20-
type: "object",
21-
properties: {
22-
page: { type: "integer", minimum: 1, default: 1 },
23-
limit: { type: "integer", minimum: 1, maximum: 100, default: 10 },
24-
},
25-
},
26-
},
27-
},
28-
async (request: FastifyRequest<{ Querystring: { page?: number; limit?: number } }>, reply) => {
29-
const userId = request.userId;
30-
if (!userId)
31-
return reply
32-
.status(401)
33-
.send({ success: false, error: "Unauthorized" });
34-
const { page = 1, limit = 10 } = request.query;
35-
const offset = (page - 1) * limit;
36-
try {
37-
const topics = await DrizzleClient.query.topics.findMany({
38-
limit: limit,
39-
offset: offset,
40-
orderBy: (table, { desc }) => [desc(table.createdAt)],
41-
});
42-
43-
return reply.status(200).send({
44-
success: true,
45-
data: topics,
46-
pagination: {
47-
page,
48-
limit,
49-
count: topics.length,
50-
},
51-
});
52-
} catch (error) {
53-
fastify.log.error({ err: error }, "Failed to fetch topics");
54-
return reply
55-
.status(500)
56-
.send({ success: false, error: "Failed to fetch topics" });
57-
}
58-
}
15+
"/topics",
16+
{
17+
preHandler: authenticateUser,
18+
schema: {
19+
querystring: {
20+
type: "object",
21+
properties: {
22+
page: { type: "integer", minimum: 1, default: 1 },
23+
limit: { type: "integer", minimum: 1, maximum: 100, default: 10 },
24+
},
25+
},
26+
},
27+
},
28+
async (
29+
request: FastifyRequest<{ Querystring: { page: number; limit: number } }>,
30+
reply
31+
) => {
32+
const { page, limit } = request.query;
33+
const offset = (page - 1) * limit;
34+
const userId = request.userId;
35+
if (!userId)
36+
return reply
37+
.status(401)
38+
.send({ success: false, error: "Unauthorized" });
39+
const user = await DrizzleClient.query.users.findFirst({
40+
where: (u, { eq }) => eq(u.id, userId),
41+
});
42+
if (!user)
43+
return reply
44+
.status(404)
45+
.send({ success: false, error: "User not found" });
46+
try {
47+
const topics = await DrizzleClient.query.topics.findMany({
48+
limit: limit,
49+
offset: offset,
50+
orderBy: (table, { desc }) => [desc(table.createdAt)],
51+
});
52+
return reply.status(200).send({
53+
success: true,
54+
data: topics,
55+
pagination: {
56+
page,
57+
limit,
58+
count: topics.length,
59+
},
60+
});
61+
} catch (error) {
62+
fastify.log.error({ err: error }, "Failed to fetch topics");
63+
return reply
64+
.status(500)
65+
.send({ success: false, error: "Failed to fetch topics" });
66+
}
67+
}
5968
);
6069

6170
fastify.post(

0 commit comments

Comments
 (0)