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

(CarAdHouse) - Marcelo Lima, projeto finalizado! #21

Open
wants to merge 8 commits 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
1 change: 1 addition & 0 deletions API/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions API/.sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path')

module.exports = {
config: path.resolve(__dirname, 'src', 'config', 'database.js'),
'migrations-path': path.resolve(__dirname, 'src', 'database', 'migrations')
}
14 changes: 14 additions & 0 deletions API/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: ''

services:
db:
image: mysql
command: --default-authentication-plugin=caching_sha2_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: admin
MYSQL_USER: admin
MYSQL_PASSWORD: admin
ports:
- 3306:3306
4,159 changes: 4,159 additions & 0 deletions API/package-lock.json

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions API/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "back-sequelize",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev --transpile-only src/server.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"@types/validator": "^13.7.10",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^8.5.1",
"mysql2": "^2.3.3",
"reflect-metadata": "^0.1.13",
"sequelize": "^6.25.2",
"sequelize-typescript": "^2.1.5",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0"
},
"devDependencies": {
"sequelize-cli": "^6.5.1"
}
}
3 changes: 3 additions & 0 deletions API/src/config/auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"secret": "057520ca7c645eb50446165522615426"
}
12 changes: 12 additions & 0 deletions API/src/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
dialect: 'mysql',
host: 'localhost',
/* port: 8080, */
username: 'admin',
password: 'admin',
database: 'admin',
define: {
timestamps: true, // created_at, updated_at
underscored: true // snake case, padrão pascal case
}
}
169 changes: 169 additions & 0 deletions API/src/controllers/CarroController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { Request, Response } from 'express'
import { Carro } from '../models/Carro'
import { Op } from 'sequelize'

module.exports = {
async listar(req: Request, res: Response) {
try {
const carros: Carro = await Carro.findAll({
include: { association: 'dono_carro' }
})

// pegar buffer do banco e transformar em base64 novamente
for(let i = 0; i < carros.length; i++) {
carros[i].foto_carro = carros[i].foto_carro ? carros[i].foto_carro.toString('base64') : carros[i].foto_carro
}

return res.json(carros)

} catch (error) {
console.log(error)
}

},

async criar(req: Request, res: Response) {
try {
const {
nome,
marca,
ano_fabricacao,
descricao,
dono_carro_id,
foto_carro
} = req.body

const carro: Carro = await Carro.create({
nome,
marca,
ano_fabricacao,
descricao,
dono_carro_id,
foto_carro: Buffer.from(foto_carro, "base64"),
ativo: "true"
})

return res.json(carro)

} catch (error) {
console.log(error)
}

},

async editar(req: Request, res: Response) {
try {
const { id } = req.params
const {
nome,
marca,
ano_fabricacao,
descricao,
dono_carro_id,
foto_carro,
ativo
} = req.body

let carro: Carro = await Carro.findByPk(id);

if(!carro) {
return res.status(400).json({ error: 'carro não encontrado' })
}

carro.nome = nome ? nome : carro.nome
carro.marca = marca ? marca : carro.marca
carro.ano_fabricacao = ano_fabricacao ? ano_fabricacao : carro.ano_fabricacao
carro.descricao = descricao ? descricao : carro.descricao

// CONFIFURAR EDIÇÃO DE IMAGEM PARA BASE64
carro.foto_carro = foto_carro ? Buffer.from(foto_carro, "base64") : carro.foto_carro

carro.dono_carro_id = dono_carro_id ? dono_carro_id : carro.dono_carro_id
carro.ativo = ativo ? ativo : carro.ativo /* <- para que o valor seja editado, é necessário passar o false/true como string */

await carro.save()

return res.json(carro)

} catch (error) {
console.log(error)
}
},

async deletar(req: Request, res: Response) {
try {
const { id } = req.params

if(!await Carro.findByPk(id)) {
return res.status(400).json({ error: 'Carro não encontrado' })
}

await Carro.destroy({ where: { id } })

return res.status(204).end()

} catch (error) {
console.log(error)
}
},

async buscar(req: Request, res: Response) {
try {
const { nome } = req.params

const carros = await Carro.findAll({
where: {
nome: {
[Op.like]: `${nome}%`
}
}})

if(!carros) {
return res.status(400).json({ error: 'Carro não encontrado' })
}

for(let i = 0; i < carros.length; i++) {
carros[i].foto_carro = carros[i].foto_carro ? carros[i].foto_carro.toString('base64') : carros[i].foto_carro
}

return res.json(carros)

} catch (error) {
console.log(error)
}
},

async buscarUm(req: Request, res: Response) {
try {
const { id } = req.params

const carro: Carro = await Carro.findByPk(id, {
include: { association: 'dono_carro' }
});

// pegar buffer do banco e transformar em base64 novamente
carro.foto_carro = carro.foto_carro ? carro.foto_carro.toString('base64') : carro.foto_carro;

if(!carro) {
return res.status(400).json({ error: 'Carro não encontrado' });
}

return res.json(carro);

} catch (error) {
console.log(error);
}
},

async buscarPorDono(req: Request, res: Response) {
const { dono_carro_id } = req.body

const carros: Carro = await Carro.findAll({ where: { dono_carro_id } })

if(!carros) {
return res.status(400).json({ error: 'Carro não encontrado' })
}

return res.json(carros)
}
}
149 changes: 149 additions & 0 deletions API/src/controllers/DonoCarroController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { Request, Response } from 'express'
import { DonoCarro } from '../models/DonoCarro'
import { Op } from 'sequelize'

type DonoCarroType = {
nome: string
email: string
telefone: string
foto_perfil: Buffer
}

module.exports = {
async listar(req: Request, res: Response) {
try {
const donosCarro: DonoCarro[] = await DonoCarro.findAll()

// pegar buffer do banco e transformar em base64 novamente
for(let i = 0; i < donosCarro.length; i++) {
donosCarro[i].foto_perfil = donosCarro[i].foto_perfil ? donosCarro[i].foto_perfil.toString('base64') : donosCarro[i].foto_perfil;
}

return res.json(donosCarro)

} catch (error) {
console.log(error)
}
},

async criar(req: Request, res: Response) {
try {
const {
nome,
email,
telefone,
// foto_perfil

} = req.body

const donoCarro: DonoCarroType = await DonoCarro.create({
nome,
email,
telefone,
// foto_perfil: Buffer.from(foto_perfil, "base64"),
ativo: "true"
})

return res.json(donoCarro)

} catch (error) {
console.log(error)
}

},

async editar(req: Request, res: Response) {
try {
const { id } = req.params
const {
nome,
email,
telefone,
// foto_perfil,
ativo

} = req.body

let donoCarro: DonoCarro = await DonoCarro.findByPk(id);

if(!donoCarro) {
return res.status(400).json({ error: 'Dono do carro não encontrado' })
}

donoCarro.nome = nome ? nome : donoCarro.nome
donoCarro.email = email ? email : donoCarro.email
donoCarro.telefone = telefone ? telefone : donoCarro.telefone

// CONFIFURAR EDIÇÃO DE IMAGEM PARA BASE64
// donoCarro.foto_perfil = foto_perfil ? Buffer.from(foto_perfil, "base64") : donoCarro.foto_perfil

donoCarro.ativo = ativo ? ativo : donoCarro.ativo /* <- para que o valor seja editado, é necessário passar o false/true como string */

await donoCarro.save()

return res.json(donoCarro)

} catch (error) {
console.log(error)
}
},

async deletar(req: Request, res: Response) {
try {
const { id } = req.params

if(!await DonoCarro.findByPk(id)) {
return res.status(400).json({ error: 'Dono do carro não encontrado' })
}

await DonoCarro.destroy({ where: { id } })

return res.status(204).end()

} catch (error) {
console.log(error)
}
},

async buscar(req: Request, res: Response) {
try {
const { nome } = req.body

const donosCarro = await DonoCarro.findAll({
where: {
nome: {
[Op.like]: `${nome}%`
}
}})

if(!donosCarro) {
return res.status(400).json({ error: 'Dono do carro não encontrado' })
}

return res.json(donosCarro)

} catch (error) {
console.log(error)
}
},

async buscarUm(req: Request, res: Response) {
try {
const { id } = req.params

const donoCarro: DonoCarro = await DonoCarro.findByPk(id);

// pegar buffer do banco e transformar em base64 novamente
donoCarro.foto_perfil = donoCarro.foto_perfil ? donoCarro.foto_perfil.toString('base64') : donoCarro.foto_perfil

if(!donoCarro) {
return res.status(400).json({ error: 'Dono do carro não encontrado' })
}

return res.json(donoCarro)

} catch (error) {
console.log(error)
}
}
}
Loading