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
Binary file added .DS_Store
Binary file not shown.
Binary file added modulo07/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions modulo07/cookenu/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
build/
.env
.DS_Store
5,794 changes: 5,794 additions & 0 deletions modulo07/cookenu/package-lock.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions modulo07/cookenu/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Cookenu",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "tsc && node --inspect ./build/index.js",
"dev": "tsnd --transpile-only --ignore-watch node_modules ./src/index.ts",
"test": "ts-node-dev ./src/services/authenticator.ts"
},
"author": "Ana Carolina Kazue Inada",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"knex": "^0.21.5",
"mysql": "^2.18.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/jsonwebtoken": "^8.5.9",
"@types/knex": "^0.16.1",
"@types/node": "^14.11.2",
"@types/uuid": "^8.3.4",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "4.6.4"
}
}
1 change: 1 addition & 0 deletions modulo07/cookenu/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- não entendi a funcionalidade da class IdGenerator
59 changes: 59 additions & 0 deletions modulo07/cookenu/requests.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
### criar usuário
POST http://localhost:3003/user/create
Content-Type: application/json

{
"name": "Ofélia",
"email": "felia@email.com",
"password": "hgdhhlhlkhyy"
}

### Login do usuário
POST http://localhost:3003/user/login
Content-Type: application/json

{
"email": "felia@email.com",
"password": "hgdhhlhlkhyy"
}

### ver o próprio perfil

GET http://localhost:3003/user
Content-Type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjA2NTY3Y2FlLTJjYTYtNGFlMi1hM2RkLWVjMTkxYWIyMzBhYSIsImVtYWlsIjoiZmVsaWFAZW1haWwuY29tIiwiaWF0IjoxNjYxODg2NTY0LCJleHAiOjE2NjE4OTAxNjR9.ZWU6VpYblfABVlcg4hFIZle8TkRwNKHUEBZpuHTMwdo

### criar amizade
POST http://localhost:3003/friendship/06567cae-2ca6-4ae2-a3dd-ec191ab23
Content-Type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjA2NTY3Y2FlLTJjYTYtNGFlMi1hM2RkLWVjMTkxYWIyMzBhYSIsImVtYWlsIjoiZmVsaWFAZW1haWwuY29tIiwiaWF0IjoxNjYxNjk5ODQ4LCJleHAiOjE2NjE3MDM0NDh9.aBfowbsDwR1X92RGzk38jmrHzJkSBsCrk9f_rja7dfU

{
"id_followed_user": "0e8c2a38-467b-44a0-accd-f62643a93711"
}

### Criar receita
POST http://localhost:3003/recipe/create
Content-Type: application/json

{
"title": "Sopa de abóbora",
"description": "Em uma panela grande, doure o alho em um fio de óleo e logo acrescente a cebola picada. Mexa rápido e acrescente a abóbora picada e a batata. Coloque a água e deixe ferver, após fervura colocar o caldo de legumes e mexer. Deixe cozinhar com tampa até a abóbora e a batata estarem cozidas. Desligue o fogo. E Espere amornar um pouco. Depois despeje tudo no liquidificador e bata até virar creme. Despeje na panela novamente e cozinhe uns 3 minutos com a cebolinha, acrescente sal se necessário.Sirva quente, com cubinhos de queijo branco e torradas",
"deadline": "2022-08-09",
"author_id": "0e8c2a38-467b-44a0-accd-f62643a93711"
}

### Feed
GET http://localhost:3003/friendship/06567cae-2ca6-4ae2-a3dd-ec191ab230aa
Content-Type: application/json
###


# PUT http://localhost:3003/user/edit/id
# Content-Type: application/json

# {
# "name": "Harry Osbourne"
# }

###
13 changes: 13 additions & 0 deletions modulo07/cookenu/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
38 changes: 38 additions & 0 deletions modulo07/cookenu/src/business/FriendshipBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FriendshipDatabase } from "../data/friendshipDatabase";
import { CustomError } from "../error/customError";
import { friendshipPost } from "../model/friendship";
import { Authenticator } from "../services/Authenticator";
import { IdGenerator } from "../services/IdGenerator";

const authenticator = new Authenticator();
const idGenerator = new IdGenerator();
const friendshipDatabase = new FriendshipDatabase();

export class FriendshipBusiness {
public insertFriend = async ({
id_followed_user,
token,
}: any): Promise<void> => {
try {
if (!id_followed_user) {
throw new CustomError(400, "Preencha o campo");
}

const { id: id_user_follows } = authenticator.getTokenData(token);
const id = idGenerator.generateID();
const inserirFriend = {
id,
id_user_follows,
id_followed_user,
};
await friendshipDatabase.insertFriend(inserirFriend);
} catch (error: any) {
throw new CustomError(400, error.message);
}
};

public getFriendsPost = async (id: string): Promise<friendshipPost[]> => {
const result = await friendshipDatabase.getFriendsPost(id);
return result;
};
}
29 changes: 29 additions & 0 deletions modulo07/cookenu/src/business/RecipeBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { RecipeDatabase } from "../data/RecipeDatabase";
import { CustomError } from "../error/customError";
import { recipe, recipeInput } from "../model/recipe";
import { IdGenerator } from "../services/IdGenerator";

const recipeDatabase = new RecipeDatabase();

export class RecipeBusiness {
public postRecipe = async (input: recipeInput) => {
try {
const { title, description, deadline, author_id } = input;
if (!title || !description || !deadline || !author_id) {
throw new CustomError(400, "Preencha todos os campos");
}
const id: string = Date.now().toString();
// const generateID = new IdGenerator();
const recipe: recipe = {
id,
title,
description,
deadline,
author_id,
};
await recipeDatabase.postRecipe(recipe);
} catch (error: any) {
throw new CustomError(400, error.message);
}
};
}
104 changes: 104 additions & 0 deletions modulo07/cookenu/src/business/UserBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { UserDatabase } from "../data/UserDatabase";
import {
CustomError,
invalidAuthenticatorData,
InvalidEmail,
InvalidPassword,
invalidToken,
UserNotFound,
} from "../error/customError";
import { Profile, user, UserInputDTO, UserLogin } from "../model/user";
import { Authenticator } from "../services/Authenticator";
import { HashManager } from "../services/HashManager";
import { IdGenerator } from "../services/IdGenerator";

const userDatabase = new UserDatabase();
const authenticator = new Authenticator();
const hashManager = new HashManager();
const idGenerator = new IdGenerator();

export class UserBusiness {
public createUser = async (input: UserInputDTO) => {
try {
const { name, email, password } = input;
if (!name || !email || !password) {
throw new CustomError(
400,
'Preencha os campos "name", "email" e "password"'
);
}
if (password.length < 6) {
throw new InvalidPassword();
}
if (!email.includes("@")) {
throw new InvalidEmail();
}
const id = idGenerator.generateID();

const hashPassword = await hashManager.generateHash(password);
const user: user = {
id,
name,
email,
password: hashPassword,
};
await userDatabase.insertUser(user);
const token = authenticator.generateToken({ id, email });
return token;
} catch (error: any) {
throw new CustomError(400, error.message);
}
};

public login = async (input: UserLogin): Promise<string> => {
try {
const { email, password } = input;

if (!email || !password) {
throw new CustomError(400, 'Preencha os campos "email" e "password"');
}

if (!email.includes("@")) {
throw new InvalidEmail();
}

const user = await userDatabase.findUserByEmail(email);
if (!user) {
throw new UserNotFound();
}

const hashCompare = await hashManager.compareHash(
password,
user.password
);
if (!hashCompare) {
throw new InvalidPassword();
}

const token = authenticator.generateToken({ id: user.id, email });
return token;
} catch (error: any) {
throw new CustomError(400, error.message);
}
};

public getProfile = async (token: string): Promise<Profile> => {
if (!token) {
throw new invalidToken();
}

const authenticatorData = new Authenticator().getTokenData(token);

if (!authenticatorData.id) {
throw new invalidAuthenticatorData();
}

const user = await userDatabase.getProfile(authenticatorData.id);

return user;
};
}
// async findOne(id: string): Promise<post[]> {
// const result = await postDataBase.findOne(id);
// return result;
// }
33 changes: 33 additions & 0 deletions modulo07/cookenu/src/controller/FriendshipController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Request, Response } from "express";
import { FriendshipBusiness } from "../business/FriendshipBusiness";
import { post } from "../model/recipe";

const friendshipBusiness = new FriendshipBusiness();

export class FriendshipController {
public insertFriend = async (req: Request, res: Response) => {
try {
const { id_followed_user } = req.body;
const input = {
id_followed_user,
token: req.headers.authorization as string,
};
await friendshipBusiness.insertFriend(input);
res.status(201).send({ message: "Usuário com um novo amigo" });
} catch (error: any) {
res.status(400).send(error.message);
}
};

public getFriendsPost = async (req: Request, res: Response) => {
try {
const { id } = req.params;

const result = await friendshipBusiness.getFriendsPost(id);

res.status(200).send({ result });
} catch (error: any) {
res.statusCode = 400;
}
};
}
23 changes: 23 additions & 0 deletions modulo07/cookenu/src/controller/RecipeController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Request, Response } from "express";
import { recipeInput } from "../model/recipe";
import { RecipeBusiness } from "../business/RecipeBusiness";

const recipeBusiness = new RecipeBusiness();
export class RicipeController {
public postRecipe = async (req: Request, res: Response) => {
try {
const { title, description, deadline, author_id } = req.body;

const input: recipeInput = {
title,
description,
deadline,
author_id,
};
await recipeBusiness.postRecipe(input);
res.status(201).send({ message: "Receita criada!" });
} catch (error: any) {
res.status(400).send(error.message);
}
};
}
Loading