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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion services/category-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
88 changes: 53 additions & 35 deletions test/category-service-tests.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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({
Expand All @@ -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);
Expand All @@ -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();
});
});
});
17 changes: 15 additions & 2 deletions views/categories/edit.handlebars
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h1>Edit</h1>


{{#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
<form action="/categories/update/{{id}}" method="POST">

<div class="">
Expand All @@ -11,4 +13,15 @@

<input type="submit">
</form>
{{/each}}
{{/each}} --}}


<form action="/categories/update/{{data.id}}" method="POST">

<div class="">
<label for="">Description</label>
<input type="text" name="description" value="{{data.description}}">
</div>

<input type="submit">
</form>