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
2,427 changes: 2,427 additions & 0 deletions semana17/aula1/package-lock.json

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions semana17/aula1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "aula-back-arquitetura",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node-dev ./src/index.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.3",
"@types/knex": "^0.16.1",
"@types/node": "^13.7.7",
"@types/uuid": "^7.0.0",
"express": "^4.17.1",
"knex": "^0.20.11",
"mysql": "^2.18.1",
"ts-node": "^8.6.2",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.8.3",
"uuid": "^7.0.2"
}
}
40 changes: 40 additions & 0 deletions semana17/aula1/src/business/entities/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export class User {
constructor(
private id: string,
private name: string,
private email: string,
private birthDate: Date
) {}

public getEmail(): string {
return this.email;
}

public setEmail(email: string): void {
this.email = email;
}

public getName(): string {
return this.name;
}

public setName(name: string): void {
this.name = name;
}

public getId(): string {
return this.id;
}

public setId(id: string): void {
this.id = id;
}

public getBirthDate(): Date {
return this.birthDate;
}

public setBirthDate(birthDate: Date) {
this.birthDate = birthDate;
}
}
27 changes: 27 additions & 0 deletions semana17/aula1/src/business/usecase/getAllUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { UserDB } from "../../data/userDataBase";

export class GetAllUsersUC {
constructor(private userDB: UserDB) {}
public async execute(): Promise<GetAllUsersOutput> {
const users = await this.userDB.getAllUsers();
return {
users: users.map(user => ({
id: user.getId(),
name: user.getName(),
email: user.getEmail(),
birthDate: user.getBirthDate()
}))
};
}
}

export interface GetAllUsersOutput {
users: GetAllUsersOutputUser[];
}

export interface GetAllUsersOutputUser {
id: string;
name: string;
email: string;
birthDate: Date;
}
38 changes: 38 additions & 0 deletions semana17/aula1/src/business/usecase/getUserByEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { User } from "../entities/user";
import { UserDB } from "../../data/userDataBase";

export class GetUserByEmailUC {
constructor(private userDB: UserDB) {}

public async execute(
input: GetUserByEmailUCInput
): Promise<GetUserByEmailUCOutput> {
if (!(input.email && input.email.indexOf("@") !== -1)) {
throw new Error("Invalid email");
}

const user = await this.userDB.getUserByEmail(input.email);

if (!user) {
throw new Error("User was not found");
}

return {
id: user.getId(),
name: user.getName(),
email: user.getEmail(),
birthDate: user.getBirthDate()
};
}
}

export interface GetUserByEmailUCInput {
email: string;
}

export interface GetUserByEmailUCOutput {
id: string;
email: string;
name: string;
birthDate: Date;
}
45 changes: 45 additions & 0 deletions semana17/aula1/src/business/usecase/signup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { v4 } from "uuid";
import { UserDB } from "../../data/userDataBase";
import { User } from "../entities/user";

export class SignupUC {
constructor(private userDB: UserDB) {}

public async execute(input: SignupUCInput): Promise<SignupUCOutput> {
const id = v4();

// if (input.email.indexOf("@") === -1) {
// throw new Error("Invalid email");
// }

const birthDate = new Date(input.birthDate + " 00:00");

if (Object.is(birthDate.getFullYear(), NaN)) {
throw new Error("Invalid date");
}

await this.userDB.createUser(
new User(id, input.name, input.email, birthDate)
);

return {
id,
name: input.name,
email: input.email,
birthDate
};
}
}

export interface SignupUCInput {
name: string;
email: string;
birthDate: string;
}

export interface SignupUCOutput {
id: string;
name: string;
email: string;
birthDate: Date;
}
85 changes: 85 additions & 0 deletions semana17/aula1/src/data/userDataBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import knex from "knex";
import { User } from "../business/entities/user";

export class UserDB {
private connection = knex({
client: "mysql",
connection: {
host: "ec2-18-229-236-15.sa-east-1.compute.amazonaws.com",
port: 3306,
user: "eloisa",
password: "9B14WnvOEfRE8kyM8Fuf",
database: "bouman-eloisa"
}
});

private userTableName = "usersSoftware";

private mapDateToDbDate(input: Date): string {
const year = input.getFullYear();
const month = input.getMonth() + 1;
const date = input.getDate();
return `${year + "-" + month + "-" + date}`;
}

private mapDbDateToDate(input: string): Date {
return new Date(input);
}

private mapDbUserToUser(input?: any): User | undefined {
return (
input &&
new User(
input.id,
input.name,
input.email,
this.mapDbDateToDate(input.birthDate)
)
);
}

public async createUser(user: User): Promise<void> {
await this.connection.raw(`
INSERT INTO ${this.userTableName} (id, name, email, birthDate)
VALUES(
'${user.getId()}',
'${user.getName()}',
'${user.getEmail()}',
STR_TO_DATE('${this.mapDateToDbDate(user.getBirthDate())}', '%Y-%m-%d')
);
`);
}

public async getAllUsers(): Promise<User[]> {
const result = await this.connection.raw(
`SELECT * FROM ${this.userTableName}`
);

return result[0].map((res: any) => this.mapDbUserToUser(res)!);
}

public async getUserByEmail(email: string): Promise<User | undefined> {
const result = await this.connection.raw(
`SELECT * FROM ${this.userTableName} WHERE email='${email}'`
);

const user =
result[0][0] &&
this.mapDbUserToUser(result[0][0]);

return user;
}

public async updateUser(user: User): Promise<void> {
await this.connection.raw(`
UPDATE ${this.userTableName}
SET
name='${user.getName()}',
email='${user.getEmail()}',
birthDate=STR_TO_DATE('${this.mapDateToDbDate(
user.getBirthDate()
)}', '%Y-%m-%d')
WHERE id='${user.getId()}';
`);
}
}
12 changes: 12 additions & 0 deletions semana17/aula1/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AddressInfo } from "net";
import app from "./presentation/index";

// Trecho do código responsável por inicializar todas as APIs
const server = app.listen(process.env.PORT || 3000, () => {
if (server) {
const address = server.address() as AddressInfo;
console.log(`Server is running in http://localhost:${address.port}`);
} else {
console.error(`Failure upon starting server.`);
}
});
17 changes: 17 additions & 0 deletions semana17/aula1/src/presentation/endpoints/editUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Request, Response } from "express";
import { GetUserByEmailUC } from "../../business/usecase/getUserByEmail";

export const getUserByEmail = async (req: Request, res: Response) => {
try {
// CHAMAR USE CASE
res.status(200).send({
msg: "success"
});
} catch (err) {
console.log(err);
res.status(400).send({
message: err.message,
...err
});
}
};
18 changes: 18 additions & 0 deletions semana17/aula1/src/presentation/endpoints/getAllUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Request, Response } from "express";
import { GetAllUsersUC } from "../../business/usecase/getAllUsers";
import { UserDB } from "../../data/userDataBase";


export const getAllUsers = async (req: Request, res: Response) => {
try {
const getAllUsersUC = new GetAllUsersUC(new UserDB())
const result = await getAllUsersUC.execute()
res.send(200).send(result)
} catch(err) {
console.log(err);
res.status(400).send({
message: err.message,
...err
})
}
}
19 changes: 19 additions & 0 deletions semana17/aula1/src/presentation/endpoints/getUserByEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request, Response } from "express";
import { GetUserByEmailUC } from "../../business/usecase/getUserByEmail";
import { UserDB } from "../../data/userDataBase";

export const getUserByEmail = async (req: Request, res: Response) => {
try {
const signupUC = new GetUserByEmailUC(new UserDB);
const result = await signupUC.execute({
email: req.query.email
});
res.status(200).send(result);
} catch (err) {
console.log(err);
res.status(400).send({
message: err.message,
...err
});
}
};
22 changes: 22 additions & 0 deletions semana17/aula1/src/presentation/endpoints/signup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Request, Response } from "express";
import { SignupUC } from "../../business/usecase/signup";
import { UserDB } from "../../data/userDataBase";
import { User } from "../../business/entities/user";

export const signupEndpoint = async (req: Request, res: Response) => {
try {
const signupUC = new SignupUC(new UserDB());
const result = await signupUC.execute({
name: req.body.name,
email: req.body.email,
birthDate: req.body.birthDate
});
res.status(200).send(result);
} catch (err) {
console.log(err);
res.status(400).send({
message: err.message,
...err
});
}
};
15 changes: 15 additions & 0 deletions semana17/aula1/src/presentation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express, { Request, Response } from "express";
import { signupEndpoint } from "./endpoints/signup";
import { getUserByEmail } from "./endpoints/getUserByEmail";
import { getAllUsers } from "./endpoints/getAllUsers";

const app = express();
app.use(express.json());

app.post("/signup", signupEndpoint);

app.get("/user", getUserByEmail);

app.get("/user/all", getAllUsers);

export default app;
Loading