diff --git a/modulo5/knex-js/.gitignore b/modulo5/knex-js/.gitignore new file mode 100644 index 0000000..8ece3ba --- /dev/null +++ b/modulo5/knex-js/.gitignore @@ -0,0 +1,4 @@ +node_modules +package-lock.json +build +.env \ No newline at end of file diff --git a/modulo5/knex-js/package.json b/modulo5/knex-js/package.json new file mode 100644 index 0000000..7fb1e1b --- /dev/null +++ b/modulo5/knex-js/package.json @@ -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" + } +} diff --git a/modulo5/knex-js/src/Ex01.ts b/modulo5/knex-js/src/Ex01.ts new file mode 100644 index 0000000..328dc7a --- /dev/null +++ b/modulo5/knex-js/src/Ex01.ts @@ -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) + } + +} + \ No newline at end of file diff --git a/modulo5/knex-js/src/Ex02.ts b/modulo5/knex-js/src/Ex02.ts new file mode 100644 index 0000000..a2388a4 --- /dev/null +++ b/modulo5/knex-js/src/Ex02.ts @@ -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."); +// } +// }); \ No newline at end of file diff --git a/modulo5/knex-js/src/app.ts b/modulo5/knex-js/src/app.ts new file mode 100644 index 0000000..5e7b747 --- /dev/null +++ b/modulo5/knex-js/src/app.ts @@ -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.`); + } +}); + diff --git a/modulo5/knex-js/src/connection.ts b/modulo5/knex-js/src/connection.ts new file mode 100644 index 0000000..50ff314 --- /dev/null +++ b/modulo5/knex-js/src/connection.ts @@ -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 + }, +}); + diff --git a/modulo5/knex-js/src/index.ts b/modulo5/knex-js/src/index.ts new file mode 100644 index 0000000..48f2717 --- /dev/null +++ b/modulo5/knex-js/src/index.ts @@ -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) + + diff --git a/modulo5/knex-js/tsconfig.json b/modulo5/knex-js/tsconfig.json new file mode 100644 index 0000000..b6574ed --- /dev/null +++ b/modulo5/knex-js/tsconfig.json @@ -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 + } +} \ No newline at end of file