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
4 changes: 4 additions & 0 deletions modulo5/knex-js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
29 changes: 29 additions & 0 deletions modulo5/knex-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "aula-knex",
"version": "1.0.0",
"main": "index.js",


"scripts": {
"dev": "clear && ts-node-dev ./src/index.ts",
"start": "tsc && node ./build/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Labenu",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.95.2",
"mysql": "^2.18.1"
},
"devDependencies": {
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/knex": "^0.16.1",
"@types/node": "^14.14.35",
"ts-node-dev": "^1.1.6",
"typescript": "^4.2.3"
}
}
47 changes: 47 additions & 0 deletions modulo5/knex-js/src/Ex01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Request, Response} from "express"
import { connection } from "./connection"

// a)
// É retornado um array com a informações solicitadas pelo usuario

// b)
export const actors = async (req: Request, res: Response) => {

try {
const name = req.query.name
const result = await connection.raw
(
`SELECT * from Actor
WHERE name = "${name}"`
)
console.log(result)
res.status(200).send(result[0])
}

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

}

// c)

export const gen = async (req: Request, res: Response) => {

try {
const gender = req.query.gender
const result = await connection.raw
(
`SELECT COUNT (*) from Actor
WHERE gender = "${gender}"`
)
console.log(result)
res.status(200).send(result[0])
}

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

}

40 changes: 40 additions & 0 deletions modulo5/knex-js/src/Ex02.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Request, Response} from "express"
import { connection } from "./connection"


export const addMoney = async (req: Request, res: Response) => {
const id = req.params.id;
const { salary } = req.body;

try {
await connection("Actor")
.update({
salary,
})
.where({
id
});
res.send("Ok");
} catch(e) {
console.error({e});
return res.status(500).send("Algo deu errado.");
}
};

// app.delete('/actor/:id', async (req, res) => {
// // const id = req.params.id;
// // const { id } = req.params;

// try {
// await connection("Actor")
// .delete()
// .where({
// id: req.params.id
// });
// res.send("Ok");

// } catch(e) {
// console.error({e});
// return res.status(500).send("Algo deu errado.");
// }
// });
18 changes: 18 additions & 0 deletions modulo5/knex-js/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import express from "express";
import cors from "cors";
import { AddressInfo } from "net";

export const app = 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.`);
}
});

17 changes: 17 additions & 0 deletions modulo5/knex-js/src/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import knex from "knex";
import dotenv from "dotenv";

dotenv.config();

export const connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SCHEMA,
multipleStatements: true
},
});

12 changes: 12 additions & 0 deletions modulo5/knex-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { app } from "./app"
import {Request, Response} from "express"
import { actors, gen } from "./Ex01"
import { addMoney } from "./Ex02"

app.get("/actor/", actors)

app.get("/gender/", gen)

app.put("/addSalary/:id", addMoney)


14 changes: 14 additions & 0 deletions modulo5/knex-js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}