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
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<h2>Material referente as entregas dos desafios no curso de Back-End na Coderhouse Brasil</h2>

<h1>Descrição:</h1>
<hr>

<h3> Aulas com entrega:</h3>
<ul>
<li>Aula 2 ( Classes com ECMAScript e ECMAScript avançado )</li>

</ul>

<span>Acesse as aulas pelas branches</span>
<div>
<p>12/12/2024 Desafio ( Gestão de Arquivos em Javascript ) da Aula 4 ( Introdução ao Node.js ) do curso de Back-end na CoderHouse; Turma #63515 de 2024/25.</p>
</div>
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "cursocoderhousebackend",
"version": "1.0.0",
"description": "aula 4 de back end",
"main": "productManager.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Gustavo Rezende",
"license": "ISC"
}
75 changes: 75 additions & 0 deletions src/assets/productManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const fs = require('fs');

class ProductManager {
constructor(path) {
this.path = path;
}

async addProduct(product) {
let products = await this.getProducts();
const newProduct = {
id: products.length > 0 ? products[products.length - 1].id + 1 : 1,
...product
};
products.push(newProduct);
await fs.promises.writeFile(this.path, JSON.stringify(products, null, 2));
return newProduct;
}

async getProducts() {
try {
const data = await fs.promises.readFile(this.path, 'utf-8');
return JSON.parse(data);
} catch (error) {
return [];
}
}

async getProductById(id) {
const products = await this.getProducts();
return products.find(product => product.id === id) || null;
}

async updateProduct(id, updatedFields) {
let products = await this.getProducts();
let productIndex = products.findIndex(product => product.id === id);

if (productIndex === -1) {
return null;
}

products[productIndex] = { ...products[productIndex], ...updatedFields, id };
await fs.promises.writeFile(this.path, JSON.stringify(products, null, 2));
return products[productIndex];
}

async deleteProduct(id) {
let products = await this.getProducts();
const filteredProducts = products.filter(product => product.id !== id);

if (products.length === filteredProducts.length) {
return false;
}

await fs.promises.writeFile(this.path, JSON.stringify(filteredProducts, null, 2));
return true;
}
}

//! Execution
(async () => {
const manager = new ProductManager('products.json');

await manager.addProduct({
title: 'Produto Exemplo',
description: 'Descrição do produto',
price: 100,
thumbnail: 'imagem.jpg',
code: 'ABC123',
stock: 10
});

console.log(await manager.getProducts());
})();


1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// 404