Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E-mail: wildersonazevedo@gmail.com #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

/node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
18 changes: 18 additions & 0 deletions Backend/Routes/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import express from 'express'

import { getCars, addCars, updateCar, getUserById, deleteCar } from "../controllers/ProductController.js";

const router = express.Router();

router.get("/carros", getCars)

router.post("/cadastrar", addCars)

router.put("/carros/:id", updateCar)

router.get("/carros/:id", getUserById)

router.delete("/carros/:id", deleteCar)

export default router;
61 changes: 61 additions & 0 deletions Backend/controllers/ProductController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Connect } from '../database/PostgresDB.js'

export const getCars = async (_, res) => {
try {
const response = await Connect.query('SELECT * FROM Carros2 ORDER BY id ASC');
return res.status(200).json(response.rows);
} catch (err) {
console.log(err);
return res.status(500).json('Internal Server error');
}
};

export const addCars = async (req, res) => {
try {
const { nome_carro, marca, ano_fabrica, description, proprietario, email, fone } = req.body;
const response =
await Connect.query('INSERT INTO Carros2 (nome_carro, marca, ano_fabrica, description, proprietario, email, fone) VALUES ($1, $2, $3, $4, $5, $6, $7)',
[nome_carro, marca, ano_fabrica, description, proprietario, email, fone]);
return res.status(200).json('Cadastrado com sucesso!');

} catch (err) {
console.log(err);
return res.status(500).json('Internal Server error');
}
};

export const updateCar = async (req, res) => {
try {
const id = parseInt(req.params.id);
const { nome_carro, marca, ano_fabrica, description, proprietario, email, fone } = req.body;

await Connect.query('UPDATE Carros2 SET nome_carro = $1, marca = $2, ano_fabrica = $3, description = $4, proprietario = $5, email = $6, fone =$7 WHERE id = $8',
[nome_carro, marca, ano_fabrica, description, proprietario, email, fone, id]);
return res.status(200).json('Atualizado com sucesso!');

} catch (err) {
console.log(err);
return res.status(500).json('Internal Server error');
}

};

export const getUserById = async (req, res) => {
try {
const id = parseInt(req.params.id);
const response = await Connect.query('SELECT * FROM Carros2 WHERE id = $1', [id]);
return res.json(response.rows);

} catch (err) {
console.log(err);
return res.status(500).json('Internal Server error');
}
};

export const deleteCar = async (req, res) => {
const id = parseInt(req.params.id);
await Connect.query('DELETE FROM Carros2 where id = $1', [
id
]);
res.json(`Carro com id: ${id} foi deletado com sucesso!`);
};
13 changes: 13 additions & 0 deletions Backend/database/PostgresDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import pg from "pg";

const { Pool } = pg;

export const Connect = new Pool({
host: 'localhost',
port: 5432,
user: 'postgres',
database: 'teste',
password: 'admin',
})

14 changes: 14 additions & 0 deletions Backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from 'express';
import cors from 'cors';
import router from './Routes/router.js';


const app = express()
app.use(cors())
app.use(express.json())

app.use("/", router)
app.listen(4001, function () {
console.log("Servidor listening")
}
)
Loading