generated from Greenstand/treetracker-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
244 additions
and
240 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const BaseRepository = require('./BaseRepository'); | ||
|
||
class CaptureRepository extends BaseRepository { | ||
constructor(session) { | ||
super('capture', session); | ||
this._tableName = 'capture'; | ||
this._session = session; | ||
} | ||
|
||
async getByFilter(filterCriteria, options) { | ||
console.log('PG REPOSITORY DB getByFilter', filterCriteria, options); | ||
const query = Object.keys(filterCriteria).length | ||
? filterCriteria | ||
: `id` > 10; | ||
return await this._session | ||
.getDB() | ||
.where(filterCriteria) | ||
.select( | ||
'id', | ||
'reference_id', | ||
'image_url', | ||
'lat', | ||
'lon', | ||
'gps_accuracy', | ||
'planter_id', | ||
'planter_photo_url', | ||
'planter_username', | ||
'device_identifier', | ||
'note', | ||
'morphology', | ||
'age', | ||
'attributes', | ||
'status', | ||
'created_at', | ||
'updated_at', | ||
) | ||
.from('treetracker.capture') | ||
.orderBy('created_at', 'desc') | ||
.limit(options.limit) | ||
.offset(options.offset); | ||
} | ||
|
||
async add(capture) { | ||
return await super.create(capture); | ||
} | ||
} | ||
|
||
module.exports = CaptureRepository; |
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,15 @@ | ||
const BaseRepository = require('./BaseRepository'); | ||
|
||
class EventRepository extends BaseRepository { | ||
constructor(session) { | ||
super('domain_event', session); | ||
this._tableName = 'domain_event'; | ||
this._session = session; | ||
} | ||
|
||
async add(domainEvent) { | ||
return await super.create(domainEvent); | ||
} | ||
} | ||
|
||
module.exports = EventRepository; |
This file was deleted.
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
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,37 @@ | ||
const BaseRepository = require('./BaseRepository'); | ||
|
||
class TreeRepository extends BaseRepository { | ||
constructor(session) { | ||
super('tree', session); | ||
this._tableName = 'tree'; | ||
this._session = session; | ||
} | ||
|
||
async add(tree) { | ||
const wellKnownText = `POINT(${tree.lon} ${tree.lat})`; | ||
const result = await this._session.getDB().raw( | ||
`insert into treetracker.tree ( | ||
id, lat, lon, location, latest_capture_id, image_url, species_id, age, morphology, | ||
status, created_at, updated_at) | ||
values(?, ?, ?, ST_PointFromText(?, 4326), ?, ?, ?, ?, ?, ?, ?, ?) | ||
returning id`, | ||
[ | ||
tree.id, | ||
tree.lat, | ||
tree.lon, | ||
wellKnownText, | ||
tree.latest_capture_id, | ||
tree.image_url, | ||
tree.species_id, | ||
tree.age, | ||
tree.morphology, | ||
tree.status, | ||
tree.created_at, | ||
tree.updated_at, | ||
], | ||
); | ||
return result.rows[0]; | ||
} | ||
} | ||
|
||
module.exports = TreeRepository; |
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,50 +1,58 @@ | ||
const sinon = require('sinon') | ||
const chai = require("chai"); | ||
const sinon = require('sinon'); | ||
const chai = require('chai'); | ||
const assertArrays = require('chai-arrays'); | ||
const sinonChai = require("sinon-chai"); | ||
const sinonChai = require('sinon-chai'); | ||
|
||
chai.use(sinonChai); | ||
chai.use(assertArrays); | ||
const {expect} = chai; | ||
const { expect } = chai; | ||
|
||
const { treeFromRequest, createTree } = require('./tree.js'); | ||
const { TreeRepository } = require('../infra/database/PgRepositories.js'); | ||
|
||
describe("executing treeFromRequest function", () => { | ||
const tree = treeFromRequest({ | ||
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78', | ||
image_url: 'http://test', | ||
lat: 10.15, | ||
lon: 15.03 | ||
}); | ||
|
||
it("should return an immutable tree object", () => { | ||
tree.arandomNewAttribute = 1; | ||
expect(tree.arandomNewAttribute).to.equal(undefined); | ||
}); | ||
|
||
it("should return an object with the required parameters", function() { | ||
expect(Object.keys(tree)).to.equalTo( | ||
['id', 'latest_capture_id', 'image_url', 'lat', 'lon', 'species_id', 'morphology', 'age', 'status'] | ||
); | ||
}); | ||
const TreeRepository = require('../infra/database/TreeRepository'); | ||
|
||
describe('executing treeFromRequest function', () => { | ||
const tree = treeFromRequest({ | ||
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78', | ||
image_url: 'http://test', | ||
lat: 10.15, | ||
lon: 15.03, | ||
}); | ||
|
||
it('should return an immutable tree object', () => { | ||
tree.arandomNewAttribute = 1; | ||
expect(tree.arandomNewAttribute).to.equal(undefined); | ||
}); | ||
|
||
it('should return an object with the required parameters', function () { | ||
expect(Object.keys(tree)).to.equalTo([ | ||
'id', | ||
'latest_capture_id', | ||
'image_url', | ||
'lat', | ||
'lon', | ||
'species_id', | ||
'morphology', | ||
'age', | ||
'status', | ||
]); | ||
}); | ||
}); | ||
|
||
describe("executing createTree function", () => { | ||
const tree = treeFromRequest({ | ||
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78', | ||
image_url: 'http://test', | ||
lat: 10.15, | ||
lon: 15.03 | ||
}); | ||
|
||
const repository = new TreeRepository(); | ||
const stub = sinon.stub(repository, "add"); | ||
const executeCreateTree = createTree(repository); | ||
|
||
it("should add tree to the repository", async () => { | ||
await executeCreateTree(tree); | ||
expect(stub).calledWith(tree); | ||
stub.restore(); | ||
}) | ||
}) | ||
describe('executing createTree function', () => { | ||
const tree = treeFromRequest({ | ||
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78', | ||
image_url: 'http://test', | ||
lat: 10.15, | ||
lon: 15.03, | ||
}); | ||
|
||
const repository = new TreeRepository(); | ||
const stub = sinon.stub(repository, 'add'); | ||
const executeCreateTree = createTree(repository); | ||
|
||
it('should add tree to the repository', async () => { | ||
await executeCreateTree(tree); | ||
expect(stub).calledWith(tree); | ||
stub.restore(); | ||
}); | ||
}); |
Oops, something went wrong.