feat(03-02): Paw reactions GREEN — toggle/count router + PawButton on PostCard
- reactions.toggle: assertPetOwnership → findUnique(postId_petId) → create or delete, return {reacted}
- reactions.hasReacted, getCount implemented
- feed.ts + posts.byPetId: add _count{reactions,comments} to hydration include
- PawButton: optimistic toggle, filled/outline PawPrint icon, count badge
- PostCard: replace "0 likes/0 comments" placeholder with action row (PawButton + comment chip + Paw-Back stub)
- PostCardPost type: add optional _count field
- 4/4 reactions tests GREEN; 56 passing total (+3 from 53)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
b71edfa758
commit
6fbb99e7c9
5 files changed
+147
-12
No files matched your search
@@ -82,6 +82,7 @@ export const feedRouter = router({
|
||||
pet: { select: { id: true, name: true, avatarKey: true } },
|
||||
images: { orderBy: { position: "asc" } },
|
||||
milestone: true,
|
||||
_count: { select: { reactions: true, comments: true } },
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -209,6 +209,7 @@ export const postsRouter = router({
|
||||
images: { orderBy: { position: "asc" } },
|
||||
pet: { select: { id: true, name: true, avatarKey: true } },
|
||||
milestone: true,
|
||||
_count: { select: { reactions: true, comments: true } },
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -2,23 +2,59 @@ import "server-only";
|
||||
import { z } from "zod";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { router, protectedProcedure } from "@/trpc/init";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
|
||||
async function assertPetOwnership(
|
||||
prisma: PrismaClient,
|
||||
petId: string,
|
||||
userId: string
|
||||
) {
|
||||
const pet = await prisma.pet.findUnique({ where: { id: petId } });
|
||||
if (!pet || pet.ownerId !== userId) {
|
||||
throw new TRPCError({
|
||||
code: "FORBIDDEN",
|
||||
message: "You do not have permission to access this pet.",
|
||||
});
|
||||
}
|
||||
return pet;
|
||||
}
|
||||
|
||||
export const reactionsRouter = router({
|
||||
toggle: protectedProcedure
|
||||
.input(z.object({ postId: z.string().min(1), petId: z.string().min(1) }))
|
||||
.mutation(async (): Promise<{ reacted: boolean }> => {
|
||||
throw new TRPCError({ code: "NOT_IMPLEMENTED" });
|
||||
.mutation(async ({ ctx, input }): Promise<{ reacted: boolean }> => {
|
||||
await assertPetOwnership(ctx.prisma, input.petId, ctx.userId);
|
||||
|
||||
const existing = await ctx.prisma.reaction.findUnique({
|
||||
where: { postId_petId: { postId: input.postId, petId: input.petId } },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
await ctx.prisma.reaction.delete({
|
||||
where: { postId_petId: { postId: input.postId, petId: input.petId } },
|
||||
});
|
||||
return { reacted: false };
|
||||
}
|
||||
|
||||
await ctx.prisma.reaction.create({
|
||||
data: { postId: input.postId, petId: input.petId },
|
||||
});
|
||||
return { reacted: true };
|
||||
}),
|
||||
|
||||
hasReacted: protectedProcedure
|
||||
.input(z.object({ postId: z.string().min(1), petId: z.string().min(1) }))
|
||||
.query(async (): Promise<boolean> => {
|
||||
throw new TRPCError({ code: "NOT_IMPLEMENTED" });
|
||||
.query(async ({ ctx, input }): Promise<boolean> => {
|
||||
await assertPetOwnership(ctx.prisma, input.petId, ctx.userId);
|
||||
const existing = await ctx.prisma.reaction.findUnique({
|
||||
where: { postId_petId: { postId: input.postId, petId: input.petId } },
|
||||
});
|
||||
return existing !== null;
|
||||
}),
|
||||
|
||||
getCount: protectedProcedure
|
||||
.input(z.object({ postId: z.string().min(1) }))
|
||||
.query(async (): Promise<number> => {
|
||||
throw new TRPCError({ code: "NOT_IMPLEMENTED" });
|
||||
.query(async ({ ctx, input }): Promise<number> => {
|
||||
return ctx.prisma.reaction.count({ where: { postId: input.postId } });
|
||||
}),
|
||||
});
|
||||
Reference in new issue
Block a user