Skip to content

Commit

Permalink
refactor: update following logic
Browse files Browse the repository at this point in the history
  • Loading branch information
haru52 committed Jun 1, 2024
1 parent ee8f6cc commit 41d6235
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 50 deletions.
10 changes: 8 additions & 2 deletions src/app/_components/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ export async function Avatar({
{loginAvatarIsInSocial &&
loginAvatarId !== avatar.id &&
(isFollowing ? (
<UnfollowButton avatarId={avatar.id} />
<UnfollowButton
followedById={loginAvatarId}
followingId={avatar.id}
/>
) : (
<FollowButton avatarId={avatar.id} />
<FollowButton
followedById={loginAvatarId}
followingId={avatar.id}
/>
))}
</div>
</div>
Expand Down
10 changes: 8 additions & 2 deletions src/app/_components/follow-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import { useRouter } from "next/navigation";
import { api } from "~/trpc/react";

export function FollowButton({ avatarId }: { avatarId: string }) {
export function FollowButton({
followedById,
followingId,
}: {
followedById: string;
followingId: string;
}) {
const router = useRouter();
const { mutate } = api.follows.follow.useMutation({
onSuccess: () => {
Expand All @@ -16,7 +22,7 @@ export function FollowButton({ avatarId }: { avatarId: string }) {
className="btn btn-primary"
onClick={(e) => {
e.preventDefault();
mutate(avatarId);
mutate({ followedById, followingId });
}}
>
フォロー
Expand Down
10 changes: 8 additions & 2 deletions src/app/_components/unfollow-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import { useRouter } from "next/navigation";
import { api } from "~/trpc/react";

export function UnfollowButton({ avatarId }: { avatarId: string }) {
export function UnfollowButton({
followedById,
followingId,
}: {
followedById: string;
followingId: string;
}) {
const router = useRouter();
const { mutate } = api.follows.unfollow.useMutation({
onSuccess: () => {
Expand All @@ -16,7 +22,7 @@ export function UnfollowButton({ avatarId }: { avatarId: string }) {
className="btn btn-outline btn-primary hover:btn-error after:content-['フォロー中'] hover:after:content-['フォロー解除']"
onClick={(e) => {
e.preventDefault();
mutate(avatarId);
mutate({ followedById, followingId });
}}
></button>
);
Expand Down
7 changes: 5 additions & 2 deletions src/app/socials/[screenName]/[avatarScreenName]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ export default async function Page({
{loginAvatar !== null &&
loginAvatar.id !== avatar.id &&
(isFollowing ? (
<UnfollowButton avatarId={avatar.id} />
<UnfollowButton
followedById={loginAvatar.id}
followingId={avatar.id}
/>
) : (
<FollowButton avatarId={avatar.id} />
<FollowButton followedById={loginAvatar.id} followingId={avatar.id} />
))}
<p>{avatar.user.introduction}</p>
{avatar.user.url !== null && avatar.user.url !== "" && (
Expand Down
79 changes: 37 additions & 42 deletions src/server/api/routers/follows.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,70 @@
import { z } from "zod";
import { CreateFollows } from "~/entities/follows";
import {
createTRPCRouter,
protectedProcedure,
publicProcedure,
} from "~/server/api/trpc";
import { TRPCError } from "@trpc/server";

export const followsRouter = createTRPCRouter({
follow: protectedProcedure
.input(z.string().uuid())
.input(CreateFollows)
.mutation(async ({ ctx, input }) => {
const followedAvatar = await ctx.db.avatar.findUnique({
where: { id: input },
});
if (followedAvatar === null) {
throw new Error("フォローされるアバターが見つかりません");
if (input.followedById === input.followingId) {
throw new Error("自分自身をフォローすることはできません");
}
const user = await ctx.db.user.findUnique({
where: { id: ctx.session.user.id },
include: {
avatars: {
select: { id: true },
where: { socialId: followedAvatar.socialId },
},
},
const followedBy = await ctx.db.avatar.findUnique({
where: { id: input.followedById },
});
if (user === null) throw new Error("ログインユーザーが見つかりません");
const followingAvatar = user.avatars[0];
if (followingAvatar === undefined) {
if (followedBy === null) {
throw new Error("フォローするアバターが見つかりません");
}
if (followingAvatar.id === followedAvatar.id) {
throw new Error("自分自身をフォローすることはできません");
if (followedBy.userId !== ctx.session.user.id) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
const following = await ctx.db.avatar.findUnique({
where: { id: input.followingId },
});
if (following === null) {
throw new Error("フォローされるアバターが見つかりません");
}
return ctx.db.follows.create({
data: {
followedBy: { connect: { id: followingAvatar.id } },
following: { connect: { id: input } },
followedBy: { connect: { id: input.followedById } },
following: { connect: { id: input.followingId } },
},
});
}),

unfollow: protectedProcedure
.input(z.string().uuid())
.input(
z.object({
followedById: z.string().uuid(),
followingId: z.string().uuid(),
}),
)
.mutation(async ({ ctx, input }) => {
const unfollowedAvatar = await ctx.db.avatar.findUnique({
where: { id: input },
});
if (unfollowedAvatar === null) {
throw new Error("フォロー解除されるアバターが見つかりません");
}
const user = await ctx.db.user.findUnique({
where: { id: ctx.session.user.id },
const follows = await ctx.db.follows.findFirst({
where: {
followedById: input.followedById,
followingId: input.followingId,
},
include: {
avatars: {
select: { id: true },
where: { socialId: unfollowedAvatar.socialId },
followedBy: {
include: { user: true },
},
},
});
if (user === null) throw new Error("ログインユーザーが見つかりません");
const unfollowingAvatar = user.avatars[0];
if (unfollowingAvatar === undefined) {
throw new Error("フォロー解除するアバターが見つかりません");
if (follows === null) {
throw new Error("フォローしていません");
}
if (unfollowingAvatar.id === unfollowedAvatar.id) {
throw new Error("自分自身をフォロー解除することはできません");
if (follows.followedBy.userId !== ctx.session.user.id) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return ctx.db.follows.deleteMany({
return ctx.db.follows.delete({
where: {
followedById: unfollowingAvatar.id,
followingId: input,
id: follows.id,
},
});
}),
Expand Down

0 comments on commit 41d6235

Please sign in to comment.