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. + 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", 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; } diff --git a/test/category-service-tests.js b/test/category-service-tests.js index c1462b4..4f4ff22 100644 --- a/test/category-service-tests.js +++ b/test/category-service-tests.js @@ -1,37 +1,36 @@ -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 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_test"; 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 +39,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 +61,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 +78,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 +}); 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 @@