-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from FGA0138-MDS-Ajax/homol
Finalização da estrutura do Backend
- Loading branch information
Showing
18 changed files
with
4,681 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
Dockerfile | ||
docker-compose.yml | ||
.dockerignore | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM node:18-alpine | ||
|
||
RUN npm install -g yarn@latest mysql2 | ||
|
||
RUN mkdir -p /home/node/src/node_modules && chown -R node:node /home/node/src | ||
|
||
WORKDIR /home/node/src | ||
|
||
COPY package.json ./ | ||
|
||
RUN yarn | ||
|
||
COPY --chown=node:node . . | ||
|
||
EXPOSE 3001 | ||
|
||
CMD ["yarn", "dev"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
version: '3' | ||
|
||
services: | ||
database: | ||
image: postgres | ||
container_name: Matcher_DB | ||
restart: always | ||
ports: | ||
- 5432:5432 | ||
environment: | ||
- POSTGRES_HOST=localhost | ||
- POSTGRES_USER=dev_usermatcher | ||
- POSTGRES_PASSWORD=matcher_devpass | ||
- POSTGRES_DB=matcher_db | ||
volumes: | ||
- pgdata:/data/postgres | ||
|
||
volumes: | ||
pgdata: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"env": { | ||
"es2021": true, | ||
"node": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"never" | ||
], | ||
"no-unused-vars": [ | ||
"off" | ||
], | ||
"no-console": [ | ||
0 | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Matcher | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
## Welcome to the development branch for the Matcher Back-end project. | ||
|
||
## Getting Started | ||
|
||
### Prerequisites | ||
|
||
- Install [NodeJS](https://nodejs.org/en) version 18.17.1 LTS or higher. | ||
- Install [Yarn](https://yarnpkg.com/) | ||
version 1.22.19 or higher. | ||
|
||
### Installation | ||
|
||
First, install the project dependencies using the command: | ||
|
||
```bash | ||
yarn | ||
``` | ||
|
||
Run the development server: | ||
|
||
```bash | ||
yarn dev | ||
``` | ||
|
||
## How to start | ||
|
||
- To initialize, run the above step by typing ```bash yarn dev``` | ||
|
||
## Database | ||
|
||
### Prerequisites | ||
|
||
- Install [Docker](https://docker.com) | ||
- Install [Docker-Compose](https://docs.docker.com/compose/) | ||
- Recommended to install Docker Extension on Visual Studio Code | ||
|
||
## Initialization | ||
|
||
- To initialize the database, with all the requirements met, you can go to the ```.docker``` folder and open the ```docker-compose.yml``` file. | ||
|
||
- Right-click and execute the ```Compose Up``` command. This will run a command in the terminal: | ||
``` | ||
docker compose -f "backend\.docker\docker-compose.yml" up -d --build | ||
``` | ||
Which will download the database image and start the database container. | ||
|
||
- Once the process is complete, the database will be available for connection. I recommend using Beekeeper as a database manager. From there, you can connect to the database and access its tables. | ||
|
||
- Finally, we need to configure [Prisma](https://prisma.io). To do this, open the terminal in the ```backend``` folder and enter the following command: | ||
``` | ||
npx prisma init --datasource-provider postgresql | ||
``` | ||
|
||
- After running the above command, Prisma will initialize the ```Prisma``` folder and a ```.env``` file with the ```schema.prisma``` file. The ```schema.prisma``` file contains the model instances that we will use in the project, and the ```.env``` file contains our environment variables. | ||
|
||
- Go to the ```.env``` file and configure the ```DATABASE_URL``` variable in the following format: | ||
``` | ||
postgresql://USER:PASSWORD@HOST:5432/DATABASE?schema=public | ||
``` | ||
For example: | ||
``` | ||
postgresql://philipe:senhaphilipe@localhost:5432/philipe_db?schema=public | ||
``` | ||
|
||
- The environment variables used to configure the ```DATABASE_URL``` should be the same as those defined in the ```docker-compose.yml``` file in the ```.docker``` folder. | ||
|
||
###### Example of Environment Variables used in the format: | ||
``` | ||
- POSTGRES_HOST=localhost | ||
- POSTGRES_USER=philipe | ||
- POSTGRES_PASSWORD=senhaphilipe | ||
- POSTGRES_DB=philipe_db | ||
``` | ||
|
||
- After making this configuration, go to the ```schema.prisma``` file and define a Model for testing. Example: | ||
|
||
``` | ||
model User { | ||
id Int @id @default(autoincrement ()) | ||
email String @unique | ||
name String? | ||
password String | ||
} | ||
``` | ||
|
||
- Then, simply run the command: | ||
``` | ||
npx prisma migrate dev --name test-table | ||
``` | ||
This command will generate a migration in the database. After that, go to your database manager (Beekeeper) and check if the User table was created in the public schema. If it is, the database is connected. | ||
|
||
## Project Structure | ||
``` | ||
├── .docker | ||
├── node_modules | ||
├── src | ||
│ ├── controllers | ||
│ ├── middlewares | ||
│ ├── tests | ||
│ ├── types | ||
│ └── ... | ||
│ └── index.ts | ||
│ └── routes.ts | ||
└── prisma | ||
│ ├── migrations | ||
│ └── schema.prisma | ||
└── ... | ||
``` | ||
|
||
## License | ||
|
||
This project is under license. See the [LICENSE](LICENSE) file for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "backend_matcher", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "tsx src/index.ts", | ||
"dev": "tsx watch src/index.ts", | ||
"build": "tsup src", | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"dependencies": { | ||
"@prisma/client": "^5.5.2", | ||
"bcrypt": "^5.1.1", | ||
"eslint": "^8.52.0", | ||
"express": "^4.18.2", | ||
"jsonwebtoken": "^9.0.2", | ||
"mysql": "^2.18.1" | ||
}, | ||
"devDependencies": { | ||
"@rocketseat/eslint-config": "^2.1.0", | ||
"@types/bcrypt": "^5.0.1", | ||
"@types/express": "^4.17.20", | ||
"@types/jsonwebtoken": "^9.0.4", | ||
"@types/node": "^20.8.9", | ||
"@typescript-eslint/eslint-plugin": "^6.9.0", | ||
"@typescript-eslint/parser": "^6.9.0", | ||
"@vitest/coverage-v8": "^0.34.6", | ||
"dotenv": "^16.3.1", | ||
"prisma": "^5.5.2", | ||
"tsup": "^7.2.0", | ||
"tsx": "^3.14.0", | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.6", | ||
"zod": "^3.22.4" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/prisma/migrations/20231030163615_test_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" SERIAL NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"name" TEXT, | ||
"password" TEXT NOT NULL, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
email String @unique | ||
name String? | ||
password String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
import { Request, Response } from 'express' | ||
import bcrypt from 'bcrypt' | ||
import jwt from 'jsonwebtoken' | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
const prisma = new PrismaClient() | ||
|
||
export class UserController { | ||
async create(req: Request, res: Response) { | ||
const { name, email, password } = req.body | ||
|
||
const userExists = await prisma.user.findUnique({ where: { email } }) | ||
|
||
if (userExists) { | ||
return res.status(400).json({ error: 'Email already exists!' }) | ||
} | ||
|
||
const hashPassword = await bcrypt.hash(password, 10) | ||
|
||
const newUser = await prisma.user.create({ | ||
data: { name, email, password: hashPassword }, | ||
}) | ||
|
||
const { password: _, ...user } = newUser | ||
|
||
return res.status(201).json(user) | ||
} | ||
|
||
async login(req: Request, res: Response) { | ||
const { email, password } = req.body | ||
|
||
const user = await prisma.user.findUnique({ where: { email } }) | ||
|
||
if (!user) { | ||
return res.status(400).json({ error: 'Email or password invalids' }) | ||
} | ||
|
||
const verifyPass = await bcrypt.compare(password, user.password) | ||
|
||
if (!verifyPass) { | ||
return res.status(400).json({ error: 'Email or password invalids' }) | ||
} | ||
|
||
const token = jwt.sign({ id: user.id }, process.env.JWT_PASS ?? '', { | ||
expiresIn: '1d', | ||
}) | ||
|
||
const { password: _, ...userLogin } = user | ||
|
||
return res.status(200).json({ user: userLogin, token: token }) | ||
} | ||
|
||
async getProfile(req: Request, res: Response) { | ||
const user = await prisma.user.findUnique({ where: { id: req.user.id } }) | ||
|
||
return res.json(user) | ||
} | ||
} |
Oops, something went wrong.