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 semana20/labook/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
.env
node_modules
1,201 changes: 1,201 additions & 0 deletions semana20/labook/package-lock.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions semana20/labook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "labook",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "tsc && node build/tests",
"start": "tsc && node build/index"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.2",
"jsonwebtoken": "^8.5.1",
"knex": "^0.95.15",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.6",
"@types/node": "^17.0.7",
"@types/uuid": "^8.3.3",
"ts-node-dev": "^1.1.8",
"typescript": "^4.5.4"
}
}
14 changes: 14 additions & 0 deletions semana20/labook/src/business/PostBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PostDatabase } from "../data/PostDatabase"
import { post } from "../model/Post"

export class PostBusiness {

getPostById = async (
id: string
) : Promise<post | undefined> => {

const post = await new PostDatabase().getPostById(id)

return post
}
}
14 changes: 14 additions & 0 deletions semana20/labook/src/business/UserBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserDatabase } from "../data/UserDatabase"
import { userCredentials } from "../model/User"

export class UserBusiness {

createUser = async (
id: string
) : Promise <userCredentials | undefined> => {

const user = await new UserDatabase().createUser()

return user
}
}
25 changes: 25 additions & 0 deletions semana20/labook/src/controller/PostController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response } from "express"
import { PostBusiness } from "../business/PostBusiness"

export class PostController {

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

const { id } = req.params

const post = await new PostBusiness().getPostById(id)

res.send(post)

} catch (error) {
console.log(error)
res.status(500).send("Ocorreu um erro, tente novamente.")
}
}

createPost = () => { }
}
25 changes: 25 additions & 0 deletions semana20/labook/src/controller/UserController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response } from "express"
import { UserBusiness } from "../business/UserBusiness"

export class PostController {

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

const { id } = req.params

const user = await new UserBusiness().createUser(id)

res.send(user)

} catch (error) {
console.log(error)
res.status(500).send("Ocorreu um erro, tente novamente.")
}
}

createPost = () => { }
}
11 changes: 11 additions & 0 deletions semana20/labook/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 ativo na porta 3003")
})
19 changes: 19 additions & 0 deletions semana20/labook/src/data/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import knex from 'knex'
import dotenv from 'dotenv'

dotenv.config()

export class BaseDatabase {

protected 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
}
})
}
13 changes: 13 additions & 0 deletions semana20/labook/src/data/PostDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { post } from "../model/Post"
import { BaseDatabase } from "./BaseDatabase"

export class PostDatabase extends BaseDatabase {
getPostById = async (
id: string
) : Promise<post | undefined> => {

const result = await this.connection("labook_posts").where({ id })

return result[0]
}
}
14 changes: 14 additions & 0 deletions semana20/labook/src/data/UserDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserDatabase } from "../data/UserDatabase"
import { user } from "../model/User"

export class UserBusiness {

getPostById = async (
id: string
) : Promise<user | undefined> => {

const user = await new UserDatabase().createUser(id)

return user
}
}
9 changes: 9 additions & 0 deletions semana20/labook/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { app } from "./controller/app"
import { PostController } from "./controller/PostController"
import { UserController } from "./controller/UserController"

const postController = new PostController()
const userController = new UserController()

app.post("/users", userController.createUser) // Criar usuário
app.get("/posts/:id", postController.getPostById) // Busca um post por ID
13 changes: 13 additions & 0 deletions semana20/labook/src/model/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum POST_TYPES {
NORMAL = "NORMAL",
EVENT = "EVENT"
}

export interface post {
userId: string
id: string
photo: string
description: string
creationDate: Date
type: POST_TYPES
}
13 changes: 13 additions & 0 deletions semana20/labook/src/model/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type authenticationData = {
id: string
}

export type userCredentials ={
email: string
password: string
}

export interface user extends authenticationData, userCredentials{
name:string
}

35 changes: 35 additions & 0 deletions semana20/labook/src/services/Authenticator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { config } from "dotenv"
import { JwtPayload, sign, verify } from "jsonwebtoken"
import { authenticationData } from "../model/User"

config()

export class Authenticator {

generateToken = (
payload: authenticationData
) => {
return sign(
payload,
process.env.JWT_KEY as string,
{
expiresIn: "12h"
}
)
}

getTokenData = (token: string): authenticationData | null => {
try {
const tokenData = verify(
token,
process.env.JWT_KEY!
) as JwtPayload

return {
id: tokenData.id
}
} catch (error) {
return null
}
}
}
19 changes: 19 additions & 0 deletions semana20/labook/src/services/HashManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { compareSync, genSaltSync, hashSync } from "bcryptjs"

export class HashManager {

createHash = ( plainText: string) => {
const salt = genSaltSync(12)

const cypherText = hashSync(plainText, salt)

return cypherText
}

compareHash = (
plainText: string,
cypherText: string
) => {
return compareSync(plainText, cypherText)
}
}
5 changes: 5 additions & 0 deletions semana20/labook/src/services/IdGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { v4 } from "uuid"

export class IdGenerator {
generate = () => v4()
}
8 changes: 8 additions & 0 deletions semana20/labook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"rootDir": "./src",
"outDir": "./build"
}
}