restructurize code in api and controller folder
This commit is contained in:
parent
d94f22a3dc
commit
6d4822e50d
|
|
@ -26,3 +26,4 @@ dist-ssr
|
|||
*.sqlite
|
||||
*.db
|
||||
.env
|
||||
.prisma/
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import { Router } from 'express';
|
||||
import login from './login';
|
||||
|
||||
const auth = Router();
|
||||
|
||||
auth.use('/login', login);
|
||||
|
||||
export default auth;
|
||||
|
|
@ -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;
|
||||
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
import { Router } from 'express';
|
||||
|
||||
const posts = Router();
|
||||
|
||||
posts.get('/', (req, res) => {
|
||||
res.send('Hello World!');
|
||||
});
|
||||
|
||||
export default posts;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export default prisma;
|
||||
|
|
@ -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 };
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@
|
|||
"outDir": "./dist",
|
||||
"strictPropertyInitialization": false,
|
||||
"paths": {
|
||||
"~*": [
|
||||
"@/*": [
|
||||
"./src/*"
|
||||
],
|
||||
"~~/*": [
|
||||
"@@/*": [
|
||||
"./*"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue