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
11,764 changes: 11,764 additions & 0 deletions modulo6/rodada-cases-semana3/package-lock.json

Large diffs are not rendered by default.

Binary file added modulo6/rodada-cases-semana4/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions modulo6/rodada-cases-semana4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules/
build/
.vscode/

.env
.rest

package-lock.json
8 changes: 8 additions & 0 deletions modulo6/rodada-cases-semana4/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
roots: ["<rootDir>/tests"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};
39 changes: 39 additions & 0 deletions modulo6/rodada-cases-semana4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "rodada-cases-semana4-amaro",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "clear && echo \"Running tests...\" && jest",
"start": "tsc && node --inspect ./build/src/index.js",
"dev": " ts-node-dev ./src/index.ts",
"migrations": "tsc && node ./build/migrations.js",
"build": "clear && echo \"Transpiling files...\" && tsc && echo \"Done!\" "

},
"author": "",
"license": "ISC",
"dependencies": {
"@types/cors": "^2.8.12",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"knex": "^0.21.1",
"mysql": "^2.18.1",
"uuid": "^8.0.0"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.6",
"@types/jest": "^25.2.3",
"@types/jsonwebtoken": "^8.5.0",
"@types/knex": "^0.16.1",
"@types/uuid": "^7.0.3",
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.9.10"
}
}
25 changes: 25 additions & 0 deletions modulo6/rodada-cases-semana4/src/Controller/createProducts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Request, Response } from "express";
import { connection } from "../Data/BaseDatabase";
import generateId from "../Services/idGeneration";

export default async function createProducts(req: Request, res: Response) {
try {
let message = " Product registered successfully"
const name = req.body.name
const tagsList = req.body.tags
if (!name || !tagsList) {
res.statusCode = 406
message = 'All fields must be filled'
throw new Error(message)
}
const id: string = generateId()
const tags = tagsList.join()
await connection('amaroProducts')
.insert({
id, name, tags
})
res.status(201).send(message)
} catch (error) {
res.status(400).send(error.message || error.sqlMessage);
}
}
48 changes: 48 additions & 0 deletions modulo6/rodada-cases-semana4/src/Controller/getProducts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Request, Response } from "express";
import { connection } from "../Data/BaseDatabase";
import { product, responseDB } from "../Model/products";

export default async function getProducts(req: Request, res: Response): Promise<void> {
try {
let message = 'Success'
const id = req.query.id
const name = req.query.name
const tags = req.query.tags
let page = Number(req.query.page)
if (page < 1 || isNaN(page)) {
page = 1;
}
let size = 5;
let offset = size * (page - 1)
let resultDB: responseDB[] = []
if (!id && !name && !tags) {
resultDB = await connection('amaroProducts')
.select('*')
.limit(size)
.offset(offset)
} else {
resultDB = await connection('amaroProducts')
.select('*')
.where('name', 'like', `%${name}%`)
.orWhere('tags', 'like', `%${tags}%`)
.orWhere('id', `${id}`)
.limit(size)
.offset(offset)
}
if (!resultDB) {
res.statusCode = 404
message = 'Data not found'
throw new Error(message)
}
const result = resultDB.map((item) => {
return ({
id: item.id,
name: item.name,
tags: item.tags.split(',')
})
})
res.status(200).send({ message, page, result })
} catch (error) {
res.status(400).send(error.message || error.sqlMessage);
}
}
17 changes: 17 additions & 0 deletions modulo6/rodada-cases-semana4/src/Data/BaseDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Knex from "knex";
import dotenv from "dotenv";

dotenv.config();


export const connection: Knex = 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
}
})
12 changes: 12 additions & 0 deletions modulo6/rodada-cases-semana4/src/Data/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import express, { Express } from "express";
import cors from "cors";

const app: Express = express()

app.use(express.json())
app.use(cors())
app.listen(process.env.PORT || 3003, () => {
console.log("Server running on port 3003!")
})

export default app
18 changes: 18 additions & 0 deletions modulo6/rodada-cases-semana4/src/Data/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {connection} from "./BaseDatabase"





connection.raw (`

CREATE TABLE IF NOT EXISTS amaroProducts (
id VARCHAR(255) PRIMARY KEY,
name VARCHAR (255) NOT NULL,
tags VARCHAR (255)
);
`)




5 changes: 5 additions & 0 deletions modulo6/rodada-cases-semana4/src/Error/baseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export abstract class BaseError extends Error {
constructor(message: string, public code: number) {
super(message);
}
}
10 changes: 10 additions & 0 deletions modulo6/rodada-cases-semana4/src/Model/products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type product = {
id: string,
name: string,
tags: string[]
}
export type responseDB = {
id: string,
name: string,
tags: string
}
5 changes: 5 additions & 0 deletions modulo6/rodada-cases-semana4/src/Services/idGeneration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { v4 } from "uuid";

export default function generateId(): string {
return v4();
}
129 changes: 129 additions & 0 deletions modulo6/rodada-cases-semana4/src/Services/products.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"products": [
{
"id": 8371,
"name": "VESTIDO TRICOT CHEVRON",
"tags": ["balada", "neutro", "delicado", "festa"]
},
{
"id": 8367,
"name": "VESTIDO MOLETOM COM CAPUZ MESCLA",
"tags": ["casual", "metal", "metal"]
},
{
"id": 8363,
"name": "VESTIDO CURTO MANGA LONGA LUREX",
"tags": ["colorido", "metal", "delicado", "estampas", "passeio"]
},
{
"id": 8360,
"name": "VESTIDO FEMININO CANELADO",
"tags": ["workwear", "viagem", "descolado"]
},
{
"id": 8358,
"name": "VESTIDO REGATA FEMININO COM GOLA",
"tags": ["moderno", "inverno", "liso", "basics"]
},
{
"id": 8314,
"name": "VESTIDO PLISSADO ACINTURADO",
"tags": ["casual", "viagem", "delicado"]
},
{
"id": 8311,
"name": "VESTIDO SLIPDRESS CETIM",
"tags": ["balada", "metal", "boho", "descolado", "passeio"]
},
{
"id": 8310,
"name": "VESTIDO CURTO PONTO ROMA MANGA",
"tags": ["casual", "metal", "delicado", "descolado", "elastano", "estampas"]
},
{
"id": 8309,
"name": "VESTIDO MOLETOM COM CAPUZ",
"tags": ["inverno", "liso", "casual", "descolado"]
},
{
"id": 8301,
"name": "VESTIDO LONGO CREPE MANGA COMPRIDA",
"tags": ["casual", "metal", "delicado", "descolado"]
},
{
"id": 8300,
"name": "VESTIDO MALHA COM FENDA",
"tags": ["balada", "metal", "estampas", "moderno"]
},
{
"id": 8293,
"name": "VESTIDO CURTO VELUDO RECORTE GOLA",
"tags": ["colorido", "viagem", "delicado", "descolado", "inverno"]
},
{
"id": 8291,
"name": "VESTIDO MANGA COMPRIDA COSTAS",
"tags": ["inverno", "estampas", "delicado", "descolado", "casual", "passeio", "basics"]
},
{
"id": 8264,
"name": "VESTIDO CURTO VELUDO CRISTAL",
"tags": ["casual", "viagem", "boho", "neutro", "festa"]
},
{
"id": 8119,
"name": "VESTIDO BABADOS KNIT",
"tags": ["moderno", "metal", "descolado", "elastano", "festa", "colorido"]
},
{
"id": 8110,
"name": "VESTIDO CUT OUT TRICOT",
"tags": ["casual", "colorido", "delicado", "descolado", "viagem", "inverno"]
},
{
"id": 8109,
"name": "VESTIDO BABADOS HORIZONTAIS",
"tags": ["moderno", "boho", "festa", "descolado", "colorido"]
},
{
"id": 8104,
"name": "VESTIDO BABADO TURTLENECK",
"tags": ["casual", "metal", "delicado", "neutro", "basics", "inverno", "viagem"]
},
{
"id": 8091,
"name": "VESTIDO MIDI VELUDO DECOTADO",
"tags": ["couro", "veludo", "passeio", "viagem"]
},
{
"id": 8083,
"name": "VESTIDO LONGO ESTAMPADO",
"tags": ["couro", "estampado", "passeio", "viagem"]
},
{
"id": 8080,
"name": "VESTIDO CURTO RENDA VISCOSE",
"tags": ["neutro", "workwear", "moderno", "descolado", "liso", "elastano"]
},
{
"id": 7613,
"name": "VESTIDO LONGO BABADO",
"tags": ["casual", "liso", "passeio", "colorido", "boho"]
},
{
"id": 7533,
"name": "VESTIDO COTTON DOUBLE",
"tags": ["balada", "liso", "moderno", "descolado"]
},
{
"id": 7518,
"name": "VESTIDO CAMISETA FANCY",
"tags": ["casual", "liso"]
},
{
"id": 7516,
"name": "VESTIDO WRAP FLEUR",
"tags": ["neutro", "liso", "basics", "viagem"]
}
]
}
9 changes: 9 additions & 0 deletions modulo6/rodada-cases-semana4/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import getProducts from "./Controller/getProducts"
import createProducts from "./Controller/createProducts"
import app from "./Data/app"


app.get('/products', getProducts)

app.post('/products', createProducts)

14 changes: 14 additions & 0 deletions modulo6/rodada-cases-semana4/src/request.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
POST http://localhost:3003/products
Content-Type: application/json

{
"name": "VESTIDO TRICOT CHEVRON",
"tags": ["balada", "neutro", "delicado", "festa"]
}

###
GET http://localhost:3003/products


###
GET http://localhost:3003/products?tags=balada
14 changes: 14 additions & 0 deletions modulo6/rodada-cases-semana4/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}