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
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# ![CF](http://i.imgur.com/7v5ASc8.png) LAB 08

## Data Modeling
### Author: Student/Group Name
### Author: Jagdeep Singh

Provided a partially working API server, lab is to complete the server's functionality by creating the data models and writing a full test suite.

### Links and Resources
* [submission PR](http://xyz.com)
* [travis](http://xyz.com)
* [submission PR](https://github.com/401-advanced-javascript-js/lab-08-data-modeling/pull/1)
* [travis](https://www.travis-ci.com/401-advanced-javascript-js/lab-08-data-modeling)
* [back-end](http://xyz.com) (when applicable)
* [front-end](http://xyz.com) (when applicable)
<!-- * [front-end](http://xyz.com) (when applicable) -->

#### Documentation
* [api docs](http://xyz.com) (API servers)
* [jsdoc](http://xyz.com) (Server assignments)
* [styleguide](http://xyz.com) (React assignments)
<!-- * [styleguide](http://xyz.com) (React assignments) -->

### Modules
#### `modulename.js`
Expand All @@ -40,8 +40,11 @@ Usage Notes or examples

#### Tests
* How do you run tests?
* What assertions were made?
* What assertions need to be / should be made?

`npm test`

<!-- * What assertions were made?
* What assertions need to be / should be made? -->

#### UML
Link to an image of the UML for your application and response to events
74 changes: 74 additions & 0 deletions __tests__/src/models/categories.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

const Categories = require('../../../src/models/categories.js');

const supergoose = require('../../supergoose.js');
beforeAll(supergoose.startDB);
afterAll(supergoose.stopDB);

describe('Categories Model', () => {
it('can post() a new category', () => {
let obj = {name: 'category1'};
let categories = new Categories();

return categories.post(obj)
.then((record) => {
Object.keys(obj).forEach((key) => {
expect(record[key]).toEqual(obj[key]);
});
})
.catch((e) => console.error('ERR', e));
});

it('can get() a category', () => {
const categories = new Categories();
let obj = { name: 'Test Category' };

return categories.post(obj).then((record) => {
return categories.get(record._id)
.then((category) => {
Object.keys(obj).forEach((key) => {
expect(category[0][key]).toEqual(obj[key]);
});
})
.catch((e) => console.error('ERR', e));
});
});

it('can put() a category', () => {
const categories = new Categories();
let obj = { name: 'Test Category' };
let obj2 = { name: 'Updated Category' };

return categories.post(obj).then((record) => {
return categories.put(record._id, obj2)
.then((category) => {
Object.keys(obj2).forEach((key) => {
expect(category[key]).toEqual(obj2[key]);
});
})
.catch((e) => console.error('ERR', e));
});
});

it('can delete() a category', () => {
const categories = new Categories();
let obj = { name: 'Test Category' };

return categories.post(obj).then((record) => {
return categories.delete(record._id)
.then((category) => {
// verify delete returns deleted category
Object.keys(obj).forEach((key) => {
expect(category[0][key]).toEqual(obj[key]);
});

return categories.get().then(allCat => {
expect(allCat.length).toEqual(1);
expect(allCat[0]._id).not.toEqual(record._id);
});
})
.catch((e) => console.error('ERR', e));
});
});
});
75 changes: 75 additions & 0 deletions __tests__/src/models/products.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const Products = require('../../../src/models/products.js');

const supergoose = require('../../supergoose.js');
beforeAll(supergoose.startDB);
afterAll(supergoose.stopDB);

describe('Products Model', () => {
it('can post() a new product', () => {
let obj = {name: 'product1'};
let products = new Products();

return products.post(obj)
.then((record) => {
Object.keys(obj).forEach((key) => {
expect(record[key]).toEqual(obj[key]);
});
})
.catch((e) => console.error('ERR', e));
});

it('can get() a product', () => {
const products = new Products();
let obj = { name: 'Test Product' };

return products.post(obj).then((record) => {
return products.get(record._id)
.then((product) => {
Object.keys(obj).forEach((key) => {
expect(product[0][key]).toEqual(obj[key]);
});
})
.catch((e) => console.error('ERR', e));
});
});

it('can put() a product', () => {
const products = new Products();
let obj = { name: 'Test Product' };
let obj2 = { name: 'Updated Product' };

return products.post(obj).then((record) => {
return products.put(record._id, obj2)
.then((category) => {
Object.keys(obj2).forEach((key) => {
expect(category[0][key]).toEqual(obj2[key]);
});
})
.catch((e) => console.error('ERR', e));
});
});

it('can delete() a product', () => {
const products = new Products();
let obj = { name: 'Test Product' };
// let obj2 = { name: 'Updated Product' };

return products.post(obj).then((record) => {
return products.delete(record._id)
.then((product) => {
// verify delete returns deleted category
Object.keys(record).forEach((key) => {
expect(product[0][key]).toEqual(record[key]);
});

return products.get().then(allProducts => {
expect(allProducts.length).toEqual(1);
expect(allProducts[0]._id).not.toEqual(record._id);
});
})
.catch((e) => console.error('ERR', e));
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "eslint '**/*.js'",
"test": "jest --verbose",
"test": "jest --verbose --coverage",
"test-watch": "jest --watchAll --verbose"
},
"jest": {
Expand Down
10 changes: 10 additions & 0 deletions src/models/categories-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mongoose = require('mongoose');

const categories = mongoose.Schema({
_id: { type:String, required:true },
name: { type:String, required:true },
});

module.exports = mongoose.model('categories', categories);
66 changes: 64 additions & 2 deletions src/models/categories.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,84 @@
'use strict';

// version 4 uses random id
// uuid() will return djflkajfoihoioi23h423r9u
const uuid = require('uuid/v4');

const schema = require('./categories-schema.js');

// Categories data model
class Categories {

constructor() {
}

/**
* Get category of passed id
*
* @param {integer} _id
* @returns {Promise|null} promise that resolves to category of passed in id, null if category with id doesn't exist
*/
get(_id) {
// if no id passed, return all categories
// if (!_id) return Promise.resolve(this.database);
let queryObject = _id ? {_id} : {};
return schema.find(queryObject);
}

post(record) {
/**
* Add category to database
*
* @param {Object} record, contains category data
* @returns {Promise}
*/
post(entry) {
entry._id = uuid(); // id for entry, random hash
let record = new schema(entry);
return record.save();
}

put(_id, record) {
/**
* Update data for category with passed id
*
* @param {integer} _id
* @param {Object} record
* @returns {Promise}
*/
put(_id, entry) {
let record = new schema(entry);
return schema.findByIdAndUpdate(_id, record, {new: true});
}

/**
* Delete category with passed id
* @param {integer} _id
* @returns {Promise}
*/
delete(_id) {
return schema.findByIdAndRemove(_id);
}

// sanitize(entry) {
// let valid = true;
// let record = {};

// Object.keys(schema).forEach(field => {
// // mongo talks about "columns" as fields
// // property === field
// if (schema[field].required) {
// // null and 0 are valid
// if (entry[field] !== undefined) {
// record[field] = entry[field];
// } else {
// valid = false;
// }
// } else {
// record[field] = entry[field];
// }
// });
// return valid ? record : null;
// }

}

module.exports = Categories;
10 changes: 10 additions & 0 deletions src/models/products-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const mongoose = require('mongoose');

const products = mongoose.Schema({
_id: { type:String, required:true },
name: { type:String, required:true },
});

module.exports = mongoose.model('products', products);
33 changes: 24 additions & 9 deletions src/models/products.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
'use strict';


// version 4 uses random id
// uuid() will return djflkajfoihoioi23h423r9u
const uuid = require('uuid/v4');
const schema = require('./products-schema.js');

const schema = {
};
// const schema = {
// _id: {required: true},
// name: {required: true}
// };

class Products {

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

get(id) {
get(_id) {
let queryObject = _id ? {_id} : {};
return schema.find(queryObject);
}

post(entry) {
entry._id = uuid();
let record = new schema(entry);
// TODO: pre-save
return record.save();
}

put(id, entry) {
put(_id, entry) {
let record = new schema(entry);
return schema.findByIdAndUpdate(_id, record, {new: true});
}

delete(id) {
delete(_id) {
return schema.findByIdAndDelete(_id);
}

sanitize(entry) {
}
// sanitize(entry) {
// }

}

module.exports = Products;
module.exports = Products;