Skip to content

Commit 09ec311

Browse files
committed
Fixing bugs with Mecayle
1 parent 9b4e7af commit 09ec311

7 files changed

Lines changed: 37 additions & 10 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (process.env.DATABASE_URL && !local){
2323
useSSL = true;
2424
}
2525
// which db connection to use
26-
const connectionString = process.env.DATABASE_URL || 'postgresql://coder:pg123@localhost:5432/my_products';
26+
const connectionString = process.env.DATABASE_URL || 'postgresql://localhost:5432/my_products';
2727

2828
const pool = new Pool({
2929
connectionString,

public/style.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,9 @@ th, td {
105105
}
106106

107107
.errors {
108-
color: red;
108+
color: crimson;
109+
margin-top: 1em;
110+
margin-bottom: 0.5em;
111+
font-weight: bold;
109112
}
113+

routes/categories.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ module.exports = function CategoryRoutes(categoryService) {
2323
}
2424

2525
async function add(req, res, next) {
26+
const {description} = req.body;
2627
try {
27-
let input = req.body;
28-
await categoryService.add(input);
28+
29+
if (!description) {
30+
req.flash('error', 'Category is empty!');
31+
return res.redirect('/categories/add');
32+
}
33+
34+
await categoryService.add(description);
2935
req.flash('info', 'Category added!');
3036
res.redirect('/categories');
3137
}
3238
catch (err) {
39+
40+
if (err.stack.includes("duplicate key")){
41+
req.flash('error', 'Category already exists : ' + description);
42+
return res.redirect('/categories/add');
43+
}
44+
3345
next(err)
3446
}
3547
};
@@ -40,7 +52,7 @@ module.exports = function CategoryRoutes(categoryService) {
4052
let result = await categoryService.get(id); // pool.query('SELECT * FROM categories WHERE id = $1', [id]);
4153
res.render('categories/edit', {
4254
page_title: "Edit Customers - Node.js",
43-
data: result.rows[0]
55+
data: result
4456
});
4557
}
4658
catch (err) {

services/category-service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module.exports = function CategoryService(pool){
33
let categories = await pool.query('SELECT * from categories');
44
return categories.rows;
55
}
6-
async function add(category){
6+
async function add(description){
77
let data = [
8-
category.description
8+
description
99
];
1010
let results = await pool.query(`insert into categories (description)
1111
values ($1)

tables.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ create table products (
1010
category_id int,
1111
foreign key (category_id) references categories(id)
1212
);
13+
14+
alter table categories add constraint uniq_desc_constraint unique(description);
15+

test/category-service-tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const pool = new Pool({
1212
describe('The basic database web app', function(){
1313

1414
beforeEach(async function(){
15+
console.log("*****");
1516
await pool.query("delete from products;");
1617
await pool.query("delete from categories;");
1718
});
@@ -58,5 +59,5 @@ describe('The basic database web app', function(){
5859

5960
after(function(){
6061
pool.end();
61-
})
62+
});
6263
});

views/layouts/main.handlebars

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
<a href="/products">Products</a>
1616
</li>
1717
</ul>
18+
19+
<div class="errors">
20+
{{messages.error}}
21+
</div>
22+
1823
<div class="message">
19-
{{messages.info}}
20-
</div>
24+
{{messages.info}}
25+
</div>
26+
27+
2128
{{{body}}}
2229
</div>
2330

0 commit comments

Comments
 (0)