switch to remix
This commit is contained in:
commit
52a0ba1b3b
77 changed files with 13468 additions and 0 deletions
53
app/models/note.server.ts
Normal file
53
app/models/note.server.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
import type { User, Note } from "@prisma/client";
|
||||
|
||||
import { prisma } from "~/db.server";
|
||||
|
||||
export type { Note } from "@prisma/client";
|
||||
|
||||
export function getNote({
|
||||
id,
|
||||
userId,
|
||||
}: Pick<Note, "id"> & {
|
||||
userId: User["id"];
|
||||
}) {
|
||||
return prisma.note.findFirst({
|
||||
where: { id, userId },
|
||||
});
|
||||
}
|
||||
|
||||
export function getNoteListItems({ userId }: { userId: User["id"] }) {
|
||||
return prisma.note.findMany({
|
||||
where: { userId },
|
||||
select: { id: true, title: true },
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
export function createNote({
|
||||
body,
|
||||
title,
|
||||
userId,
|
||||
}: Pick<Note, "body" | "title"> & {
|
||||
userId: User["id"];
|
||||
}) {
|
||||
return prisma.note.create({
|
||||
data: {
|
||||
title,
|
||||
body,
|
||||
user: {
|
||||
connect: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteNote({
|
||||
id,
|
||||
userId,
|
||||
}: Pick<Note, "id"> & { userId: User["id"] }) {
|
||||
return prisma.note.deleteMany({
|
||||
where: { id, userId },
|
||||
});
|
||||
}
|
59
app/models/user.server.ts
Normal file
59
app/models/user.server.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
import type { Password, User } from "@prisma/client";
|
||||
import bcrypt from "@node-rs/bcrypt";
|
||||
|
||||
import { prisma } from "~/db.server";
|
||||
|
||||
export type { User } from "@prisma/client";
|
||||
|
||||
export async function getUserById(id: User["id"]) {
|
||||
return prisma.user.findUnique({ where: { id } });
|
||||
}
|
||||
|
||||
export async function getUserByEmail(email: User["email"]) {
|
||||
return prisma.user.findUnique({ where: { email } });
|
||||
}
|
||||
|
||||
export async function createUser(email: User["email"], password: string) {
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
return prisma.user.create({
|
||||
data: {
|
||||
email,
|
||||
password: {
|
||||
create: {
|
||||
hash: hashedPassword,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteUserByEmail(email: User["email"]) {
|
||||
return prisma.user.delete({ where: { email } });
|
||||
}
|
||||
|
||||
export async function verifyLogin(
|
||||
email: User["email"],
|
||||
password: Password["hash"]
|
||||
) {
|
||||
const userWithPassword = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
include: {
|
||||
password: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!userWithPassword || !userWithPassword.password) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const isValid = await bcrypt.verify(password, userWithPassword.password.hash);
|
||||
|
||||
if (!isValid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { password: _password, ...userWithoutPassword } = userWithPassword;
|
||||
|
||||
return userWithoutPassword;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue