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

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions modulo5/labebook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "projeto-cookenu-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ./build/index.js",
"build": "tsc",
"dev": "ts-node-dev ./src/index.ts",
"migrations": "tsc && node ./build/database/migrations/Migrations.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
"@types/knex": "^0.16.1",
"@types/node": "^18.0.6",
"@types/uuid": "^8.3.4",
"@types/bcryptjs": "^2.4.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"knex": "^2.2.0",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
}
}
2 changes: 2 additions & 0 deletions modulo5/labebook/requests.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Endpoint de teste
GET http://localhost:3003/ping
9 changes: 9 additions & 0 deletions modulo5/labebook/src/business/PingBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class PingBusiness {
public ping = async () => {
const response = {
message: "Pong!"
}

return response
}
}
12 changes: 12 additions & 0 deletions modulo5/labebook/src/business/PostBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PostDatabase } from "../database/PostDatabase"
import { Authenticator } from "../services/Authenticator"
import { IdGenerator } from "../services/IdGenerator"

export class PostBusiness {
constructor(
private postDatabase: PostDatabase,
private idGenerator: IdGenerator,
private authenticator: Authenticator
) {}

}
14 changes: 14 additions & 0 deletions modulo5/labebook/src/business/UserBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UserDatabase } from "../database/UserDatabase"
import { Authenticator } from "../services/Authenticator"
import { HashManager } from "../services/HashManager"
import { IdGenerator } from "../services/IdGenerator"

export class UserBusiness {
constructor(
private userDatabase: UserDatabase,
private idGenerator: IdGenerator,
private hashManager: HashManager,
private authenticator: Authenticator
) {}

}
23 changes: 23 additions & 0 deletions modulo5/labebook/src/controller/PingController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Request, Response } from "express"
import { PingBusiness } from "../business/PingBusiness"
import { BaseError } from "../errors/BaseError"

export class PingController {
constructor(
private pingBusiness: PingBusiness
) {}

public ping = async (req: Request, res: Response) => {
try {
const response = await this.pingBusiness.ping()

res.status(200).send(response)
} catch (error) {
console.log(error)
if (error instanceof BaseError) {
return res.status(error.statusCode).send({ message: error.message })
}
res.status(500).send({ message: "Erro inesperado" })
}
}
}
8 changes: 8 additions & 0 deletions modulo5/labebook/src/controller/PostController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PostBusiness } from "../business/PostBusiness";

export class PostController {
constructor(
private postBusiness: PostBusiness
) {}

}
8 changes: 8 additions & 0 deletions modulo5/labebook/src/controller/UserController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { UserBusiness } from "../business/UserBusiness";

export class UserController {
constructor(
private userBusiness: UserBusiness
) {}

}
18 changes: 18 additions & 0 deletions modulo5/labebook/src/database/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import knex from "knex"
import dotenv from "dotenv"

dotenv.config()

export abstract class BaseDatabase {
protected static connection = knex({
client: "mysql",
connection: {
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
multipleStatements: true
},
})
}
7 changes: 7 additions & 0 deletions modulo5/labebook/src/database/PostDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseDatabase } from "./BaseDatabase"

export class PostDatabase extends BaseDatabase {
public static TABLE_POSTS = "Labook_Posts"
public static TABLE_LIKES = "Labook_Likes"

}
6 changes: 6 additions & 0 deletions modulo5/labebook/src/database/UserDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BaseDatabase } from "./BaseDatabase"

export class UserDatabase extends BaseDatabase {
public static TABLE_USERS = "Labook_Users"

}
75 changes: 75 additions & 0 deletions modulo5/labebook/src/database/migrations/Migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { BaseDatabase } from "../BaseDatabase"
import { PostDatabase } from "../PostDatabase"
import { UserDatabase } from "../UserDatabase"
import { likes, posts, users } from "./data"

class Migrations extends BaseDatabase {
execute = async () => {
try {
console.log("Creating tables...")
await this.createTables()
console.log("Tables created successfully.")

console.log("Populating tables...")
await this.insertData()
console.log("Tables populated successfully.")

console.log("Migrations completed.")
} catch (error) {
console.log("FAILED! Error in migrations...")
console.log(error.message)
} finally {
console.log("Ending connection...")
BaseDatabase.connection.destroy()
console.log("Connection closed graciously.")
}
}

createTables = async () => {
await BaseDatabase.connection.raw(`
DROP TABLE IF EXISTS ${PostDatabase.TABLE_LIKES};
DROP TABLE IF EXISTS ${PostDatabase.TABLE_POSTS};
DROP TABLE IF EXISTS ${UserDatabase.TABLE_USERS};

CREATE TABLE IF NOT EXISTS ${UserDatabase.TABLE_USERS}(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM("NORMAL", "ADMIN") DEFAULT "NORMAL" NOT NULL
);

CREATE TABLE IF NOT EXISTS ${PostDatabase.TABLE_POSTS}(
id VARCHAR(255) PRIMARY KEY,
content VARCHAR(255) NOT NULL,
user_id VARCHAR(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES ${UserDatabase.TABLE_USERS}(id)
);

CREATE TABLE IF NOT EXISTS ${PostDatabase.TABLE_LIKES}(
id VARCHAR(255) PRIMARY KEY,
post_id VARCHAR(255) NOT NULL,
user_id VARCHAR(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES ${UserDatabase.TABLE_USERS}(id),
FOREIGN KEY (post_id) REFERENCES ${PostDatabase.TABLE_POSTS}(id)
);
`)
}

insertData = async () => {
await BaseDatabase
.connection(UserDatabase.TABLE_USERS)
.insert(users)

await BaseDatabase
.connection(PostDatabase.TABLE_POSTS)
.insert(posts)

await BaseDatabase
.connection(PostDatabase.TABLE_LIKES)
.insert(likes)
}
}

const migrations = new Migrations()
migrations.execute()
77 changes: 77 additions & 0 deletions modulo5/labebook/src/database/migrations/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { ILikeDB, IPostDB } from "../../models/Post"
import { IUserDB, USER_ROLES } from "../../models/User"

export const users: IUserDB[] = [
{
id: "101",
name: "Astrodev",
email: "astrodev@gmail.com",
password: "$2a$12$RBAWOHpUvGTE.MEeIohAzec9tlVqtNA/x2PMPt/Hrt0vI437cQdJC", // bananinha
role: USER_ROLES.ADMIN
},
{
id: "102",
name: "Fulano",
email: "fulano@gmail.com",
password: "$2a$12$PULtVNlAll87D6E8pR/0HO9vbzVDPaUMA89rc5cNmYoAAepbwmkcO", // qwerty00
role: USER_ROLES.NORMAL
},
{
id: "103",
name: "Ciclana",
email: "ciclana@gmail.com",
password: "$2a$12$LkWMqS3oPhP2iVMcZOVvWer9ahUPulxjB0EA4TWPxWaRuEEfYGu/i", // asdfg123
role: USER_ROLES.NORMAL
}
]

export const posts: IPostDB[] = [
{
id: "201",
content: "Olá, sou novo por aqui!",
user_id: "101"
},
{
id: "202",
content: "Bom dia, família!",
user_id: "102"
},
{
id: "203",
content: "Receba!",
user_id: "103"
}
]

export const likes: ILikeDB[] = [
{
id: "301",
post_id: "201",
user_id: "101"
},
{
id: "302",
post_id: "202",
user_id: "101"
},
{
id: "303",
post_id: "203",
user_id: "101"
},
{
id: "304",
post_id: "201",
user_id: "102"
},
{
id: "305",
post_id: "201",
user_id: "103"
},
{
id: "306",
post_id: "202",
user_id: "103"
}
]
9 changes: 9 additions & 0 deletions modulo5/labebook/src/errors/AuthenticationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseError } from "./BaseError";

export class AuthenticationError extends BaseError {
constructor(
message: string = "Credenciais inválidas"
) {
super(401, message)
}
}
9 changes: 9 additions & 0 deletions modulo5/labebook/src/errors/AuthorizationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseError } from "./BaseError";

export class AuthorizationError extends BaseError {
constructor(
message: string = "Permissão insuficiente"
) {
super(403, message)
}
}
8 changes: 8 additions & 0 deletions modulo5/labebook/src/errors/BaseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class BaseError extends Error {
constructor(
public statusCode: number,
message: string
) {
super(message)
}
}
9 changes: 9 additions & 0 deletions modulo5/labebook/src/errors/ConflictError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseError } from "./BaseError";

export class ConflictError extends BaseError {
constructor(
message: string = "Recurso já existe"
) {
super(409, message)
}
}
9 changes: 9 additions & 0 deletions modulo5/labebook/src/errors/NotFoundError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseError } from "./BaseError";

export class NotFoundError extends BaseError {
constructor(
message: string = "Recurso não encontrado"
) {
super(404, message)
}
}
9 changes: 9 additions & 0 deletions modulo5/labebook/src/errors/ParamsError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseError } from "./BaseError";

export class ParamsError extends BaseError {
constructor(
message: string = "Parâmetros inválidos ou faltando"
) {
super(400, message)
}
}
Loading