diff --git a/README.md b/README.md index 07fd982..c4aa258 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ -

Material referente as entregas dos desafios no curso de Back-End na Coderhouse Brasil

- +

Descrição:


- -

Aulas com entrega:

- - -Acesse as aulas pelas branches \ No newline at end of file +
+

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.

+
\ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..7aeafda --- /dev/null +++ b/package.json @@ -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" +} diff --git a/src/assets/productManager.js b/src/assets/productManager.js new file mode 100644 index 0000000..d27dbe4 --- /dev/null +++ b/src/assets/productManager.js @@ -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()); +})(); + + diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..03576ed --- /dev/null +++ b/src/index.js @@ -0,0 +1 @@ +// 404 \ No newline at end of file