From fda8d2b405220f9f4783d11966514744c08eb09a Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele Date: Thu, 25 Aug 2022 14:54:32 +0200 Subject: [PATCH 1/8] remove type module as it is not a dependency --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 9111af1..456930a 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,7 @@ "mysql": "^2.5.3", "pg": "^7.4.3", "pg-promise": "^10.11.1", - "prompt": "^0.2.14", - "type": "module" + "prompt": "^0.2.14" }, "devDependencies": { "eslint": "^8.20.0", From 5dfe7df16bc8556890d004eb825afdd7997487a7 Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele Date: Thu, 25 Aug 2022 14:55:09 +0200 Subject: [PATCH 2/8] fix issue with updating category --- views/categories/edit.handlebars | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/views/categories/edit.handlebars b/views/categories/edit.handlebars index 1c9ba10..cc5b422 100644 --- a/views/categories/edit.handlebars +++ b/views/categories/edit.handlebars @@ -1,7 +1,9 @@

Edit

-{{#each data}} +{{!-- {{#each data}} +I think this was working for pg but now the query is using pg-promise +which is not returning a list with an object, but returning an object
@@ -11,4 +13,15 @@ -{{/each}} \ No newline at end of file +{{/each}} --}} + + +
+ +
+ + +
+ + +
\ No newline at end of file From 438b1f0cb659c9b0b5e15d5ebcf82be3587226d5 Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele Date: Thu, 25 Aug 2022 15:06:01 +0200 Subject: [PATCH 3/8] update readme to trigger workflow --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 68401fa..5d689f4 100644 --- a/README.md +++ b/README.md @@ -191,3 +191,4 @@ The API end point is running at http://localhost:3000/api/products. The `client.js` file in the public folder is using the API. It calls the API and renders the resulting JSON data to the screen using HandlebarsJS. Go to http://localhost:3000/client.html to see the screen using the API in action. It uses [axios](https://github.com/axios/axios) to call the API endpoints using HTTP. + From de043989017dc1c5a4466fd4ad985bc172215234 Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele Date: Thu, 25 Aug 2022 15:49:33 +0200 Subject: [PATCH 4/8] removed console.log --- services/category-service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/category-service.js b/services/category-service.js index b62f9d2..49128a4 100644 --- a/services/category-service.js +++ b/services/category-service.js @@ -15,7 +15,7 @@ module.exports = function CategoryService(db) { async function get(id) { let results = await db.oneOrNone('SELECT * FROM categories WHERE id = $1', [id]) - console.log(results); + // console.log(results); return results; } From d6713e8f826d4efee92a1d7245fb086b755e06b8 Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele Date: Thu, 25 Aug 2022 15:51:19 +0200 Subject: [PATCH 5/8] fixed failing test incorrect data type parameters --- index.js | 2 +- test/category-service-tests.js | 87 +++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index 74ed572..e07af92 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ const ProductService = require('./services/product-service'); const UserService = require('./services/user-service'); const pgp = require('pg-promise')(); -const DATABASE_URL= process.env.DATABASE_URL || "postgresql://localhost:5432/my_products_list"; +const DATABASE_URL= process.env.DATABASE_URL || "postgresql://postgres:nimda@localhost:5432/my_products_list"; const config = { connectionString : DATABASE_URL diff --git a/test/category-service-tests.js b/test/category-service-tests.js index c1462b4..d1dbe25 100644 --- a/test/category-service-tests.js +++ b/test/category-service-tests.js @@ -1,37 +1,37 @@ -const assert = require('assert'); -const { log } = require('console'); -const CategoryService = require('../services/category-service'); -const ProductService = require('../services/product-service'); -const pgp = require('pg-promise')(); +const assert = require("assert"); +const { log } = require("console"); +const CategoryService = require("../services/category-service"); +const ProductService = require("../services/product-service"); +const pgp = require("pg-promise")(); // we are using a special test database for the tests const connectionString = - process.env.DATABASE_URL || 'postgresql://localhost:5432/my_products_tests'; + process.env.DATABASE_URL || "postgresql://localhost:5432/my_products_list"; const db = pgp(connectionString); -describe('The basic database web app', function () { +describe("The basic database web app", function () { beforeEach(async function () { try { // clean the tables before each test run - await db.none('TRUNCATE TABLE products RESTART IDENTITY CASCADE;'); - await db.none('TRUNCATE TABLE categories RESTART IDENTITY CASCADE;'); + await db.none("TRUNCATE TABLE products RESTART IDENTITY CASCADE;"); + await db.none("TRUNCATE TABLE categories RESTART IDENTITY CASCADE;"); } catch (err) { console.log(err); throw err; } }); - it('should able to add a category', async function () { + it("should able to add a category", async function () { try { let categoryService = CategoryService(db); - await categoryService.add({ - description: 'Diary', - }); - await categoryService.add({ - description: 'Bread', - }); + // the categoryService.add(string) function receives a string as a param + // the changes could have been cause from updating the queries from pg to pg-promise + + await categoryService.add("Diary"); + await categoryService.add("Bread"); + let categories = await categoryService.all(); assert.equal(2, categories.length); @@ -40,19 +40,20 @@ describe('The basic database web app', function () { } }); - it('should able to update a category', async function () { + it("should able to update a category", async function () { // assemble let categoryService = CategoryService(db); - let category = await categoryService.add({ - description: "Diary" - }); + + // the categoryService.add(string) function receives a string as a param + // the changes could have been cause from updating the queries from pg to pg-promise + + let category = await categoryService.add("Diary"); // act - category.description = 'Milk products'; + category.description = "Milk products"; await categoryService.update(category); // assert - let updateCategory = await categoryService.get(category.id); assert.deepEqual({ @@ -61,12 +62,14 @@ describe('The basic database web app', function () { }, updateCategory); }); - it('should able to delete a category', async function () { + it("should able to delete a category", async function () { // assemble let categoryService = CategoryService(db); - let category = await categoryService.add({ - description: "Diary" - }); + + // the categoryService.add(string) function receives a string as a param + // the changes could have been cause from updating the queries from pg to pg-promise + + let category = await categoryService.add("Diary"); // act await categoryService.delete(category.id); @@ -76,28 +79,44 @@ describe('The basic database web app', function () { assert.equal(null, updateCategory); }); - it('should able to add a products', async function () { + it("should able to add a products", async function () { try { let productService = ProductService(db); let categoryService = CategoryService(db); - let data1 = await categoryService.add({ - description: 'Diary', + // the categoryService.add(string) function receives a string as a param + // the changes could have been cause from updating the queries from pg to pg-promise + + let data1 = await categoryService.add("Diary"); + + let data2 = await categoryService.add("Bread"); + + //the productService.create(object) function receives an object as a param + // object structure => + // category_id => the id of the category the product belongs to + // description => the description of the product being added + // price => the price of the product beng added + await productService.create({ + category_id: data1.id, + description: "Milk", + price: 20 }); - let data2 = await categoryService.add({ - description: 'Bread', + await productService.create({ + category_id: data2.id, + description: "Banana Loaf", + price: 20 }); - productService.create() + let products = await productService.all(); - assert.equal(2, categories.length); + assert.equal(2, products.length); } catch (err) { console.log(err); } }); after(function () { - db.$pool.end + db.$pool.end(); }); }); \ No newline at end of file From 51417c4b1736d558c7e1c234b4c23f7aaac83c8c Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele Date: Thu, 25 Aug 2022 15:52:18 +0200 Subject: [PATCH 6/8] removed unused import --- test/category-service-tests.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/category-service-tests.js b/test/category-service-tests.js index d1dbe25..b8f3e2b 100644 --- a/test/category-service-tests.js +++ b/test/category-service-tests.js @@ -1,5 +1,4 @@ const assert = require("assert"); -const { log } = require("console"); const CategoryService = require("../services/category-service"); const ProductService = require("../services/product-service"); const pgp = require("pg-promise")(); From 9dda5af08d40486d311401d083dfeb89e4af7d6f Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele <47126706+LukhanyoV@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:17:03 +0200 Subject: [PATCH 7/8] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e07af92..74ed572 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ const ProductService = require('./services/product-service'); const UserService = require('./services/user-service'); const pgp = require('pg-promise')(); -const DATABASE_URL= process.env.DATABASE_URL || "postgresql://postgres:nimda@localhost:5432/my_products_list"; +const DATABASE_URL= process.env.DATABASE_URL || "postgresql://localhost:5432/my_products_list"; const config = { connectionString : DATABASE_URL From 22fe13b56202636ff8b5b6dc519d2e2770fe4dc7 Mon Sep 17 00:00:00 2001 From: Lukhanyo Vakele <47126706+LukhanyoV@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:18:06 +0200 Subject: [PATCH 8/8] Update category-service-tests.js --- test/category-service-tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/category-service-tests.js b/test/category-service-tests.js index b8f3e2b..4f4ff22 100644 --- a/test/category-service-tests.js +++ b/test/category-service-tests.js @@ -5,7 +5,7 @@ const pgp = require("pg-promise")(); // we are using a special test database for the tests const connectionString = - process.env.DATABASE_URL || "postgresql://localhost:5432/my_products_list"; + process.env.DATABASE_URL || "postgresql://localhost:5432/my_products_test"; const db = pgp(connectionString); @@ -118,4 +118,4 @@ describe("The basic database web app", function () { after(function () { db.$pool.end(); }); -}); \ No newline at end of file +});