Skip to content

Commit

Permalink
improve things slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Mar 7, 2022
1 parent 730940b commit ea6662e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions app/models/note.server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { prisma } from "~/db.server";

export function getNote(userId: string, id: string) {
export function getNote({ userId, id }: { userId: string; id: string }) {
return prisma.note.findFirst({
where: { id, userId },
});
}

export function getNoteListItems(userId: string) {
export function getNoteListItems({ userId }: { userId: string }) {
return prisma.note.findMany({
where: { userId: userId },
select: { id: true, title: true },
orderBy: { updatedAt: "desc" },
});
}

export function createNote(title: string, body: string, userId: string) {
export function createNote({
title,
body,
userId,
}: {
title: string;
body: string;
userId: string;
}) {
return prisma.note.create({
data: {
title,
Expand All @@ -28,7 +36,7 @@ export function createNote(title: string, body: string, userId: string) {
});
}

export function deleteNote(userId: string, id: string) {
export function deleteNote({ id, userId }: { id: string; userId: string }) {
return prisma.note.deleteMany({
where: { id, userId },
});
Expand Down
4 changes: 2 additions & 2 deletions app/routes/notes/$noteId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const loader: LoaderFunction = async ({ request, params }) => {
const userId = await requireUserId(request);
invariant(params.noteId, "noteId not found");

const note = await getNote(userId, params.noteId);
const note = await getNote({ userId, id: params.noteId });
if (!note) {
throw new Response("Not Found", { status: 404 });
}
Expand All @@ -26,7 +26,7 @@ export const action: ActionFunction = async ({ request, params }) => {
const userId = await requireUserId(request);
invariant(params.noteId, "noteId not found");

await deleteNote(userId, params.noteId);
await deleteNote({ userId, id: params.noteId });

return redirect("/notes");
};
Expand Down
2 changes: 1 addition & 1 deletion app/routes/notes/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const action: ActionFunction = async ({ request }) => {
);
}

const note = await createNote(title, body, userId);
const note = await createNote({ title, body, userId });

return redirect(`/notes/${note.id}`);
};
Expand Down

0 comments on commit ea6662e

Please sign in to comment.