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
7 changes: 7 additions & 0 deletions semana19-Back/template-intro-autenticacao/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
build

.env

.vscode
requests.rest
2,187 changes: 2,187 additions & 0 deletions semana19-Back/template-intro-autenticacao/package-lock.json

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions semana19-Back/template-intro-autenticacao/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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",
"migrations": "ts-node-dev ./src/data/migrations.ts"
},
"author": "Labenu",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"knex": "^0.21.5",
"mysql": "^2.18.1"
},
"devDependencies": {
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
13 changes: 13 additions & 0 deletions semana19-Back/template-intro-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
16 changes: 16 additions & 0 deletions semana19-Back/template-intro-autenticacao/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
}
})
34 changes: 34 additions & 0 deletions semana19-Back/template-intro-autenticacao/src/data/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { connection } from "./connection";

connection.raw(`
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
);

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)
);
`).then(() => {
console.log("Tabelas criadas!");
}).catch(error => {
console.log(error.sqlMessage || error.message);
}).finally(()=>{
connection.destroy()
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Request, Response } from "express";
import {connection} from "../data/connection";
import { user } from "../types";

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

const { name, nickname, email } = req.body

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

const [user] = await connection('to_do_list_users')
.where({ email })

if (user) {
res.statusCode = 409
throw new Error('Email já cadastrado')
}

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

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

await connection('to_do_list_users')
.insert(newUser)

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

} catch (error) {

if (res.statusCode === 200) {
console.log(error)
res.status(500).send({ message: "Internal server error" })
} else {
res.send({ message: error.sqlMessage || error.message })
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Request, Response } from "express";
import {connection} from "../data/connection";

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()
}

await connection('to_do_list_users')
.update({ name, nickname })
.where({ id: req.params.id })

res.end()

} catch (error) {

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

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


app.post('/users/signup', createUser)
app.put('/users/:id', editUser)

6 changes: 6 additions & 0 deletions semana19-Back/template-intro-autenticacao/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface user {
id: string
email: string
name: string
nickname: string
}
13 changes: 13 additions & 0 deletions semana19-Back/template-intro-autenticacao/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./src",
"removeComments": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}