-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from nathalia45/GetItem
Get item and Delete Item- Implementation Back End
- Loading branch information
Showing
11 changed files
with
300 additions
and
7,230 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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' } }, | ||
] | ||
} | ||
} |
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
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) | ||
}) | ||
|
||
}) |
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
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') | ||
}) | ||
}) |
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
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) | ||
}) | ||
|
||
}), | ||
|
||
}) |
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
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) | ||
}) | ||
|
||
}) |
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
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
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
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 |
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
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) | ||
} |
Oops, something went wrong.