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
4 changes: 4 additions & 0 deletions modulo5/arquitetura-de-software-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
.env
build/
.vscode
Empty file.
5,681 changes: 5,681 additions & 0 deletions modulo5/arquitetura-de-software-1/package-lock.json

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions modulo5/arquitetura-de-software-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{

"name": "to-do-list",

"version": "1.0.0",

"main": "index.js",

"scripts": {

"start": "tsc && node --inspect ./build/index.js",

"dev-start": "ts-node-dev ./src/index.ts",

"test": "ts-node-dev ./src/services/authenticator.ts"

},

"author": "Labenu",

"license": "ISC",

"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"knex": "^0.21.5",
"mysql": "^2.18.1",
"uuid": "^8.3.2"

},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/jsonwebtoken": "^8.5.8",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"@types/uuid": "^8.3.4",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"

}

}

30 changes: 30 additions & 0 deletions modulo5/arquitetura-de-software-1/requests.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

POST http://localhost:3003/user/signup
Content-Type: application/json

{ "name": "Larissa" ,
"email": "Larissa@matos.com" ,
"password": "ihatepeter",
"role": "ADMIN"
}

###
GET http://localhost:3003/user?email=Larissa@matos.com
Content-Type: application/json
###
POST http://localhost:3003/user/login
Content-Type: application/json

{
"email": "Larissa@matos.com" ,
"password": "ihatepeter"
}
###
GET http://localhost:3003/all
Content-Type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjA1N2EyMWEzLTliZDEtNDNjNS05YTc0LWY0YmJkY2Q3MTY1NyIsInJvbGUiOiJBRE1JTiIsImlhdCI6MTY0NjcwMzgxNywiZXhwIjoxNjQ2NzA1MjU3fQ.vF_3vgmPWnQIGSP-cg3wHcJ_vhQVX9TAxFg5a59Uomc

###
DELETE http://localhost:3003/?id=6c39d2dd-539d-40d0-b913-f4c1012b61f4
Content-Type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjA1N2EyMWEzLTliZDEtNDNjNS05YTc0LWY0YmJkY2Q3MTY1NyIsInJvbGUiOiJBRE1JTiIsImlhdCI6MTY0NjcwMzgxNywiZXhwIjoxNjQ2NzA1MjU3fQ.vF_3vgmPWnQIGSP-cg3wHcJ_vhQVX9TAxFg5a59Uomc
Binary file not shown.
57 changes: 57 additions & 0 deletions modulo5/arquitetura-de-software-1/src/business/TaskBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { TaskDatabase } from "../data/TaskDatabase"
import { IdGenerator } from "../services/IdGenerator"

const taskDB = new TaskDatabase()
const idGenerator = new IdGenerator()

export class TaskBusiness {

createTask = async (
title: string,
description: string,
deadline: string,
authorId: string,
) => {

if (
!title ||
!description ||
!deadline ||
!authorId
) {
throw new Error('"title", "description", "deadline" e "authorId" são obrigatórios')
}

const id: string = idGenerator.generateId()

await taskDB.insertTask({
id,
title,
description,
deadline,
authorId,
})
}

getTaskById = async (id:string) => {

const result = await taskDB.selectTaskById(id)

if (!result) {
throw new Error("Tarefa não encontrada")
}

const taskWithUserInfo = {
id: result.id,
title: result.title,
description: result.description,
deadline: result.deadline,
status: result.status,
authorId: result.author_id,
authorNickname: result.nickname
}

return taskWithUserInfo
}

}
112 changes: 112 additions & 0 deletions modulo5/arquitetura-de-software-1/src/business/UserBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { UserDatabase } from "../data/UserDatabase"
import { Authenticator } from "../services/Authenticator"
import { HashManager } from "../services/HashManager"
import { IdGenerator } from "../services/IdGenerator"
import { user, USER_ROLES } from "../types/user"

const userDB = new UserDatabase()
const hashManager = new HashManager()
const idGenerator = new IdGenerator()
const getData = new Authenticator().getTokenData

export class UserBusiness {

signup = async (
name: string,
email: string,
password: string,
role: USER_ROLES,
) => {
if (
!name ||
!email ||
!password ||
!role
) {
throw new Error('Preencha os campos "name", "email" e "password"')
}

if (email.indexOf("@") === -1) {
throw new Error("Invalid Email");
}

if (password.length < 6) {
throw new Error("Password must have at least 6 characters");
}

const id: string = idGenerator.generateId()

const cypherPassword = await hashManager.hash(password);

await userDB.insertUser({
id,
name,
email,
password: cypherPassword,
role
})

const authenticator = new Authenticator()

const token: string = authenticator.generateToken({
id,
role: role
})

return token
}

login = async (
email: string,
password: string
) => {

if (!email || !password) {
throw new Error("'email' e 'senha' são obrigatórios")
}

const user: user = await userDB.selectUserByEmail(email)

// const user = await connection("tabela").where({email})

if (!user) {
throw new Error("Usuário não encontrado ou senha incorreta")
}

const passwordIsCorrect: boolean = await hashManager.compare(password, user.password)

if (!passwordIsCorrect) {
throw new Error("Usuário não encontrado ou senha incorreta")
}

const authenticator = new Authenticator()

const token: string = authenticator.generateToken({
id: user.id,
role: user.role
})

return token
}

get = async (
token: string
)=> {

getData(token);
return await userDB.get()
}

delete = async (
input: {id:string, token:string}
)=>{
const verifiedToken = getData(input.token);

if(verifiedToken.role !== "ADMIN"){
throw new Error("Apenas administradores podem deletar usuários!")
}

return await userDB.delete(input.id);
}

}
44 changes: 44 additions & 0 deletions modulo5/arquitetura-de-software-1/src/controller/TaskController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Request, Response } from "express"
import { TaskBusiness } from "../business/TaskBusiness"

const taskBusiness = new TaskBusiness()

export class TaskController {

createTask = async (
req: Request,
res: Response
) => {
try {

const { title, description, deadline, authorId } = req.body

taskBusiness.createTask(title, description, deadline, authorId)

res.status(201).end()

} catch (error: any) {

res.statusMessage = error.message
res.status(500).end()
}
}

getTaskById = async (
req: Request,
res: Response
) => {
try {

const { id } = req.params

const taskWithUserInfo = await taskBusiness.getTaskById(id)


res.status(200).send(taskWithUserInfo)

} catch (error: any) {
res.status(400).send(error.message)
}
}
}
Loading