Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
superfaz committed Sep 7, 2024
1 parent 1c01fa5 commit 7661cc2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
39 changes: 39 additions & 0 deletions src/app/(edit)/edit/[character]/helpers-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { DataSets, DataSource, type IDataSource } from "data";
import { type Character, IdSchema } from "model";

interface SuccessResult {
success: true;
character: Character;
}

interface ErrorResult {
success: false;
errorCode: "notFound" | "unexpectedError";
message?: string;
}

type Result = SuccessResult | ErrorResult;

export async function retrieveCharacter(id: string): Promise<Result> {
// Validate the parameter
const parse = IdSchema.safeParse(id);
if (!parse.success) {
return { success: false, errorCode: "notFound" };
}

// Validate the request
const characterId: string = parse.data;
const { getUser } = getKindeServerSession();
const user = await getUser();
const dataSource: IDataSource = new DataSource();
const characters = await dataSource.get(DataSets.Characters).find({ id: characterId, userId: user.id });

if (characters.length === 0) {
return { success: false, errorCode: "notFound" };
} else if (characters.length > 1) {
return { success: false, errorCode: "unexpectedError", message: "Multiple characters found with the same id" };
} else {
return { success: true, character: characters[0] };
}
}
37 changes: 13 additions & 24 deletions src/app/(edit)/edit/[character]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { type Metadata } from "next";
import { notFound } from "next/navigation";
import { DataSets, DataSource, type IDataSource } from "data";
import { IdSchema } from "model";
import { ViewBuilder } from "view/server";
import { isSecure } from "app/helpers-server";
import { retrieveCharacter } from "./helpers-server";
import { PageContent } from "./PageContent";

export const metadata: Metadata = {
Expand All @@ -15,27 +13,18 @@ export default async function Page({ params }: Readonly<{ params: { character: s
const returnTo = `/edit/${params.character}`;

if (await isSecure(returnTo)) {
// Validate the parameter
const parse = IdSchema.safeParse(params.character);
if (!parse.success) {
return notFound();
const result = await retrieveCharacter(params.character);
if (result.success) {
// Render the page
const builder = new ViewBuilder();
return <PageContent character={await builder.createCharacterDetailed(result.character)} />;
} else {
if (result.errorCode === "notFound") {
return notFound();
} else {
console.error("Unexpected error", result.message);
throw new Error("Unexpected error");
}
}

// Validate the request
const characterId: string = parse.data;
const { getUser } = getKindeServerSession();
const user = await getUser();
const dataSource: IDataSource = new DataSource();
const characters = await dataSource.get(DataSets.Characters).find({ id: characterId, userId: user.id });

if (characters.length === 0) {
return notFound();
} else if (characters.length > 1) {
throw new Error("Multiple characters found with the same id");
}

// Render the page
const builder = new ViewBuilder();
return <PageContent character={await builder.createCharacterDetailed(characters[0])} />;
}
}

0 comments on commit 7661cc2

Please sign in to comment.