diff --git a/modulo4/projeto-todolist-backend/.gitignore b/modulo4/projeto-todolist-backend/.gitignore new file mode 100644 index 0000000..8f5e467 --- /dev/null +++ b/modulo4/projeto-todolist-backend/.gitignore @@ -0,0 +1,3 @@ +node_modules +package-lock.json +.env \ No newline at end of file diff --git a/modulo4/projeto-todolist-backend/package.json b/modulo4/projeto-todolist-backend/package.json new file mode 100644 index 0000000..de12877 --- /dev/null +++ b/modulo4/projeto-todolist-backend/package.json @@ -0,0 +1,14 @@ +{ + "name": "projeto-todolist-backend", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "tsc && node ./build/app.js", + "dev": "ts-node-dev ./src/app.ts", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "Pablo Leão", + "license": "ISC" +} \ No newline at end of file diff --git a/modulo4/projeto-todolist-backend/src/app.ts b/modulo4/projeto-todolist-backend/src/app.ts new file mode 100644 index 0000000..c13a534 --- /dev/null +++ b/modulo4/projeto-todolist-backend/src/app.ts @@ -0,0 +1,176 @@ +import express from 'express'; +import cors from 'cors'; +import { AddressInfo } from 'net'; +import { connection } from './connection'; + +const app = express(); +app.use(express.json()); +app.use(cors()); + +const cyan = '\u001b[36m'; +const white = '\u001b[37m'; + +function formataDataDate(data: string): Date { + const arrayDataString = data.split('/'); + const arrayDataNumber = [Number(arrayDataString[2]), Number(arrayDataString[1]) - 1, Number(arrayDataString[0])]; + const dataFormatada = new Date(arrayDataNumber[0], arrayDataNumber[1], arrayDataNumber[2]); + return dataFormatada; +} + +app.get('/user/:id', async (req, res) => { + const id = req.params.id; + try { + const result = await connection + .from("ToDoListUser") + .select("id", "nickname") + .where("id", id); + + if (result.length === 0) { + throw new Error("Usuário não encontrado!"); + } + + res.status(200).send(result[0]); + + } catch (error: any) { + switch(error.message) { + case "Usuário não encontrado!": + res.status(404).send({ message: error.message }); + break; + default: + res.status(500).send(error.sqlMessage || error.message); + break; + } + } +}) + +app.get('/task/:id', async (req, res) => { + const id = req.params.id; + try { + const result = await connection("ToDoListTask") + .where("id", id); + + if (result.length === 0) { + throw new Error("Tarefa não encontrada!"); + } + + res.status(200).send({...result[0], limit_date: result[0].limit_date.toLocaleDateString('pt-BR')}); + + } catch (error: any) { + switch(error.message) { + case "Tarefa não encontrada!": + res.status(404).send({ message: error.message }); + break; + default: + res.status(500).send(error.sqlMessage || error.message); + break; + } + } +}) + +app.post('/user', async (req, res) => { + const { username, nickname, email } = req.body; + + try { + if (!username || !nickname || !email) { + throw new Error("Todos os campos devem ser preenchidos. Verifique os campos username, nickname e email.") + } + + await connection("ToDoListUser") + .insert({ + id: Date.now().toString(), + username: username, + nickname: nickname, + email: email + }); + res.status(201).send("Usuário criado com sucesso!"); + + } catch (error: any) { + switch (error.message) { + case "Todos os campos devem ser preenchidos. Verifique os campos username, nickname e email.": + res.status(422).send({ message: error.message }); + break; + default: + res.status(500).send(error.sqlMessage || error.message); + break; + } + } +}); + +app.post('/task', async (req, res) => { + const { title, description, limitDate, creatorUserId } = req.body + + try { + if (!title || ! description || !limitDate || !creatorUserId) { + throw new Error("Todos os campos devem ser preenchidos. Verifique os campos title, description, limitDate e creatorUserId.") + } + + await connection("ToDoListTask") + .insert({ + id: `${Date.now().toString()}task`, + title: title, + description: description, + limit_date: formataDataDate(limitDate), + creator_user_id: creatorUserId + }); + res.status(201).send("Tarefa criada com sucesso!"); + + } catch (error: any) { + switch (error.message) { + case "Todos os campos devem ser preenchidos. Verifique os campos title, description, limitDate e creatorUserId.": + res.status(422).send({ message: error.message }); + break; + default: + res.status(500).send(error.sqlMessage || error.message); + break; + } + } +}) + +app.put('/user/edit/:id', async (req, res) => { + const id = req.params.id; + const { username, nickname } = req.body; + + try { + if (!id || !username || !nickname) { + throw new Error("Todos os campos devem ser preenchidos. Verifique os campos id, username e nickname."); + } + const findUser = await connection + .from("ToDoListUser") + .select("id") + .where("id", id); + + if (findUser.length === 0) { + throw new Error("Não existe usuário com o id inserido!"); + } + await connection("ToDoListUser") + .update({ + username: username, + nickname: nickname + }) + .where("id", id); + + res.status(200).send("Usuário editado com sucesso!"); + + } catch (error: any) { + switch (error.message) { + case "Não existe usuário com o id inserido!": + res.status(404).send({ message: error.message }); + break; + case "Todos os campos devem ser preenchidos. Verifique os campos id, username e nickname.": + res.status(422).send({ message: error.message }); + break; + default: + res.status(500).send(error.sqlMessage || error.message); + break; + } + } +}) + +const server = app.listen(3003, () => { + if (server) { + const address = server.address() as AddressInfo; + console.log(`${cyan}Server is running at http://localhost:${address.port}${white}`); + } else { + console.log("Failure upon starting server"); + } +}); \ No newline at end of file diff --git a/modulo4/projeto-todolist-backend/src/connection.ts b/modulo4/projeto-todolist-backend/src/connection.ts new file mode 100644 index 0000000..0db5bd7 --- /dev/null +++ b/modulo4/projeto-todolist-backend/src/connection.ts @@ -0,0 +1,15 @@ +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 + } +}); \ No newline at end of file diff --git a/modulo4/projeto-todolist-backend/tsconfig.json b/modulo4/projeto-todolist-backend/tsconfig.json new file mode 100644 index 0000000..467c88a --- /dev/null +++ b/modulo4/projeto-todolist-backend/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