From 21652a623bb89b96c9347ccbda59ff1fabe78766 Mon Sep 17 00:00:00 2001 From: Augusto Luna Date: Fri, 15 Jan 2021 17:11:38 -0300 Subject: [PATCH] Projeto incompleto semana2 backend --- .../template-express-knex/.DS_Store | Bin 0 -> 6148 bytes .../template-express-knex/.gitignore | 4 + .../template-express-knex/package.json | 28 ++++ .../template-express-knex/src/.DS_Store | Bin 0 -> 6148 bytes .../template-express-knex/src/index.ts | 132 ++++++++++++++++++ .../template-express-knex/tsconfig.json | 11 ++ 6 files changed, 175 insertions(+) create mode 100644 Backend/Semana 2/projeto-backend-semana2/template-express-knex/.DS_Store create mode 100644 Backend/Semana 2/projeto-backend-semana2/template-express-knex/.gitignore create mode 100644 Backend/Semana 2/projeto-backend-semana2/template-express-knex/package.json create mode 100644 Backend/Semana 2/projeto-backend-semana2/template-express-knex/src/.DS_Store create mode 100644 Backend/Semana 2/projeto-backend-semana2/template-express-knex/src/index.ts create mode 100644 Backend/Semana 2/projeto-backend-semana2/template-express-knex/tsconfig.json diff --git a/Backend/Semana 2/projeto-backend-semana2/template-express-knex/.DS_Store b/Backend/Semana 2/projeto-backend-semana2/template-express-knex/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 => { + + try { + await connection + .insert({ + id: String(Date.now()), + name, + nickname, + email + }) + .into("TodoListUser "); + } catch (error) { + console.log(error) + } + +} + +app.post("/user", async (req: Request, res: Response) => { + try { + + //Validação todos os campos obrigatórios + const keys = Object.keys(req.body) + + for (const key of keys) { + if (req.body[key] == "") + return res.status(400).send({ message: "Por gentileza preencha todos os campos corretamente!"}) + } + + const {name, nickname, email} = req.body + + await createUser(name, nickname, email); + + res.status(200).send("Usuário criado com sucesso"); + } catch (err) { + res.status(400).send({ + message: err.message, + }); + } + }); + +// ENDPOINT 2 - PEGA USUÁRIOS PELO ID + + const getUserById = async (id: string): Promise => { + const result = await connection.raw(` + SELECT * FROM TodoListUser WHERE id = "${id}" + `); + return result; + }; + + app.get("/user/:id", async (req: Request, res: Response) => { + try { + const id = req.params.id + const user = await getUserById(id); + + res.status(200).send(user); + } catch (err) { + res.status(400).send({ + message: err.message, + }); + } + }); + + + //ENDPOINT 3 - EDITAR USUÁRIO + + const updateUser = async (id: string, name: string, nickname: string, email: string): Promise => { + const result = await connection.raw(` + UPDATE TodoListUser + SET + name = "${name}", + nickname = "${nickname}", + email = "${email}" + WHERE + id = ${id} + `) + return result[0] + } + + app.post("user/edit/:id", async (req: Request, res: Response) => { + let errorCode: number = 400; + try{ + const id = req.params.id + const name = req.body.name + const nickname = req.body.nickname + const email = req.body.email + + if(!req.params.id || !req.body.name || !req.body.nickname || req.body.email){ + errorCode = 422 + throw new Error("Dados faltantes ou incorretos. Tente novamente.") + } + const result = await updateUser(id, name, nickname, email) + res.status(200).send(result) + } catch (error) { + res.status(400).send({ + message: error.message, + }); + } + }) + +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/Backend/Semana 2/projeto-backend-semana2/template-express-knex/tsconfig.json b/Backend/Semana 2/projeto-backend-semana2/template-express-knex/tsconfig.json new file mode 100644 index 0000000..b520b3a --- /dev/null +++ b/Backend/Semana 2/projeto-backend-semana2/template-express-knex/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": "./" /* 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. */ + } +}