Created
January 5, 2020 17:53
-
-
Save Lucho2027/de5a382cffed831774a66400504d5f4c to your computer and use it in GitHub Desktop.
Module 21 - Checkpoint 14 - Creating services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shoppingListService={ | |
getAllitems(knex){ | |
return knex.select('*').from('shopping_list') | |
}, | |
insertItem(knex, newItem){ | |
return knex | |
.insert(newItem) | |
.into('shopping_list') | |
.returning('*') | |
.then(rows => { | |
return rows[0] | |
}) | |
}, | |
getById(knex, id) { | |
return knex.from('shopping_list').select('*').where('id', id).first() | |
}, | |
deleteItem(knex, id){ | |
return knex('shopping_list') | |
.where({id}) | |
.delete() | |
}, | |
updateItem(knex, id, newItemFields) { | |
return knex('shopping_list') | |
.where({ id }) | |
.update(newItemFields) | |
}, | |
} | |
module.exports = shoppingListService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shoppingListService = require('../src/shopping-list-service') | |
const knex = require('knex') | |
describe(`Items service object`, function(){ | |
let db | |
let testItems = [ | |
{ | |
id: 1, | |
name: 'Park Chops', | |
price:3.00, | |
date_added: new Date('2029-01-22T16:28:32.615Z'), | |
category: 'Main', | |
}, | |
{ | |
id: 2, | |
name: 'Park Chops2', | |
price:6.00, | |
date_added: new Date('2029-01-22T16:28:32.615Z'), | |
category: 'Snack', | |
}, | |
{ | |
id: 3, | |
name: 'Park Chops2', | |
price:6.00, | |
date_added: new Date('2029-01-22T16:28:32.615Z'), | |
category: 'Snack', | |
}, | |
] | |
before(()=>{ | |
db=knex({ | |
client:'pg', | |
connection: process.env.TEST_DB_URL, | |
}) | |
}) | |
before(()=> db('shopping_list').truncate()) | |
afterEach(()=> db('shopping_list').truncate()) | |
after(()=> db.destroy()) | |
context(`Given 'shopping_list' has data`, () => { | |
beforeEach(() => { | |
return db | |
.into('shopping_list') | |
.insert(testItems) | |
}) | |
it(`getAllItems() resolves all items from 'shopping_list' table`, () => { | |
return shoppingListService | |
.getAllItems(db) | |
.then(actual => { | |
expect(actual).to.eql(testItems) | |
}) | |
}) | |
it(`deleteItem() removes an item by id from 'shopping_list' table`, () => { | |
const itemId = 3 | |
return shoppingListService | |
.deleteItem(db, itemId) | |
.then(() => shoppingListService | |
.getAllItems(db)) | |
.then(allItems => { | |
// copy the test items array without the "deleted" item | |
const expected = testItems.filter(item => item.id !== itemId) | |
expect(allItems).to.eql(expected) | |
}) | |
}) | |
it(`getById() resolves an item by id from 'shopping_list' table`, () => { | |
const thirdId = 3 | |
const thirdTestItem = testItems[thirdId - 1] | |
return shoppingListService | |
.getById(db, thirdId) | |
.then(actual => { | |
expect(actual).to.eql({ | |
id: thirdId, | |
name: thirdTestItem.name, | |
category: thirdTestItem.category, | |
date_added: thirdTestItem.date_added, | |
checked: false, | |
}) | |
}) | |
}) | |
it(`updateItem() updates an item from the 'shopping_list' table`, () => { | |
const idofItemToUpdate = 3 | |
const newItemData = { | |
name: 'updated name', | |
category: 'updated category', | |
date_added: new Date(), | |
} | |
return shoppingListService | |
.updateItem(db, idofItemToUpdate, newItemData) | |
.then(() => shoppingListService | |
.getById(db, idofItemToUpdate)) | |
.then(item => { | |
expect(item).to.eql({ | |
id: idofItemToUpdate, | |
...newItemData, | |
}) | |
}) | |
}) | |
}) | |
context(`Given 'shopping_list' has no data`,()=>{ | |
it(`getAllItems() resolves an empty array`, ()=>{ | |
return shoppingListService | |
.getAllItems(db) | |
.then(actual => { | |
expect(actual).to.eql([]) | |
}) | |
}) | |
it(`insertItem() inserts a new item and resolves the new item with a 'id'`, () =>{ | |
const newItem = { | |
name: 'Test new name', | |
category: 'Test new category', | |
date_added: new Date('2020-01-01T00:00:00.000Z'), | |
} | |
return shoppingListService | |
.insertItem(db, newItem) | |
.then(actual => { | |
expect(actual).to.eql({ | |
id:1, | |
name: newItem.name, | |
category: newItem.category, | |
date_added: newItem.date_added, | |
}) | |
}) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config() | |
const knex = require('knex') | |
const shoppingListService = require('./shopping-list-service') | |
const knexInstance = knex({ | |
client: 'pg', | |
connection: process.env.DB_URL, | |
}) | |
//use all the shoppingListService methods!! | |
shoppingListService.getAllItems(knexInstance) | |
.then(items => console.log(items)) | |
.then(() => | |
shoppingListService.insertItem(knexInstance, { | |
name: 'New Item', | |
price: 'New Price', | |
date_added: new Date(), | |
checked: false, | |
category: 'Main', | |
}) | |
) | |
.then(newItem => { | |
console.log(newItem) | |
return shoppingListService.updateItem( | |
knexInstance, | |
newItem.id, | |
{ name: 'Updated name' } | |
).then(() => shoppingListService.getById(knexInstance, newItem.id)) | |
}) | |
.then(item => { | |
console.log(item) | |
return shoppingListService.deleteItem(knexInstance, item.id) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment