diff --git a/.gitignore b/.gitignore index bb24732..aa5e9b5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ dist-ssr *.sqlite *.db -.env \ No newline at end of file +.env +.prisma/ \ No newline at end of file diff --git a/src/api/auth/index.ts b/src/api/auth/index.ts new file mode 100644 index 0000000..35733a2 --- /dev/null +++ b/src/api/auth/index.ts @@ -0,0 +1,8 @@ +import { Router } from 'express'; +import login from './login'; + +const auth = Router(); + +auth.use('/login', login); + +export default auth; diff --git a/src/api/auth/login.ts b/src/api/auth/login.ts new file mode 100644 index 0000000..ece0bf1 --- /dev/null +++ b/src/api/auth/login.ts @@ -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; diff --git a/src/api/auth/signup.ts b/src/api/auth/signup.ts new file mode 100644 index 0000000..d0e8b7a --- /dev/null +++ b/src/api/auth/signup.ts @@ -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); + } +}; diff --git a/src/api/index.ts b/src/api/index.ts index 23a9b52..dc053e5 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -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; diff --git a/src/api/posts.ts b/src/api/posts.ts new file mode 100644 index 0000000..079c7a2 --- /dev/null +++ b/src/api/posts.ts @@ -0,0 +1,9 @@ +import { Router } from 'express'; + +const posts = Router(); + +posts.get('/', (req, res) => { + res.send('Hello World!'); +}); + +export default posts; diff --git a/src/api/user.ts b/src/api/user.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/controller/index.ts b/src/controller/index.ts new file mode 100644 index 0000000..4e54f7a --- /dev/null +++ b/src/controller/index.ts @@ -0,0 +1,5 @@ +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +export default prisma; diff --git a/src/api/post.ts b/src/controller/post.ts similarity index 100% rename from src/api/post.ts rename to src/controller/post.ts diff --git a/src/api/role.ts b/src/controller/role.ts similarity index 100% rename from src/api/role.ts rename to src/controller/role.ts diff --git a/src/controller/user.ts b/src/controller/user.ts new file mode 100644 index 0000000..3613085 --- /dev/null +++ b/src/controller/user.ts @@ -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 }; diff --git a/src/db.ts b/src/db.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/index.ts b/src/index.ts index 905cfeb..56be7c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; diff --git a/tsconfig.json b/tsconfig.json index 301ac6c..31f8ca8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,10 +17,10 @@ "outDir": "./dist", "strictPropertyInitialization": false, "paths": { - "~*": [ + "@/*": [ "./src/*" ], - "~~/*": [ + "@@/*": [ "./*" ] }