restructurize code in api and controller folder

This commit is contained in:
Guillaume Dorce 2022-08-19 16:06:31 +02:00
parent d94f22a3dc
commit 6d4822e50d
14 changed files with 100 additions and 7 deletions

3
.gitignore vendored
View File

@ -25,4 +25,5 @@ dist-ssr
*.sqlite
*.db
.env
.env
.prisma/

8
src/api/auth/index.ts Normal file
View File

@ -0,0 +1,8 @@
import { Router } from 'express';
import login from './login';
const auth = Router();
auth.use('/login', login);
export default auth;

18
src/api/auth/login.ts Normal file
View File

@ -0,0 +1,18 @@
import { getUser, UserLoginModel } from '@/controller/user';
import { Request, Response } from 'express';
import { User } from '@prisma/client';
const login = async (req: Request, res: Response) => {
try {
UserLoginModel.parse(req.body);
const user: User | null = await getUser(req.body.email);
if (user === null) {
return res.status(401).send('User not found');
}
return res.status(200).send(user);
} catch (error) {
return res.status(500).send(error);
}
};
export default login;

14
src/api/auth/signup.ts Normal file
View File

@ -0,0 +1,14 @@
import { newUser, UserModel } from '@/controller/user';
import { User } from '@prisma/client';
import { Request, Response } from 'express';
export const signup = async (req: Request, res: Response) => {
try {
UserModel.parse(req.body);
const user: User = req.body;
const prismaUser = await newUser(user);
return res.status(200).send(prismaUser);
} catch (error) {
return res.status(500).send(error);
}
};

View File

@ -1,9 +1,10 @@
import { Router } from 'express';
import posts from './posts';
import auth from './auth';
const router = Router();
router.get('/', (req, res) => {
res.send('Hello World!');
});
router.use('/posts', posts);
router.use('/auth', auth);
export default router;

9
src/api/posts.ts Normal file
View File

@ -0,0 +1,9 @@
import { Router } from 'express';
const posts = Router();
posts.get('/', (req, res) => {
res.send('Hello World!');
});
export default posts;

View File

5
src/controller/index.ts Normal file
View File

@ -0,0 +1,5 @@
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default prisma;

37
src/controller/user.ts Normal file
View File

@ -0,0 +1,37 @@
import { PrismaClient, User } from '@prisma/client';
import * as z from 'zod';
const UserModel = z.object({
email: z.string().email(),
password: z.string(),
firstname: z.string(),
lastname: z.string(),
role: z.string(),
});
const UserLoginModel = z.object({
email: z.string().email(),
password: z.string(),
});
const prisma = new PrismaClient();
const getUser = (email: string) => {
const user = prisma.user.findUnique({
where: {
email,
},
});
return user;
};
const newUser = (user: User) => {
const prismaUser = prisma.user.create({
data: user,
});
return prismaUser;
};
export { getUser, newUser, UserModel, UserLoginModel };

View File

View File

@ -1,6 +1,6 @@
import express, { urlencoded, json } from 'express';
import cors from 'cors';
import api from '~api';
import api from '@/api';
const port = process.env.PORT || 3000;

View File

@ -17,10 +17,10 @@
"outDir": "./dist",
"strictPropertyInitialization": false,
"paths": {
"~*": [
"@/*": [
"./src/*"
],
"~~/*": [
"@@/*": [
"./*"
]
}