Skip to content
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
37 changes: 0 additions & 37 deletions README.md

This file was deleted.

4 changes: 4 additions & 0 deletions pasta-quarta/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
3 changes: 3 additions & 0 deletions pasta-quarta/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Exercício 1
a) Quando usamos o Raw retorna um array com mais de uma posição dai
para termos uma resposta mais especifica podemos especificar a posição [0].
28 changes: 28 additions & 0 deletions pasta-quarta/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "pasta-quarta",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"ts-node-dev": "^1.1.6",
"typescript": "^4.2.3"
},
"dependencies": {
"@types/knex": "^0.16.1",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.95.2",
"mysql": "^2.18.1"
}
}
43 changes: 43 additions & 0 deletions pasta-quarta/requests.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
POST http://localhost:3003/actor
Content-Type:application/json

{
"name": "Keanu Reaves",
"gender": "male",
"birthDate": "1964-09-02",
"salary": "1000000"
}

###

GET http://localhost:3003/actor



###
GET http://localhost:3003/actor/name

###
GET http://localhost:3003/actor/gender-count

###
PUT http://localhost:3003/actor/001
Content-Type: application/json

{
"gender": "male",
"salary": "700000"
}

###
POST http://localhost:3003/actor/delete/001

###
POST http://localhost:3003/actor/media/male

###
GET http://localhost:3003/actor/002

###
GET http://localhost:3003/actor/count/?gender=female

19 changes: 19 additions & 0 deletions pasta-quarta/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import express, {Express} from 'express'
import cors from 'cors'
import { AddressInfo } from "net";

const app: Express = express();
app.use(express.json());
app.use(cors());

const server = app.listen(process.env.PORT || 3003, () => {
if (server) {
const address = server.address() as AddressInfo;
console.log(`Server is running in http://localhost: ${address.port}`);
} else {
console.error(`Failure upon starting server.`);
}
});


export default app
21 changes: 21 additions & 0 deletions pasta-quarta/src/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import knex from "knex"; //Importar KNEX no projeto
import dotenv from "dotenv"; //

dotenv.config();
/* DB_HOST: 35.226.146.116
DB_USER: franciane-brito
DB_PASS: IiaTYP1UTqm~4TaPmLww
DB_NAME: epps-franciane-brito */
export const connection = knex({
client: "mysql",
connection: {
host: "35.226.146.116",
user: "franciane-brito",
password: "IiaTYP1UTqm~4TaPmLww",
database: "epps-franciane-brito",
port: 3306,
multipleStatements: true
}
});

export default connection
172 changes: 172 additions & 0 deletions pasta-quarta/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import app from "./app"
import connection from "./connection"
import express, { Express, Request, Response } from 'express'

app.post("/actor", async (req, res) => {
try {
connection.raw(
`ÌNSERT INTO Actor(id, name, gender, birth_date, salary)
VALUES(
"${Date.now()}",
"${req.body.name}",
"${req.body.gender}",
"${req.body.birth_date}",
${req.body.salary},
)`
)
res.status(201).send("Created!")
} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

app.get("/actor", async (req, res) => {
try {
const result = await connection(`Actor`)
res.status(201).send(result)

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

// app.put("/actor/:id", async (req, res) => {
// try {
// await connection("Actor")
// .update({
// salary: req.body.salary,
// birth_Date: req.body.birthDate
// })
// .where({
// name: req.params.name
// })
// res.send("Created!")

// } catch (error) {
// console.log(error.mensage)
// res.status(500).send("Internal server error")
// }
// })

// Exercicio 1
app.get("/actor/name", async (req, res) => {
try {
const result = await connection.raw(`
SELECT * FROM Actor WHERE name = "Juliana Paes"
`)

res.status(200).send(result)
} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})


app.get("/actor/gender-count", async (req, res) => {
try {
const result = await connection.raw(`
SELECT COUNT(*) as count FROM Actor WHERE gender = "male"
`);
const count = result[0][0];
res.status(200).send(count)

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

// Exercicio 2
app.put("/actor/:id", async (req, res) => {
try {

await connection("Actor")
.update({
salary: req.body.salary,
})
.where({
name: req.params.name
})
res.send("Created!")

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

app.post("/actor/delete/:id", async (req, res) => {
try {
await connection("Actor")
.delete()
.where(
"id", req.params.id
)
res.send("Delete!!!!!")

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

app.post("/actor/media/:gender", async (req, res) => {
try {
const result = await connection("Actor")
.avg("salary")
.where(
"gender", req.params.gender
)
res.send(result)

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})


// Exercício 3
app.get("/actor/:id", async (req, res) => {
try {
const result = await connection("Actor")
.where("id", req.params.id)
res.status(201).send(result)

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

app.get("/actor/count/search", async (req, res) => {
try {
const result = await connection("Actor")
.where(
"gender", req.params.gender
)


res.status(202).send(result)

} catch (error) {
console.log(error.mensage)
res.status(500).send("Internal server error")
}
})

app.post("/actor", async (req: Request, res: Response) => {
try {
await updateSalary(req.body.id, req.body.salary);
res.status(200).send({
message: "Success",
});
} catch (err) {
res.status(400).send({
message: err.message,
});
}
});
Loading