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
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/node_modules/*
**/*.min.js
**/vendor/*
**/coverage/*
**/build/*
**/docs/*
31 changes: 31 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"globals" : {
"Handlebars": true,
"$": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"browser": true,
"jquery": true,
"node": true,
"es6": true,
"jest": true
},
"rules": {
"no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }],
"eqeqeq": ["error", "always"],
"no-template-curly-in-string": "error",
"no-console": "off",
"no-undefined": "off",
"indent": ["error", 2],
"quotes": ["warn", "single", {"allowTemplateLiterals": true}],
"no-multi-spaces": ["warn", {"exceptions": { "VariableDeclarator": true }}],
"no-trailing-spaces": "warn",
"new-cap": "off",
"no-redeclare": ["error", { "builtinGlobals": true }],
"eol-last": "error"
}
}
Binary file added 20200202_200657.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# Class-04-Data-Modeling
# Class-04-Data-Modeling
# lab-04-Data modeling
# LAB - Class 04
### Author: Goorob Ahmad Alswalqah
### Links and Resources-
[submission PR](https://github.com/Goorob-401-advanced-javascript/lab-07-api-server/pull/1)

- [ci/cd](https://github.com/Goorob-401-advanced-javascript/lab-07-api-server/actions) (GitHub Actions)

#### Tests-
How do you run tests? `npm run test`
- Any tests of note? no
- Describe any tests that you did not complete, skipped, etc

#### UMLLink to an image of the -UML for your application and response to event


[UML](https://github.com/Goorob-401-advanced-javascript/lab-07-api-server/blob/express-API-server/assests/20200201_073814.jpg)
24 changes: 24 additions & 0 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

module.exports = exports = {};

var fileContents = '';

exports.readFile = (file, cb) => {
if (file.match(/bad/i)) {
cb('Invalid File');
}
else {
cb(undefined, Buffer.from(fileContents));
}
};

exports.writeFile = (file, buffer, cb) => {
if (file.match(/bad/i)) {
cb('Invalid File');
}
else {
fileContents = buffer;
cb(undefined, true);
}
};
61 changes: 61 additions & 0 deletions __tests__/categories.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

'use strict';
const Categories = require('../categories/categories.js');

describe('Categories Model', () => {

let categories;

beforeEach(() => {
categories = new Categories();
});

it('test post() ', () => {
let obj = { name: 'Test Category',};
return categories.create(obj)
.then(record => {
Object.keys(obj).forEach(key => {
expect(record[key]).toEqual(obj[key]);
});
})
.catch(e => console.error('ERR', e));
});

it('test get() ', () => {
let obj = { name: 'Test Category',};
return categories.create(obj)
.then(record => {
return categories.get(record._id)
.then(category => {
Object.keys(obj).forEach(key => {
expect(category[0][key]).toEqual(obj[key]);
});
});
});
});


it('test update ()', () => {
let obj = { name: 'Test category',};
return categories.create(obj)
.then(data => {
let newObj = { name: 'new category', };
return categories.update(data.id, newObj)
.then(data => {
expect(data.name).toEqual('new category');
});
});

});

it('test delete() ', () => {
let obj = { name: 'Test Category', };
return categories.create(obj)
.then(data => {
return categories.delete(data)
.then((deletedItem) => {
expect(deletedItem).toBeUndefined();
});
});
});
});
62 changes: 62 additions & 0 deletions __tests__/products.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

'use strict';
const Products = require('../products/product.js');

describe('Products Model', () => {

let products;


beforeEach(() => {
products = new Products();
});

it('test post() ', () => {
let obj = { name: 'Test product', };
return products.create(obj)
.then(record => {
Object.keys(obj).forEach(key => {
expect(record[key]).toEqual(obj[key]);
});
})
.catch(e => console.error('ERR', e));
});

it('test get() ', () => {
let obj = { name: 'Test products',};
return products.create(obj)
.then(record => {
return products.get(record._id)
.then(record => {
Object.keys(obj).forEach(key => {
expect(record[0][key]).toEqual(obj[key]);
});
});
});
});


it('test update ()', () => {
let obj = { name: 'Test product', };
return products.create(obj)
.then(data => {
let newObj = { name: 'new product',};
return products.update(data.id, newObj)
.then(data => {
expect(data.name).toEqual('new product');
});
});

});

it('test delete() ', () => {
let obj = { name: 'Test product',};
return products.create(obj)
.then(data => {
return products.delete(data)
.then((deletedItem) => {
expect(deletedItem).toEqual(undefined);
});
});
});
});
16 changes: 16 additions & 0 deletions categories/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

'use strict';

const DataModel = require('../memory-data-model.js');

class Categories extends DataModel {
constructor() {
super();
this.schema = {
id: { required: true, },
name: { required: true, },
};
}
}

module.exports = Categories;
Empty file added data/categories.db
Empty file.
33 changes: 33 additions & 0 deletions memory-data-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const uuid = require('uuid/v4');

class Model {

constructor() {
this.database = [];
}

get(id) {
let response = id ? this.database.filter((record) => record.id === id) : this.database;
return Promise.resolve(response);
}

create(record) {
record.id = uuid();
this.database.push(record);
return Promise.resolve(record);
}

update(id, record) {
this.database = this.database.map((item) => (item.id === id) ? record : item);
return Promise.resolve(record);
}

delete(id) {
this.database = this.database.filter((record) => record.id !== id);
return Promise.resolve();
}

}
module.exports = Model;
Loading