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
3 changes: 3 additions & 0 deletions modulo4/projeto-todolist-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
.env
14 changes: 14 additions & 0 deletions modulo4/projeto-todolist-backend/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
176 changes: 176 additions & 0 deletions modulo4/projeto-todolist-backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -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");
}
});
15 changes: 15 additions & 0 deletions modulo4/projeto-todolist-backend/src/connection.ts
Original file line number Diff line number Diff line change
@@ -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
}
});
14 changes: 14 additions & 0 deletions modulo4/projeto-todolist-backend/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
}
}