diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 75a6800..a3c88df 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -1,3 +1,4 @@
+// Connexion
generator client {
provider = "prisma-client-js"
}
@@ -7,24 +8,37 @@ datasource db {
url = env("DATABASE_URL")
}
+// Models
model User {
- id String @id @default(auto()) @map("_id") @db.ObjectId
- email String @unique
+ id String @id @default(auto()) @map("_id") @db.ObjectId
+ email String @unique
password String?
- name String
- surname String
- admin Boolean @default(false)
-
- messageSent Chat[]
- messageReceived Chat[] @relation("chatReceived")
+ firstName String
+ lastName String
+ role Role @default(USER)
+
+ Chat Chat? @relation(fields: [chatId], references: [id])
+ chatId String? @db.ObjectId
+ Message Message[]
}
model Chat {
- id String @id @default(auto()) @map("_id") @db.ObjectId
- message String
- sender User? @relation(fields: [senderId], references: [id])
- senderId String @db.ObjectId
- receiver User? @relation(name:"chatReceived", fields: [receiverId], references: [id])
- receiverId String @db.ObjectId
+ id String @id @default(auto()) @map("_id") @db.ObjectId
+ messages Message[]
+ users User[]
}
+model Message {
+ id String @id @default(auto()) @map("_id") @db.ObjectId
+ message String
+ user User @relation(fields: [userId], references: [id])
+ createdAt DateTime @default(now())
+ Chat Chat @relation(fields: [chatId], references: [id])
+ userId String @db.ObjectId
+ chatId String @db.ObjectId
+}
+
+enum Role {
+ USER
+ ADMIN
+}
diff --git a/src/models/api/user.ts b/src/models/api/user.ts
new file mode 100644
index 0000000..5fd0bc6
--- /dev/null
+++ b/src/models/api/user.ts
@@ -0,0 +1,16 @@
+type CreateUserQuery = {
+ email: string
+ password: string
+ firstName: string
+ lastName: string
+}
+
+type ReadUserQuery = {
+ id?: string
+}
+
+type DeleteUserQuery = {
+ id: string
+}
+
+export type {CreateUserQuery, ReadUserQuery, DeleteUserQuery};
diff --git a/src/pages/api/user/createUser.ts b/src/pages/api/user/createUser.ts
index 71d2e09..74f267a 100644
--- a/src/pages/api/user/createUser.ts
+++ b/src/pages/api/user/createUser.ts
@@ -1,17 +1,17 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { PrismaClient } from '@prisma/client'
+import type {CreateUserQuery} from '@/models/api/user';
+
+const prisma = new PrismaClient();
-export const prisma = new PrismaClient();
export default async function createUser(req: NextApiRequest, res: NextApiResponse) {
+ const {email, password, firstName, lastName} = req.body as CreateUserQuery;
-
- const {email, password, name, surname} = req.body
-
+ if (!email || !password || !firstName || !lastName)
+ return res.status(400).send({message: req.body});
- if (!email || !password) return res.status(400).send({message: req.body})
-
const newUser = await prisma.user.create({
- data: {email, password, name, surname},
+ data: {email, password, firstName, lastName},
});
return res.status(201).send({message: "createUser", newUser});
diff --git a/src/pages/api/user/deleteUser.ts b/src/pages/api/user/deleteUser.ts
index 6431471..f8f6fe6 100644
--- a/src/pages/api/user/deleteUser.ts
+++ b/src/pages/api/user/deleteUser.ts
@@ -1,16 +1,15 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { PrismaClient } from '@prisma/client'
+import type {DeleteUserQuery} from '@/models/api/user';
-export const prisma = new PrismaClient();
+const prisma = new PrismaClient();
export default async function deleteUser(req: NextApiRequest, res: NextApiResponse) {
- const {id} = req.query
-
+ const {id} = req.query as DeleteUserQuery
if (!id) return res.status(400).send({message: "error"});
const deletedUser = await prisma.user.delete({
where: { id }
});
-
return res.status(200).send({message: "deleteUser", deletedUser});
}
\ No newline at end of file
diff --git a/src/pages/api/user/index.ts b/src/pages/api/user/index.ts
index 30758fa..9946b23 100644
--- a/src/pages/api/user/index.ts
+++ b/src/pages/api/user/index.ts
@@ -1,10 +1,8 @@
import type {NextApiRequest, NextApiResponse} from 'next';
-import createUser from '@/pages/api/user/createUser';
-import readUser from '@/pages/api/user/readUser';
import updateUser from '@/pages/api/user/updateUser';
-import deleteUser from '@/pages/api/user/deleteUser';
import CRUD from '@/utils/CRUD';
-
+import {CreateUserQuery, DeleteUserQuery} from '@/models/api/user';
+import {PrismaClient} from '@prisma/client';
export default function handler(
req: NextApiRequest,
@@ -16,9 +14,40 @@ export default function handler(
if (method === CRUD.READ) return readUser(req, res);
if (method === CRUD.UPDATE) return updateUser(req, res);
if (method === CRUD.DELETE) return deleteUser(req, res);
- return help(req, res);
+ return help(res);
}
-function help(req: NextApiRequest, res: NextApiResponse) {
+function help(res: NextApiResponse) {
res.status(400).send({message: 'error'}); // TODO add help message
+}
+
+const prisma = new PrismaClient();
+
+async function createUser(req: NextApiRequest, res: NextApiResponse) {
+ const {email, password, firstName, lastName} = req.body as CreateUserQuery;
+
+ if (!email || !password || !firstName || !lastName)
+ return res.status(400).send({message: req.body});
+
+ const newUser = await prisma.user.create({
+ data: {email, password, firstName, lastName},
+ });
+
+ return res.status(201).send({message: 'createUser', newUser});
+}
+
+async function deleteUser(req: NextApiRequest, res: NextApiResponse) {
+ const {id} = req.query as DeleteUserQuery;
+ if (!id) return res.status(400).send({message: 'error'});
+
+ const deletedUser = await prisma.user.delete({
+ where: {id},
+ });
+ return res.status(200).send({message: 'deleteUser', deletedUser});
+}
+
+async function readUser(req: NextApiRequest, res: NextApiResponse) {
+ const users = await prisma.user.findMany();
+
+ return res.status(200).send({message: 'readUser', users});
}
\ No newline at end of file
diff --git a/src/pages/api/user/readUser.ts b/src/pages/api/user/readUser.ts
index 69b6de7..f102c44 100644
--- a/src/pages/api/user/readUser.ts
+++ b/src/pages/api/user/readUser.ts
@@ -1,7 +1,7 @@
import {NextApiRequest, NextApiResponse} from 'next';
import { PrismaClient } from '@prisma/client'
-export const prisma = new PrismaClient();
+const prisma = new PrismaClient();
export default async function readUser(req: NextApiRequest, res: NextApiResponse) {
const users = await prisma.user.findMany()
diff --git a/src/pages/login.jsx b/src/pages/login.jsx
deleted file mode 100644
index 4a48428..0000000
--- a/src/pages/login.jsx
+++ /dev/null
@@ -1,98 +0,0 @@
-import {
- Box,
- Button,
- Flex,
- FormControl,
- FormLabel,
- Heading,
- Image,
- Input,
- Spacer,
-} from "@chakra-ui/react";
-import { useRouter } from "next/router";
-import { useForm } from "react-hook-form";
-
-export default function Login() {
- const {
- handleSubmit,
- register,
- formState: { errors, isSubmitting },
- } = useForm();
-
- const router = useRouter();
-
- const onLogin = async (values) => {
- alert(JSON.stringify(values, null, 2));
- // faut voir ce qu'on fait quand on se connecte
- };
-
- const redirect_home = () => {
- router.push("/");
- };
-
- const RightSide = () => (
-
- Connexion
-
-
-
-
- );
-
- const LeftSide = () => (
-
-
-
- );
-
- return (
-
-
-
-
-
-
- );
-}
diff --git a/src/pages/login.tsx b/src/pages/login.tsx
new file mode 100644
index 0000000..b3aed36
--- /dev/null
+++ b/src/pages/login.tsx
@@ -0,0 +1,102 @@
+import {
+ Box,
+ Button,
+ Flex,
+ FormControl,
+ FormLabel,
+ Heading,
+ Image,
+ Input,
+ Spacer,
+} from '@chakra-ui/react';
+import {useRouter} from 'next/router';
+import {useForm} from 'react-hook-form';
+
+type FormValues = {
+ email: string
+ password: string
+}
+
+export default function Login() {
+ const {
+ handleSubmit,
+ register,
+ formState: {errors, isSubmitting},
+ } = useForm();
+
+ const router = useRouter();
+
+ const onLogin = async (values: FormValues) => {
+ alert(JSON.stringify(values, null, 2));
+ // faut voir ce qu'on fait quand on se connecte
+ };
+
+ const goHome = () => {
+ router.push('/');
+ };
+
+ const RightSide = () => (
+
+ Connexion
+
+
+
+
+
+ );
+
+ const LeftSide = () => (
+
+
+
+ );
+
+ return (
+
+
+
+
+
+
+ );
+}
diff --git a/src/pages/register.jsx b/src/pages/register.tsx
similarity index 52%
rename from src/pages/register.jsx
rename to src/pages/register.tsx
index 7c364fa..0d1a01f 100644
--- a/src/pages/register.jsx
+++ b/src/pages/register.tsx
@@ -9,129 +9,129 @@ import {
Image,
Input,
Spacer,
-} from "@chakra-ui/react";
-import { useRouter } from "next/router";
-import { useForm } from "react-hook-form";
+} from '@chakra-ui/react';
+import {useRouter} from 'next/router';
+import {useForm} from 'react-hook-form';
+
+type FormValues = {
+ firstName: string
+ lastName: string
+ email: string
+ password: string
+ confirmPassword: string
+}
export default function Register() {
const {
handleSubmit,
register,
- formState: { errors, isSubmitting },
+ formState: {errors, isSubmitting},
} = useForm();
const router = useRouter();
const redirect_home = () => {
- router.push("/");
+ router.push('/');
};
- const onRegister = async (values) => {
- // alert(JSON.stringify(values, null, 2));
- if (values.password !== values.password_bis) {
- alert("Les mots de passe ne correspondent pas");
+ const onRegister = async (values: FormValues) => {
+ // Verify password
+ if (values.password !== values.confirmPassword) {
+ alert('Les mots de passe ne correspondent pas');
return;
}
- try {
- const response = await fetch("/api/user/", {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(values),
- });
- const data = await response.json();
- if (data.error) {
- alert(data.message);
- } else {
- console.log(data);
- alert("Inscription réussie");
- router.push("/");
- }
- } catch (error) {
- console.log(error);
- }
+
+ // Create User
+ fetch('/api/user/', {
+ method: 'PUT',
+ headers: {'Content-Type': 'application/json'},
+ body: JSON.stringify(values),
+ }).then(res => res.json()).then(data => {
+ if (data.error) throw new Error();
+ alert('Inscription réussie');
+ router.push('/');
+ }).catch(err => console.error(err));
};
const RightSide = () => (
- Inscription
-
+ Inscription
+
@@ -140,11 +140,11 @@ export default function Register() {
);
const LeftSide = () => (
-
+
@@ -153,8 +153,8 @@ export default function Register() {
return (
-
-
+
+
);