Skip to content

Commit 63dfe6d

Browse files
committed
added gitlab CL configs
1 parent 24ac690 commit 63dfe6d

9 files changed

Lines changed: 190 additions & 59 deletions

File tree

.gitlab-ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
image: node:latest
2+
3+
services:
4+
- postgres:latest
5+
6+
variables:
7+
POSTGRES_DB: my_products_tests
8+
POSTGRES_USER: codex-coder
9+
POSTGRES_PASSWORD: "pg123"
10+
11+
cache:
12+
path:
13+
- node_modules/
14+
15+
postgresql:
16+
script:
17+
- npm test

api/categories-api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = function (categoryService) {
2121
}
2222
};
2323

24+
2425
async function add(req, res) {
2526
try {
2627
let input = req.body;

api/user-api.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module.exports = function (userService) {
2+
3+
async function user(req, res) {
4+
try {
5+
let id = req.body.id;
6+
let users = await userService.getUserName();
7+
res.json({
8+
status: 'success',
9+
data: users
10+
});
11+
}
12+
catch (err) {
13+
res.json({
14+
status: "error",
15+
error: err.stack
16+
});
17+
}
18+
};
19+
20+
const validatePassword = async (req, res) => {
21+
try {
22+
let username = req.body.username;
23+
let password = req.body.password;
24+
let userPassword = await userService.getUserPassword(username);
25+
26+
/*
27+
- hash password back to normal
28+
- use compare method to check if password match
29+
30+
--- exceptions/ events ---
31+
- handle wrong password exception
32+
- handle successfull event;
33+
*/
34+
35+
} catch (error) {
36+
res.json({
37+
status: "error",
38+
error: err.stack
39+
})
40+
}
41+
}
42+
43+
const signUp = async (req, res) => {
44+
try {
45+
let username = req.body.username;
46+
let password = req.body.password;
47+
48+
/*
49+
- set-up salt
50+
- hash the password
51+
- call signUp service
52+
53+
--- exception/ event ---
54+
- user already exists
55+
- handle successfull event
56+
- create a user-token
57+
*/
58+
59+
} catch (error) {
60+
res.json({
61+
status: "error",
62+
error: error.stack
63+
})
64+
}
65+
}
66+
67+
return {
68+
user,
69+
validatePassword,
70+
signUp
71+
}
72+
73+
}

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ const Categories = require('./routes/categories');
77
const CategoriesAPI = require('./api/categories-api');
88
const Products = require('./routes/products');
99
const ProductsAPI = require('./api/products-api');
10+
const UserAPI = require('./api/user-api');
11+
1012

1113
const app = express();
1214
const session = require('express-session');
1315
const flash = require('express-flash');
1416
const CategoryService = require('./services/category-service');
1517
const ProductService = require('./services/product-service');
18+
const UserService = require('./services/user-service');
1619

1720
const pgp = require('pg-promise')();
1821

@@ -27,10 +30,14 @@ const connectionString = process.env.DATABASE_URL || 'postgresql://localhost:543
2730

2831
const categoryService = CategoryService(db);
2932
const productService = ProductService(db);
33+
const userService = UserService(db);
34+
3035
const categoryRoutes = Categories(categoryService);
31-
const categoryAPI = CategoriesAPI(categoryService);
3236
const productRoutes = Products(productService, categoryService);
37+
38+
const categoryAPI = CategoriesAPI(categoryService);
3339
const productsAPI = ProductsAPI(productService);
40+
const userAPI = UserAPI(userService);
3441

3542
app.use(session({
3643
secret: 'keyboard cat',
@@ -81,6 +88,10 @@ app.post('/api/products', productsAPI.add);
8188

8289
app.get('/api/categories', categoryAPI.all);
8390

91+
app.get('/api/users', userAPI.user);
92+
// add user/sign-up end-point
93+
94+
8495
app.use(errorHandler);
8596

8697
//configure the port number using and environment number

public/client.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
document.addEventListener('DOMContentLoaded', function() {
1+
document.addEventListener('DOMContentLoaded', function () {
22

33
function compileTemplate(selector) {
44
let template = document.querySelector(selector);
@@ -27,38 +27,38 @@ document.addEventListener('DOMContentLoaded', function() {
2727

2828
function showCategoryDropdown() {
2929
axios
30-
.get('/api/categories')
31-
.then(function(results){
32-
let response = results.data;
33-
let data = response.data;
34-
let html = categoryListTemplateInstance({
35-
categoryList: data
30+
.get('/api/categories')
31+
.then(function (results) {
32+
let response = results.data;
33+
let data = response.data;
34+
let html = categoryListTemplateInstance({
35+
categoryList: data
36+
});
37+
categoriesElem.innerHTML = html;
3638
});
37-
categoriesElem.innerHTML = html;
38-
});
3939
}
4040

4141
function showProducts() {
4242
productService
43-
.getProducts()
44-
.then(function(results){
45-
let response = results.data;
46-
let data = response.data;
47-
let html = productListTemplateInstance({
48-
productList : data
43+
.getProducts()
44+
.then(function (results) {
45+
let response = results.data;
46+
let data = response.data;
47+
let html = productListTemplateInstance({
48+
productList: data
49+
});
50+
let productTableHTML = html;
51+
productsElem.innerHTML = productTableHTML;
4952
});
50-
let productTableHTML = html;
51-
productsElem.innerHTML = productTableHTML;
52-
});
5353
}
54-
54+
5555
function clearFields() {
5656
productNameElem.value = '';
5757
categoryIdElem.value = '';
58-
priceElem.value= '';
58+
priceElem.value = '';
5959
}
6060

61-
productBtn.addEventListener('click', function() {
61+
productBtn.addEventListener('click', function () {
6262

6363
let description = productNameElem.value;
6464
let category_id = categoryIdElem.value;
@@ -82,18 +82,18 @@ document.addEventListener('DOMContentLoaded', function() {
8282
description,
8383
price
8484
})
85-
.then(function() {
86-
showProducts();
87-
clearFields();
88-
})
89-
.catch(function(err){
90-
alert(err);
91-
});
85+
.then(function () {
86+
showProducts();
87+
clearFields();
88+
})
89+
.catch(function (err) {
90+
alert(err);
91+
});
9292
}
9393
else {
94-
errorsElem.innerHTML = errorsTemplateInstance({errors});
94+
errorsElem.innerHTML = errorsTemplateInstance({ errors });
9595
}
96-
96+
9797
});
9898

9999
showCategoryDropdown();
@@ -107,7 +107,7 @@ function editProduct(id) {
107107
}
108108

109109
function ProductService() {
110-
function getProducts(){
110+
function getProducts() {
111111
return axios.get('/api/products')
112112
}
113113

services/product-service.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,38 @@ module.exports = function ProductService(db) {
1616
values ($1, $2, $3)`, data)
1717

1818
}
19-
async function get(id) {
20-
let productResult = await db.one('SELECT * FROM products WHERE id = $1', [id])
21-
return productResult;
22-
23-
}
19+
async function get(id) {
20+
let productResult = await db.one('SELECT * FROM products WHERE id = $1', [id])
21+
return productResult;
22+
}
2423

25-
async function update(product) {
26-
var data = [
27-
product.category_id,
28-
product.description,
29-
product.price,
30-
product.id
31-
];
24+
async function update(product) {
25+
var data = [
26+
product.category_id,
27+
product.description,
28+
product.price,
29+
product.id
30+
];
3231

33-
let updateQuery = `UPDATE products
32+
let updateQuery = `UPDATE products
3433
SET category_id = $1,
3534
description = $2,
3635
price = $3
3736
WHERE id = $4`;
3837

39-
return await db.any(updateQuery, data)
38+
return await db.any(updateQuery, data)
4039

41-
}
40+
}
4241

43-
async function deleteById(id) {
44-
return await db.result('DELETE FROM products WHERE id = $1', [id])
45-
}
42+
async function deleteById(id) {
43+
return await db.result('DELETE FROM products WHERE id = $1', [id])
44+
}
4645

47-
return {
48-
all,
49-
create,
50-
delete: deleteById,
51-
get,
52-
update
53-
}
54-
}
46+
return {
47+
all,
48+
create,
49+
delete: deleteById,
50+
get,
51+
update
52+
}
53+
}

services/user-service.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = function userService(db) {
2+
const getUserName = async () => {
3+
let user = await db.any('select username from users');
4+
const userData = user.length > 0 ? user : null;
5+
return userData;
6+
}
7+
8+
const getUserPassword = async username => {
9+
let userPassword = await db.any('select password from users where username = $1', [username]);
10+
const results = userPassword.length > 0 ? userPassword : null;
11+
return results;
12+
}
13+
14+
const signUp = async (username, password) => {
15+
16+
}
17+
18+
return {
19+
getUserName,
20+
getUserPassword
21+
}
22+
}

tables.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@ create table products (
1111
foreign key (category_id) references categories(id) ON DELETE CASCADE
1212
);
1313

14+
create table users (
15+
id serial not null primary key,
16+
username text not null,
17+
password text not null
18+
-- createdAt timestamp not null,
19+
-- updatedAt timestamp not null
20+
);
21+
1422
alter table categories add constraint uniq_desc_constraint unique(description);
1523

test/category-service-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const CategoryService = require('../services/category-service');
33
const pg = require("pg");
44
const Pool = pg.Pool;
55

6-
const = process.env.DATABASE_URL || 'postgresql://localhost:5432/my_products_tests';
6+
const connectionString = process.env.DATABASE_URL || 'postgresql://localhost:5432/my_products_tests';
77

88
const pool = new Pool({
99
connectionString

0 commit comments

Comments
 (0)