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-software-2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
build
.env
.vscode
27 changes: 27 additions & 0 deletions modulo5/arquitetura-software-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"scripts": {
"start": "tsc && node --inspect ./build/index.js",
"dev": "tsnd --transpile-only --ignore-watch node_modules ./src/index.ts"
},
"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.1"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/jsonwebtoken": "^8.5.0",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"@types/uuid": "^8.3.0",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
Binary file added modulo5/arquitetura-software-2/src/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { insertTask } from "../../data/task/insertTask";
import { taskData } from "../../model/task";
import { generateId } from "../../services/idGenerator";

export const createTaskBusiness = async (
taskData: taskData
) => {

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

const id: string = generateId()

await insertTask({
id,
...taskData
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { selectTaskById } from "../../data/task/selectTaskById"

export const getTaskByIdBusiness = async (
id: string
) => {
const result = await 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
}
32 changes: 32 additions & 0 deletions modulo5/arquitetura-software-2/src/business/user/loginBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { compare } from "../../services/hashManager"
import { selectUserByEmail } from "../../data/user/selectUserByEmail"
import { generateToken } from "../../services/authenticator"
import { user } from "../../model/user"

export const loginBusiness = async (
email: string,
password: string
) => {
if (!email || !password) {
throw new Error("'email' e 'senha' são obrigatórios")
}

const user: user = await selectUserByEmail(email)

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

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

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

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

return token
}
37 changes: 37 additions & 0 deletions modulo5/arquitetura-software-2/src/business/user/signupBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { hash } from "../../services/hashManager";
import { insertUser } from "../../data/user/insertUser";
import { userData } from "../../model/user";
import { generateToken } from "../../services/authenticator";
import { generateId } from "../../services/idGenerator";

export const signupBusiness = async (
userData: userData
):Promise<string> => {
if (
!userData.name ||
!userData.nickname ||
!userData.email ||
!userData.password ||
!userData.role
) {
throw new Error('Preencha os campos "name","nickname", "email" e "password"')
}

const cypherPassword = await hash(userData.password);

const newUser = {
...userData,
password: cypherPassword,
id: generateId()
}

await insertUser(newUser)

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

return token

}
11 changes: 11 additions & 0 deletions modulo5/arquitetura-software-2/src/controller/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express'
import cors from 'cors'

export const app = express()

app.use(express.json())
app.use(cors())

app.listen(3003, () => {
console.log('Servidor rodando na porta 3003')
})
23 changes: 23 additions & 0 deletions modulo5/arquitetura-software-2/src/controller/task/createTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Request, Response } from "express";
import { createTaskBusiness } from "../../business/task/createTaskBusiness";

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

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

await createTaskBusiness({
title, description, deadline, authorId
})

res.status(201).end()

} catch (error) {

res.statusMessage = error.message
res.status(500).end()
}
}
20 changes: 20 additions & 0 deletions modulo5/arquitetura-software-2/src/controller/task/getTaskById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Request, Response } from "express";
import { getTaskByIdBusiness } from "../../business/task/getTaskByIdBusiness";
import {selectTaskById} from "../../data/task/selectTaskById";

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

const { id } = req.params

const task = getTaskByIdBusiness(id)

res.status(200).send(task)

} catch (error) {
res.status(400).send(error.message)
}
}
21 changes: 21 additions & 0 deletions modulo5/arquitetura-software-2/src/controller/user/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Request, Response } from "express"
import { loginBusiness } from "../../business/user/loginBusiness"

export const login = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { email, password } = req.body

const token: string = await loginBusiness(email, password)

res.send({
message: "Usuário logado!",
token
})

} catch (error) {
res.status(400).send(error.message)
}
}
25 changes: 25 additions & 0 deletions modulo5/arquitetura-software-2/src/controller/user/signup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response } from "express";
import { signupBusiness } from "../../business/user/signupBusiness";

export const signup = async (
req: Request,
res: Response
) => {
try {
const { name, nickname, email, password, role } = req.body

const token: string = await signupBusiness({
name, nickname, email, password, role
})

res
.status(201)
.send({
message: "Usuário criado!",
token
})

} catch (error) {
res.status(400).send(error.message)
}
}
16 changes: 16 additions & 0 deletions modulo5/arquitetura-software-2/src/data/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import knex from 'knex'
import dotenv from 'dotenv'

dotenv.config()

export const connection = knex({
client: 'mysql',
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SCHEMA,
port: 3306,
multipleStatements: true
}
})
15 changes: 15 additions & 0 deletions modulo5/arquitetura-software-2/src/data/task/insertTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { connection } from "../connection";
import { task } from "../../model/task";

export const insertTask = async (
task: task
) => {
await connection('to_do_list_tasks')
.insert({
id: task.id,
title: task.title,
description: task.description,
deadline: task.deadline,
author_id: task.authorId
})
}
14 changes: 14 additions & 0 deletions modulo5/arquitetura-software-2/src/data/task/selectTaskById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { connection } from "../connection";

export const selectTaskById = async (
id: string
): Promise<any> => {
const result = await connection.raw(`
SELECT tasks.*, nickname FROM to_do_list_tasks AS tasks
JOIN to_do_list_users AS users
ON author_id = users.id
WHERE tasks.id = '${id}';
`)

return result[0][0]
}
8 changes: 8 additions & 0 deletions modulo5/arquitetura-software-2/src/data/user/insertUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { connection } from "../connection";
import { user } from "../../model/user";

export const insertUser = async(
user: user
) => {
await connection.insert(user).into('to_do_list_users')
}
24 changes: 24 additions & 0 deletions modulo5/arquitetura-software-2/src/data/user/selectUserByEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { connection } from "../connection"
import { user } from "../../model/user"

export const selectUserByEmail = async (
email: string
): Promise<user> => {
try {
const result = await connection("to_do_list_users")
.select("*")
.where({ email })

return {
id: result[0].id,
name: result[0].name,
nickname: result[0].nickname,
email: result[0].email,
password: result[0].password,
role: result[0].role
}

} catch (error) {
throw new Error(error.slqMessage || error.message)
}
}
12 changes: 12 additions & 0 deletions modulo5/arquitetura-software-2/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { app } from "./controller/app"
import { signup } from './controller/user/signup'
import { login } from './controller/user/login'
import { createTask } from './controller/task/createTask'
import { getTaskById } from './controller/task/getTaskById'

app.post('/user/signup', signup)
app.post('/user/login', login)

app.put('/task', createTask)
app.get('/task/:id', getTaskById)

8 changes: 8 additions & 0 deletions modulo5/arquitetura-software-2/src/model/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type taskData = {
title: string,
description: string,
deadline: string,
authorId: string
}

export type task = taskData & { id: string }
19 changes: 19 additions & 0 deletions modulo5/arquitetura-software-2/src/model/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export enum USER_ROLES {
NORMAL = 'NORMAL',
ADMIN = 'ADMIN'
}

export type authenticationData = {
id: string,
role: USER_ROLES
}

export type userData = {
name: string,
nickname: string,
email: string,
password: string,
role: USER_ROLES
}

export type user = userData & { id: string }
Loading