-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mibrito1/users
comecei o business
- Loading branch information
Showing
11 changed files
with
1,409 additions
and
26 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { error } from "console"; | ||
import { UserDataBase } from "../dataBase/UserDataBase"; | ||
import { SignupInputDTO, SignupOutputDTO } from "../dtos/users/signup.dto"; | ||
import { IdGenerator } from "../services/IdGenerator"; | ||
import { HashManager } from "../services/HashManager"; | ||
import { Users, userRole } from "../models/Users"; | ||
import { TokenManager, TokenPayload } from "../services/TokenManager"; | ||
|
||
export class UserBusiness { // aqui vai tratar as regras do negocio, as regras do projeto | ||
constructor(private idGenerator: IdGenerator, | ||
private userDataBase: UserDataBase, | ||
private hashManager: HashManager, | ||
private tokenManager: TokenManager | ||
) { | ||
|
||
} | ||
public signup = async (input: SignupInputDTO): Promise<SignupOutputDTO> => { | ||
const { | ||
name, email, password | ||
} = input | ||
const id = this.idGenerator.generate() | ||
const userExist = await this.userDataBase.getUserByEmail(email) | ||
if ( | ||
userExist | ||
) { | ||
throw new Error( | ||
"usuario ja existe" | ||
) | ||
} | ||
const passwordSec = await this.hashManager.hash(password) | ||
const user = new Users(id, name, email, passwordSec, userRole.Normal, new Date().toISOString()) | ||
await this.userDataBase.signup(user.toUserDb()) | ||
const tokenPayload: TokenPayload = { | ||
id: user.getId(), | ||
name: user.getName(), | ||
role: user.getRole() | ||
} | ||
const token = this.tokenManager.createToken(tokenPayload) | ||
const output: SignupOutputDTO = { | ||
message: "usuario registrado com sucesso", | ||
token: token | ||
|
||
|
||
} | ||
return output | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
import { Request, Response } from "express"; | ||
import { UserBusiness } from "../business/UserBusiness"; | ||
import { SignupSchema } from "../dtos/users/signup.dto"; | ||
import { ZodError } from "zod"; | ||
import { BaseError } from "../erros/BaseError"; | ||
|
||
|
||
export class UserController { | ||
constructor() { } | ||
constructor( | ||
private userBusiness: UserBusiness | ||
) { } | ||
public oiMirian = async (req: Request, res: Response) => { | ||
res.send("Oi, Chegou a Mirian") | ||
} | ||
} | ||
public signup = async (req: Request, res: Response) => { | ||
try { | ||
const input = SignupSchema.parse({ | ||
name: req.body.name, | ||
email: req.body.email, | ||
password: req.body.password | ||
}) | ||
const output = await this.userBusiness.signup(input) | ||
res.status(201).send(output) | ||
|
||
} catch (error) { | ||
console.log(error) | ||
if (error instanceof ZodError) { | ||
res.status(400).send(error.issues) | ||
} else if (error instanceof BaseError) { | ||
res.status(error.statusCode).send(error.message) | ||
} else { | ||
res.status(500).send("Erro inesperado") | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import knex from "knex" | ||
|
||
export abstract class BaseDatabase { | ||
protected static connection = knex({ | ||
client: "sqlite3", | ||
connection: { | ||
filename: "./labook.db" | ||
}, | ||
useNullAsDefault: true, | ||
pool: { | ||
min: 0, | ||
max: 1, | ||
afterCreate: (conn: any, cb: any) => { | ||
conn.run("PRAGMA foreign_keys = ON", cb) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { promises } from "dns"; | ||
import { UserDb } from "../models/Users"; | ||
import { BaseDatabase } from "./BaseDataBase"; | ||
|
||
export class UserDataBase extends BaseDatabase { | ||
public static USERS_TABLE = "users" | ||
public async signup( | ||
user: UserDb | ||
) { | ||
await BaseDatabase.connection(UserDataBase.USERS_TABLE).insert(user) | ||
|
||
} | ||
public async getUserByEmail(email: string): Promise<UserDb> { | ||
const [response]: UserDb[] = await BaseDatabase.connection(UserDataBase.USERS_TABLE).where({ | ||
}) | ||
return response | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,6 @@ CREATE TABLE | |
created_at TEXT NOT NULL | ||
); | ||
|
||
DROP TABLE users; | ||
|
||
SELECT * FROM users; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import z from "zod" | ||
|
||
export interface LoginInputDTO { | ||
email: string, | ||
password: String // aqui tipei a entrada de dados | ||
} | ||
|
||
export interface LoginOutputDTO { | ||
message: string, | ||
token: string // tipando o retorno | ||
} | ||
|
||
export const LoginSchema = z.object({ | ||
email: z.string().email(), | ||
password: z.string().min(6) | ||
}).transform((data) => data as LoginInputDTO) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import z from "zod" | ||
|
||
export interface SignupInputDTO { | ||
email: string, | ||
password: string, | ||
name: string | ||
} | ||
export interface SignupOutputDTO { | ||
message: string, | ||
token: string | ||
} | ||
export const SignupSchema = z.object({ | ||
name: z.string().min(2), | ||
email: z.string().email(), | ||
password: z.string().min(6) | ||
}).transform((data) => data as SignupInputDTO) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export abstract class BaseError extends Error { // criando classe para ser reutilizada ( abstract) e erda as caracteristicas do erro | ||
|
||
constructor( | ||
public statusCode: number, | ||
message: string) { | ||
super(message) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import express, { Request, Response } from "express"; | ||
import { UserController } from "../controller/UserController"; | ||
import { UserBusiness } from "../business/UserBusiness"; | ||
import { IdGenerator } from "../services/IdGenerator"; | ||
import { UserDataBase } from "../dataBase/UserDataBase"; | ||
import { HashManager } from "../services/HashManager"; | ||
import { TokenManager } from "../services/TokenManager"; | ||
|
||
|
||
export const userRouter = express.Router() | ||
const userController = new UserController() //instancia | ||
const userController = new UserController( | ||
new UserBusiness( | ||
new IdGenerator(), | ||
new UserDataBase(), | ||
new HashManager(), | ||
new TokenManager() | ||
) | ||
) //instancia | ||
userRouter.get("/", userController.oiMirian) | ||
|
||
userRouter.post('/signup', userController.signup) |