mirror of
https://github.com/LucasVbr/meeting-app.git
synced 2026-05-13 17:21:53 +00:00
Edit schema
Took 2 hours 51 minutes
This commit is contained in:
+24
-10
@@ -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
|
||||
password String?
|
||||
name String
|
||||
surname String
|
||||
admin Boolean @default(false)
|
||||
firstName String
|
||||
lastName String
|
||||
role Role @default(USER)
|
||||
|
||||
messageSent Chat[]
|
||||
messageReceived Chat[] @relation("chatReceived")
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
@@ -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) return res.status(400).send({message: req.body})
|
||||
if (!email || !password || !firstName || !lastName)
|
||||
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});
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
@@ -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});
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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 = () => (
|
||||
<Flex
|
||||
justify={"center"}
|
||||
direction={"column"}
|
||||
flexBasis={"100%"}
|
||||
align={"center"}
|
||||
>
|
||||
<Heading mb={"2.5rem"}>Connexion</Heading>
|
||||
<Box w={"25vw"}>
|
||||
<form onSubmit={handleSubmit(onLogin)}>
|
||||
<FormControl isInvalid={errors.name}>
|
||||
<Box mb={"1rem"}>
|
||||
<FormLabel>Adresse email</FormLabel>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
placeholder="Adresse@email.com"
|
||||
{...register("email", {
|
||||
required: "This is required",
|
||||
})}
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={"1rem"}>
|
||||
<FormLabel>Mot de passe</FormLabel>
|
||||
<Input
|
||||
id="pwd"
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register("pwd", {
|
||||
required: "This is required",
|
||||
})}
|
||||
/>
|
||||
</Box>
|
||||
<Flex mt={"1rem"} w={"100%"}>
|
||||
<Button onClick={redirect_home}>Retour</Button>
|
||||
<Spacer />
|
||||
<Button colorScheme="purple" type="submit">
|
||||
Connexion
|
||||
</Button>
|
||||
</Flex>
|
||||
</FormControl>
|
||||
</form>
|
||||
</Box>
|
||||
</Flex>
|
||||
);
|
||||
|
||||
const LeftSide = () => (
|
||||
<Box flexBasis={"100%"}>
|
||||
<Image
|
||||
h={"100vh"}
|
||||
w={"100%"}
|
||||
src={"/couple_horizon.png"}
|
||||
alt="couple looking at horizon"
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Flex gap={10}>
|
||||
<LeftSide />
|
||||
<RightSide />
|
||||
</Flex>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -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 = () => (
|
||||
<Flex
|
||||
justify={'center'}
|
||||
direction={'column'}
|
||||
flexBasis={'100%'}
|
||||
align={'center'}
|
||||
>
|
||||
<Heading mb={'2.5rem'}>Connexion</Heading>
|
||||
|
||||
<Box w={'25vw'}>
|
||||
<form onSubmit={handleSubmit(onLogin)}>
|
||||
<FormControl isInvalid={errors.name}>
|
||||
<Box mb={'1rem'}>
|
||||
<FormLabel>Adresse email</FormLabel>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
placeholder="Adresse@email.com"
|
||||
{...register('email', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={'1rem'}>
|
||||
<FormLabel>Mot de passe</FormLabel>
|
||||
<Input
|
||||
id="pwd"
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register('password', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</Box>
|
||||
<Flex mt={'1rem'} w={'100%'}>
|
||||
<Button onClick={goHome}>Retour</Button>
|
||||
<Spacer/>
|
||||
<Button colorScheme="purple" type="submit">Connexion</Button>
|
||||
</Flex>
|
||||
</FormControl>
|
||||
</form>
|
||||
</Box>
|
||||
</Flex>
|
||||
);
|
||||
|
||||
const LeftSide = () => (
|
||||
<Box flexBasis={'100%'}>
|
||||
<Image
|
||||
h={'100vh'}
|
||||
w={'100%'}
|
||||
src={'/couple_horizon.png'}
|
||||
alt="couple looking at horizon"
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Flex gap={10}>
|
||||
<LeftSide/>
|
||||
<RightSide/>
|
||||
</Flex>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -9,9 +9,17 @@ 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 {
|
||||
@@ -23,115 +31,107 @@ export default function Register() {
|
||||
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",
|
||||
},
|
||||
|
||||
// Create User
|
||||
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);
|
||||
}
|
||||
}).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 = () => (
|
||||
<Flex
|
||||
justify={"center"}
|
||||
direction={"column"}
|
||||
flexBasis={"100%"}
|
||||
align={"center"}
|
||||
justify={'center'}
|
||||
direction={'column'}
|
||||
flexBasis={'100%'}
|
||||
align={'center'}
|
||||
>
|
||||
<Heading mb={"2.5rem"}>Inscription</Heading>
|
||||
<Box w={"25vw"}>
|
||||
<Heading mb={'2.5rem'}>Inscription</Heading>
|
||||
<Box w={'25vw'}>
|
||||
<form onSubmit={handleSubmit(onRegister)}>
|
||||
<FormControl isInvalid={errors.name}>
|
||||
<Flex mb={"1rem"} gap={5}>
|
||||
<Flex mb={'1rem'} gap={5}>
|
||||
<FormControl>
|
||||
<FormLabel>Prénom</FormLabel>
|
||||
<Input
|
||||
id="surname"
|
||||
id="firstName"
|
||||
type="text"
|
||||
placeholder="Prénom"
|
||||
{...register("surname", {
|
||||
required: "This is required",
|
||||
{...register('firstName', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormControl>
|
||||
<FormLabel>Nom</FormLabel>
|
||||
<Input
|
||||
id="name"
|
||||
id="lastName"
|
||||
type="text"
|
||||
placeholder="Nom"
|
||||
{...register("name", {
|
||||
required: "This is required",
|
||||
{...register('lastName', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</FormControl>
|
||||
</Flex>
|
||||
<FormControl mb={"1rem"}>
|
||||
<FormControl mb={'1rem'}>
|
||||
<FormLabel>Adresse email</FormLabel>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
placeholder="Adresse@email.com"
|
||||
{...register("email", {
|
||||
required: "This is required",
|
||||
{...register('email', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormControl mb={"1rem"}>
|
||||
<FormControl mb={'1rem'}>
|
||||
<FormLabel>Mot de passe</FormLabel>
|
||||
<Input
|
||||
id="password"
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register("password", {
|
||||
required: "This is required",
|
||||
{...register('password', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</FormControl>
|
||||
{/* Il y a une margin en trop mais je me suis dit que c'etais mieux d'avoir plus d'expace entre les deux */}
|
||||
<FormControl mb={"1rem"}>
|
||||
<FormControl mb={'1rem'}>
|
||||
<FormLabel>Confirmation du mot de passe</FormLabel>
|
||||
<Input
|
||||
id="password_bis"
|
||||
id="confirmPassword"
|
||||
type="password"
|
||||
placeholder="Mot de passe"
|
||||
{...register("password_bis", {
|
||||
required: "This is required",
|
||||
{...register('confirmPassword', {
|
||||
required: 'This is required',
|
||||
})}
|
||||
/>
|
||||
</FormControl>
|
||||
|
||||
<FormErrorMessage>
|
||||
{errors.name && errors.name.message}
|
||||
{/*{errors.name && errors.name.message}*/}
|
||||
</FormErrorMessage>
|
||||
</FormControl>
|
||||
<Flex mt={"1rem"} w={"100%"}>
|
||||
<Flex mt={'1rem'} w={'100%'}>
|
||||
<Button onClick={redirect_home}>Retour</Button>
|
||||
<Spacer/>
|
||||
<Button colorScheme="purple" type="submit" isLoading={isSubmitting}>
|
||||
Je m'inscris
|
||||
Je m'inscris
|
||||
</Button>
|
||||
</Flex>
|
||||
</form>
|
||||
@@ -140,11 +140,11 @@ export default function Register() {
|
||||
);
|
||||
|
||||
const LeftSide = () => (
|
||||
<Box flexBasis={"100%"}>
|
||||
<Box flexBasis={'100%'}>
|
||||
<Image
|
||||
h={"100vh"}
|
||||
w={"100%"}
|
||||
src={"/couple_holding_hands.png"}
|
||||
h={'100vh'}
|
||||
w={'100%'}
|
||||
src={'/couple_holding_hands.png'}
|
||||
alt="couple holding hands"
|
||||
/>
|
||||
</Box>
|
||||
Reference in New Issue
Block a user