Skip to content

Commit

Permalink
Merge pull request #61 from nathalia45/GetItem
Browse files Browse the repository at this point in the history
Get item and Delete Item- Implementation Back End
  • Loading branch information
jhomarolo authored Jul 28, 2021
2 parents fbbb70a + ad76ec0 commit d8790cf
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 7,230 deletions.
7,169 changes: 19 additions & 7,150 deletions backend/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion backend/src/domain/usecases/_uclist.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module.exports = (injection) => {
return [
{ usecase: require('./createItem').createItem(injection), tags: { group: 'Items' } },
{ usecase: require('./getItems').getItems(injection), tags: { group: 'Items' } },
{ usecase: require('./deleteItem').deleteItem(injection), tags: { group: 'Items' } },
{ usecase: require('./updateItem').updateItem(injection), tags: { group: 'Items' } },
{ usecase: require('./createList').createList(injection), tags: { group: 'List' } },
{ usecase: require('./getLists').getLists(injection), tags: { group: 'List' } },
{ usecase: require('./updateList').updateList(injection), tags: { group: 'List' } },
{ usecase: require('./deleteList').deleteList(injection), tags: { group: 'List' } },
]
}
}
33 changes: 33 additions & 0 deletions backend/src/domain/usecases/deleteItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { Ok, Err, usecase, step, ifElse } = require('@herbsjs/herbs')

const dependency = {
ItemRepository: require('../../infra/repositories/pg/itemRepository'),
}

module.exports.deleteItem = injection =>
usecase('Delete Item', {
request: { id: Number },

response: Boolean,

setup: ctx => (ctx.di = Object.assign({}, dependency, injection)),

authorize: async user => (user.canDeleteItem ? Ok() : Err()),

'Check if the Item exist': step(async ctx => {
const repo = new ctx.di.ItemRepository(injection)
const ret = await repo.find({ where: { id: [ctx.req.id] } })
const item = ctx.item = ret

if (item && item.length > 0) return Ok()
return Err(`Item ID ${ctx.req.id} does not exist`)
}),

'Delete the Item': step(async ctx => {
const repo = new ctx.di.ItemRepository(injection)
const item = ctx.item
const ret = await repo.delete(item)
return Ok(ctx.ret = ret)
})

})
54 changes: 54 additions & 0 deletions backend/src/domain/usecases/deleteItem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { deleteItem } = require('./deleteItem')
const assert = require('assert')
const { Item } = require('../entities/item')


describe('Delete Items', () => {

it('should delete Item', async() => {
const item = new Item()
item.id = 1
item.description = 'graduate in college'

// Given
const injection = {
ItemRepository: class ItemRepository {
async find(where) { return [item] }
async delete(ids) { return true }
}
}

const user = { canDeleteItem: true }
const req = { id: 1 }

// When
const uc = deleteItem(injection)
await uc.authorize(user)

const ret = await uc.run({ id: req.id })

// Then
assert.ok(ret.isOk)
})

it('should not delete Item if the Item does not exist', async() => {
// Given
const injection = {
ItemRepository: class ItemRepository {
async find(where) { return [] }
async delete(ids) { return true }
}
}
const user = { canDeleteItem: true }
const req = { id: 2 }

// When
const uc = deleteItem(injection)
await uc.authorize(user)
const ret = await uc.run({ id: req.id })

// Then
assert.ok(!ret.isOk)
assert.strictEqual(ret.err, 'Item ID 2 does not exist')
})
})
39 changes: 39 additions & 0 deletions backend/src/domain/usecases/getItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { Ok, Err, usecase, step, ifElse } = require('@herbsjs/herbs')
const { Item } = require('../entities/item')

const dependency = {
ItemRepository: require('../../infra/repositories/pg/itemRepository'),
ListRepository: require('../../infra/repositories/pg/listRepository'),
}

module.exports.getItems = (injection) =>
usecase('Get Items', {
request: { ids: [Number] },

response: [Item],

setup: ctx => (ctx.di = Object.assign({}, dependency, injection)),

authorize: async(user) => (user.canGetItems ? Ok() : Err()),

'Get Item by ID or All': ifElse({
'If one or more IDs were provided': step(async(ctx) => {
return Ok(!!ctx.req.ids && ctx.req.ids.length > 0)
}),

'Then return the itens': step(async(ctx) => {
const repo = new ctx.di.ItemRepository(injection)
const item = await repo.find({ where: { id: ctx.req.ids } })

return Ok(ctx.ret = item)
}),

'Else return all': step(async(ctx) => {
const repo = new ctx.di.ItemRepository(injection)
const items = await repo.findAll()
return Ok(ctx.ret = items)
})

}),

})
67 changes: 67 additions & 0 deletions backend/src/domain/usecases/getItems.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { getItems } = require('./getItems')
const { Ok } = require('@herbsjs/herbs')
const assert = require('assert')


describe('Get Items', () => {

it('return all Items', async() => {
// Given
const injection = {
ItemRepository: class ItemRepository {
async findAll(ids) { return Ok([]) }
}
}
const user = { canGetItems: true }
const req = { ids: [] }

// When
const uc = getItems(injection)
await uc.authorize(user)
const ret = await uc.run({ ids: req.ids })

// Then
assert.ok(ret.isOk)
})

it('should find Item by ID', async() => {
// Given
const injection = {
ItemRepository: class ItemRepository {
async find(where) { return Ok([{ id: 1 }]) }
}
}
const user = { canGetItems: true }
const req = { ids: [1, 2] }

// When
const uc = getItems(injection)
await uc.authorize(user)
const ret = await uc.run({ ids: req.ids })

// Then
assert.ok(ret.isOk)
assert.strictEqual(ret.ok[0].id, 1)
})

it('should not find Item by ID if it does not exist', async() => {
// Given
const injection = {
ItemRepository: class ItemRepository {
async find(where) { return Ok([]) }
}
}
const user = { canGetItems: true }
const req = { ids: [1, 2] }

// When
const uc = getItems(injection)
await uc.authorize(user)
const ret = await uc.run({ ids: req.ids })

// Then
assert.ok(ret.isOk)
assert.strictEqual(ret.ok.length, 0)
})

})
14 changes: 6 additions & 8 deletions backend/src/domain/usecases/getLists.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const assert = require('assert')

describe('Get List', () => {

it.skip('return all Lists', async () => {
it('return all Lists', async() => {
// Given
const injection = {
ListRepository: class ListRepository {
async getAll(ids) { return Ok([]) }
async findAll(ids) { return Ok([]) }
}
}
const user = { canGetLists: true }
Expand All @@ -23,11 +23,11 @@ describe('Get List', () => {
assert.ok(ret.isOk)
})

it('should find List by ID', async () => {
it('should find List by ID', async() => {
// Given
const injection = {
ListRepository: class ListRepository {
async find(where) { return Ok([{id: 1}]) }
async find(where) { return Ok([{ id: 1 }]) }
}
}
const user = { canGetLists: true }
Expand All @@ -43,7 +43,7 @@ describe('Get List', () => {
assert.strictEqual(ret.ok[0].id, 1)
})

it('should not find List by ID if it does not exist', async () => {
it('should not find List by ID if it does not exist', async() => {
// Given
const injection = {
ListRepository: class ListRepository {
Expand All @@ -62,6 +62,4 @@ describe('Get List', () => {
assert.ok(ret.isOk)
assert.strictEqual(ret.ok.length, 0)
})
})


})
5 changes: 3 additions & 2 deletions backend/src/infra/api/graphql/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const usecases = [
require('../../../domain/usecases/createList').createList,
require('../../../domain/usecases/deleteList').deleteList,
require('../../../domain/usecases/updateList').updateList,
require('../../../domain/usecases/updateItem').updateItem
require('../../../domain/usecases/updateItem').updateItem,
require('../../../domain/usecases/deleteItem').deleteItem,
/* Add more use cases here */
]
const mutations = usecases.map(usecase => usecase2mutation(usecase(), defaultResolver(usecase)))

/* Custom Mutations */
mutations.push(require('./custom/createItem'))

module.exports = mutations
module.exports = mutations
5 changes: 3 additions & 2 deletions backend/src/infra/api/graphql/queries.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { usecase2query, defaultResolver } = require('@herbsjs/herbs2gql')

const usecases = [
require('../../../domain/usecases/getLists').getLists
require('../../../domain/usecases/getLists').getLists,
require('../../../domain/usecases/getItems').getItems
/* Add more use cases here */
]

const queries = usecases.map(usecase => usecase2query(usecase(), defaultResolver(usecase)))

/* Custom Queries */
// queries.push(require('./custom/getItem'))
// queries.push(require('./custom/getItems'))

module.exports = queries
36 changes: 19 additions & 17 deletions backend/src/infra/api/rest/routes.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const { generateRoutes } = require('@herbsjs/herbs2rest')
const Config = require('../../config/config')

const controllers = [
{
name: 'lists',
getAll: { usecase: require('../../../domain/usecases/getLists').getLists },
getById: { usecase: require('../../../domain/usecases/getLists').getLists, id: 'ids' },
post: { usecase: require('../../../domain/usecases/createList').createList },
put: { usecase: require('../../../domain/usecases/updateList').updateList },
delete: { usecase: require('../../../domain/usecases/deleteList').deleteList }
},
{
name: 'items',
post: { usecase: require('../../../domain/usecases/createItem').createItem },
put: { usecase: require('../../../domain/usecases/updateItem').updateItem }
}
const controllers = [{
name: 'lists',
getAll: { usecase: require('../../../domain/usecases/getLists').getLists },
getById: { usecase: require('../../../domain/usecases/getLists').getLists, id: 'ids' },
post: { usecase: require('../../../domain/usecases/createList').createList },
put: { usecase: require('../../../domain/usecases/updateList').updateList },
delete: { usecase: require('../../../domain/usecases/deleteList').deleteList }

},
{
name: 'items',
post: { usecase: require('../../../domain/usecases/createItem').createItem },
put: { usecase: require('../../../domain/usecases/updateItem').updateItem },
getAll: { usecase: require('../../../domain/usecases/getItems').getItems },
delete: { usecase: require('../../../domain/usecases/deleteItem').deleteItem }
}
]

module.exports = (routes) => {
const showInfo = Config.environment !== "production"
generateRoutes(controllers, routes, showInfo)
}
const showInfo = Config.environment !== "production"
generateRoutes(controllers, routes, showInfo)
}
Loading

0 comments on commit d8790cf

Please sign in to comment.