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
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# package directories
../node_modules
../jspm_packages
../.idea

../package-lock.json

.env
# Serverless directories
.serverless

../.env

.gitignore

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"dependencies": {
"chai": "^4.3.6",
"lambda-tester": "^4.0.1",
"mocha": "^10.0.0"
"mocha": "^10.0.0",
"serverless-webpack": "^5.8.0",
"webpack-node-externals": "^3.0.0"
}
}
5 changes: 3 additions & 2 deletions product-service/controller/product.controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
const model = require('../model/product.model').products;

const selectAll = function () {
const selectAll = async () => {
console.log(`${model}`)
return model;
}

const selectById = function (product_id) {
const selectById = async (product_id) => {
return model.find(product => product.id === product_id);
}

Expand Down
8 changes: 6 additions & 2 deletions product-service/get-product.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

const getById = require('./controller/product.controller').selectById;

module.exports.getProductsById = async (event) => {
module.exports.getProductById = async (event) => {
let status = 200;
let product = {}
if (event[`pathParameters`].productId === '' || !event[`pathParameters`].productId) {
status = 500;
} else {
const id = event[`pathParameters`].productId;
product = getById(id);
product = await getById(id);
if(product === undefined) {
status = 404
product = {
message: 'Product not found'
}
}
}
console.log(product);
return {
statusCode: status,
headers: {
Expand Down
5 changes: 4 additions & 1 deletion product-service/get-products.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
const selectAll = require('./controller/product.controller').selectAll;

module.exports.getProductsList = async () => {
const products = selectAll();
console.log('getProductsList Started')
const products = await selectAll();
console.log('getProductsList executed')
console.log(JSON.stringify(products));
return {
statusCode: 200,
headers: {
Expand Down
1 change: 1 addition & 0 deletions product-service/path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pathParameters":{"productId":"ABCDE102030"}}
39 changes: 24 additions & 15 deletions product-service/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ provider:
stage: dev
region: us-east-1

plugins:
- serverless-webpack

custom:
webpack:
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
includeModules: false
packager: 'npm'

# you can overwrite defaults here
# stage: dev
# region: us-east-1
Expand All @@ -47,23 +56,23 @@ functions:
- X-Amz-Security-Token
- X-Amz-User-Agent
allowCredentials: false
getProductsById:
getProductById:
handler: get-product.getProductById
events:
- http:
path: /products/{productId}
method: get
cors:
origins: '*'
headers:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
allowCredentials: false
request:
parameters:
paths:
productId: true
cors:
origins: '*'
headers:
- Content-Type
- X-Amz-Date
- Authorization
- X-Api-Key
- X-Amz-Security-Token
- X-Amz-User-Agent
allowCredentials: false
request:
parameters:
paths:
productId: true
4 changes: 2 additions & 2 deletions product-service/test/lambda.product.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const expect = require('chai').expect;
const lambda = require('lambda-tester');

const getProductList = require('../get-products').getProductsList;
const getProductById = require('../get-product').getProductsById;
const getProductById = require('../get-product').getProductById;

describe('product-service', () => {

Expand Down Expand Up @@ -35,7 +35,7 @@ describe('product-service', () => {
.expectResult(response => response);

expect(result.statusCode).equals(404);
expect(result.body).equals(undefined);
expect(JSON.parse(result.body).message).equals('Product not found');
});

it('Should return 200 with productId', async () => {
Expand Down
11 changes: 11 additions & 0 deletions product-service/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
context: __dirname,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
target: 'node',
/*target: 'node',
mode: 'none',*/
externals: [nodeExternals(), 'pg-native'],
};