From 79151e193eadbc6c8e0d04d0fde517dbd6b7b0c0 Mon Sep 17 00:00:00 2001 From: alainn-n Date: Thu, 9 Jun 2022 22:48:50 -0300 Subject: [PATCH] Entrega Exercicios Aula - APIs REST --- modulo5/apis-rest/.gitignore | 3 ++ modulo5/apis-rest/package.json | 24 +++++++++++++ modulo5/apis-rest/src/data.ts | 61 +++++++++++++++++++++++++++++++++ modulo5/apis-rest/src/index.ts | 44 ++++++++++++++++++++++++ modulo5/apis-rest/src/server.ts | 19 ++++++++++ modulo5/apis-rest/tsconfig.json | 11 ++++++ 6 files changed, 162 insertions(+) create mode 100644 modulo5/apis-rest/.gitignore create mode 100644 modulo5/apis-rest/package.json create mode 100644 modulo5/apis-rest/src/data.ts create mode 100644 modulo5/apis-rest/src/index.ts create mode 100644 modulo5/apis-rest/src/server.ts create mode 100644 modulo5/apis-rest/tsconfig.json diff --git a/modulo5/apis-rest/.gitignore b/modulo5/apis-rest/.gitignore new file mode 100644 index 0000000..a049075 --- /dev/null +++ b/modulo5/apis-rest/.gitignore @@ -0,0 +1,3 @@ +node_modules +package-lock.json +build \ No newline at end of file diff --git a/modulo5/apis-rest/package.json b/modulo5/apis-rest/package.json new file mode 100644 index 0000000..31329be --- /dev/null +++ b/modulo5/apis-rest/package.json @@ -0,0 +1,24 @@ +{ + "name": "apis-rest", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "dev": "ts-node-dev ./scr/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.12", + "@types/express": "^4.17.13", + "ts-node-dev": "^2.0.0", + "typescript": "^4.7.3" + }, + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.1" + } + } \ No newline at end of file diff --git a/modulo5/apis-rest/src/data.ts b/modulo5/apis-rest/src/data.ts new file mode 100644 index 0000000..698747b --- /dev/null +++ b/modulo5/apis-rest/src/data.ts @@ -0,0 +1,61 @@ +export enum USER_TYPE{ + ADMIN = "ADMIN", + NORMAL = "NORMAL" +} + + +export type User = { + id: number, + name: string, + email: string, + type: USER_TYPE, + age: number +} + + + + +export let users :User[] = [ + { + id: 1, + name: "Alice", + email: "alice@email.com", + type: USER_TYPE.NORMAL, + age: 12 + }, + { + id: 2, + name: "Bob", + email: "bob@email.com", + type: USER_TYPE.ADMIN, + age: 36 + }, + { + id: 3, + name: "Coragem", + email: "coragem@email.com", + type: USER_TYPE.ADMIN, + age: 21 + }, + { + id: 4, + name: "Dory", + email: "dory@email.com", + type: USER_TYPE.ADMIN, + age: 17 + }, + { + id: 5, + name: "Elsa", + email: "elsa@email.com", + type: USER_TYPE.NORMAL, + age: 17 + }, + { + id: 6, + name: "Fred", + email: "fred@email.com", + type: USER_TYPE.ADMIN, + age: 60 + } +] \ No newline at end of file diff --git a/modulo5/apis-rest/src/index.ts b/modulo5/apis-rest/src/index.ts new file mode 100644 index 0000000..bd86b89 --- /dev/null +++ b/modulo5/apis-rest/src/index.ts @@ -0,0 +1,44 @@ +import express, { Request, Response } from "express"; +import cors from "cors" +import app from "./server" +import { User, users, USER_TYPE } from "./data"; + +const servidor = app + +app.get("/users/type", (req: Request, res: Response) => { + try {const type = req.query.type as string + if(!type){ + throw new Error("Não encontrado") + } + else{const userFiltro = users.filter(usuario => usuario.type.toLocaleLowerCase() === type.toLocaleLowerCase()) + if(!userFiltro.length){ + throw new Error("usuario não encontrado"); + } + res.status(200).send(userFiltro) + } + + } + catch (error) { + res.status(500).send(error) + } +}) + + +app.get("/users/name", (req: Request, res: Response) => { + try { + const name = req.query.name as string + if(!name){ + throw new Error("Não encontrado a key") + } + else{const userFiltro = users.filter(usuario => usuario.name.toLocaleLowerCase() === name.toLocaleLowerCase()) + if(!userFiltro.length){ + throw new Error("usuario não encontrado"); + } + res.status(200).send(userFiltro) + } + + } + catch (error) { + res.status(500).send(error) + } +}) \ No newline at end of file diff --git a/modulo5/apis-rest/src/server.ts b/modulo5/apis-rest/src/server.ts new file mode 100644 index 0000000..eb87f7b --- /dev/null +++ b/modulo5/apis-rest/src/server.ts @@ -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; \ No newline at end of file diff --git a/modulo5/apis-rest/tsconfig.json b/modulo5/apis-rest/tsconfig.json new file mode 100644 index 0000000..8fbf190 --- /dev/null +++ b/modulo5/apis-rest/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, + "outDir": "./build" /* Redirect output structure to the directory. */, + "rootDir": "./scr" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + "strict": true /* Enable all strict type-checking options. */, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + } + } \ No newline at end of file