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
431 changes: 431 additions & 0 deletions modulo5/introducao-autenticacao/Exercicio.md

Large diffs are not rendered by default.

5,622 changes: 5,622 additions & 0 deletions modulo5/introducao-autenticacao/package-lock.json

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions modulo5/introducao-autenticacao/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "to-do-list",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "tsc && node --inspect ./build/index.js",
"dev": "ts-node-dev ./src/index.ts",
"test": "ts-node-dev ./src/services/authenticator.ts"
},
"author": "Labenu",
"license": "ISC",
"dependencies": {
"@types/uuid": "^8.3.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.21.5",
"mysql": "^2.18.1",
"ts-node-dev": "^2.0.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"typescript": "^4.0.3"
}
}
22 changes: 22 additions & 0 deletions modulo5/introducao-autenticacao/requests.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @name signup

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

{
"name": "Norman Osbourne",
"nickname": "Green Goblin",
"email": "osbourne@oscorp.com" ,
"password": "ihatepeter"
}

###

@id = {{signup.response.body.newUser.id}}

PUT http://localhost:3003/user/edit/{{id}}
Content-Type: application/json

{
"name": "Harry Osbourne"
}
13 changes: 13 additions & 0 deletions modulo5/introducao-autenticacao/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import express from 'express'
import cors from 'cors'

const app = express()

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

app.listen(3003, ()=>{
console.log('Servidor rodando na porta 3003')
})

export default app
20 changes: 20 additions & 0 deletions modulo5/introducao-autenticacao/src/data/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Knex from "knex"
import dotenv from "dotenv"
dotenv.config()

export class BaseDatabase {
private static 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
}
})
protected getConnection() {
return BaseDatabase.connection
}
}
21 changes: 21 additions & 0 deletions modulo5/introducao-autenticacao/src/data/UserDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { user } from "../types";
import { BaseDatabase } from "./BaseDatabase";

export class UserDatabase extends BaseDatabase {
public create = async (newUser: user) => {
await this.getConnection()
('to_do_list_users')
.insert(newUser)

}
public edit = async (id: string, columnsUpdate: { name: string, nickname: string }) => {
await this.getConnection()('to_do_list_users')
.update(columnsUpdate)
.where({ id })
}
public getByEmail = async (email: string) => {
const [result] = await this.getConnection()('to_do_list_users')
.where({ email })
return result
}
}
41 changes: 41 additions & 0 deletions modulo5/introducao-autenticacao/src/endpoints/createUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Request, Response } from "express";
import { UserDatabase } from "../data/UserDatabase";
import { user } from "../types";

export default async function createUser(
req: Request,
res: Response
): Promise<void> {
try {

const { name, nickname, email, password } = req.body
const userDB = new UserDatabase()

if (!name || !nickname || !email || !password) {
res.statusCode = 422
throw new Error("Preencha os campos 'name','nickname', 'password' e 'email'")
}

const user = await userDB.getByEmail(email)
console.log(user)
if (user) {
res.statusCode = 409
throw new Error('Email já cadastrado')
}

const id: string = Date.now().toString()

const newUser: user = { id, name, nickname, email, password }

await userDB.create(newUser)

res.status(201).send({ newUser })

} catch (error: any) {
if (res.statusCode === 200) {
res.status(500).send({ message: "Internal server error" })
} else {
res.send({ message: error.message })
}
}
}
30 changes: 30 additions & 0 deletions modulo5/introducao-autenticacao/src/endpoints/editUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Request, Response } from "express";
import { UserDatabase } from "../data/UserDatabase";

export default async function createUser(
req: Request,
res: Response
): Promise<void> {
try {

const { name, nickname } = req.body

if (!name && !nickname) {
res.statusCode = 422
res.statusMessage = "Informe o(s) novo(s) 'name' ou 'nickname'"
throw new Error()
}

new UserDatabase().edit(req.params.id, {name, nickname})

res.end()

} catch (error) {

if (res.statusCode === 200) {
res.status(500).end()
}

res.end()
}
}
6 changes: 6 additions & 0 deletions modulo5/introducao-autenticacao/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import app from "./app"
import editUser from './endpoints/editUser'
import createUser from './endpoints/createUser'

app.post('/user/signup', createUser)
app.put('/user/edit/:id', editUser)
10 changes: 10 additions & 0 deletions modulo5/introducao-autenticacao/src/services/GenerateId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { v4 } from "uuid";

export class GenerateId{

createId(): string {
return v4()
}
}

export default GenerateId
7 changes: 7 additions & 0 deletions modulo5/introducao-autenticacao/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type user = {
id: string
email: string
password: string
name: string
nickname: string
}
25 changes: 25 additions & 0 deletions modulo5/introducao-autenticacao/tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE IF NOT EXISTS to_do_list_users (
id VARCHAR(64) PRIMARY KEY,
name VARCHAR(64) NOT NULL,
nickname VARCHAR(64) NOT NULL,
email VARCHAR(64) NOT NULL,
password VARCHAR(64) NOT NULL
);

CREATE TABLE IF NOT EXISTS to_do_list_tasks (
id VARCHAR(64) PRIMARY KEY,
title VARCHAR(64) NOT NULL,
description VARCHAR(1024) DEFAULT "No description provided",
deadline DATE,
status ENUM("TO_DO", "DOING", "DONE") DEFAULT "TO_DO",
author_id VARCHAR(64),
FOREIGN KEY (author_id) REFERENCES to_do_list_users(id)
);

CREATE TABLE IF NOT EXISTS to_do_list_assignees (
task_id VARCHAR(64),
assignee_id VARCHAR(64),
PRIMARY KEY (task_id, assignee_id),
FOREIGN KEY (task_id) REFERENCES to_do_list_tasks(id),
FOREIGN KEY (assignee_id) REFERENCES to_do_list_users(id)
);
14 changes: 14 additions & 0 deletions modulo5/introducao-autenticacao/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}