11import { Constructor , inject } from '@loopback/core' ;
22import {
33 DefaultCrudRepository ,
4+ repository ,
5+ HasManyRepositoryFactory ,
46 Filter ,
7+ Options ,
58 Where ,
69 Count ,
710} from '@loopback/repository' ;
@@ -23,16 +26,29 @@ export class PlanterRepository extends UtilsRepositoryMixin<
2326> ( DefaultCrudRepository ) {
2427 constructor (
2528 @inject ( 'datasources.treetracker' ) dataSource : TreetrackerDataSource ,
29+ @repository . getter ( 'PlanterRegistrationRepository' )
30+ protected planterRegistrationRepositoryGetter : Getter < PlanterRegistrationRepository > ,
2631 ) {
2732 super ( Planter , dataSource ) ;
33+ this . planterRegs = this . createHasManyRepositoryFactoryFor (
34+ 'planterRegs' ,
35+ planterRegistrationRepositoryGetter ,
36+ ) ;
37+ this . registerInclusionResolver (
38+ 'planterRegs' ,
39+ this . planterRegs . inclusionResolver ,
40+ ) ;
2841 }
2942
3043 // default .find() wasn't applying the org filters
44+
3145 async findWithOrg (
3246 filter ?: Filter < Planter > ,
47+ deviceIdentifier ?: string ,
48+ options ?: Options ,
3349 ) : Promise < ( Planter & PlanterRelations ) [ ] > {
34- if ( ! filter ) {
35- return await this . find ( filter ) ;
50+ if ( ! filter || deviceIdentifier === null ) {
51+ return await this . find ( filter , options ) ;
3652 }
3753
3854 try {
@@ -42,7 +58,14 @@ export class PlanterRepository extends UtilsRepositoryMixin<
4258 filter ,
4359 ) ;
4460
45- const selectStmt = `SELECT ${ columnNames } FROM planter ` ;
61+ let selectStmt ;
62+ if ( deviceIdentifier ) {
63+ selectStmt = `SELECT planter.* FROM planter ${ this . getPlanterRegistrationJoinClause (
64+ deviceIdentifier ,
65+ ) } `;
66+ } else {
67+ selectStmt = `SELECT ${ columnNames } FROM planter` ;
68+ }
4669
4770 const params = {
4871 filter,
@@ -51,25 +74,37 @@ export class PlanterRepository extends UtilsRepositoryMixin<
5174 } ;
5275
5376 const query = buildFilterQuery ( selectStmt , params ) ;
77+ // console.log('query ---------', query);
5478
55- const result = await this . execute ( query . sql , query . params ) ;
79+ const result = await this . execute ( query . sql , query . params , options ) ;
5680 return < Planter [ ] > result . map ( ( planter ) => utils . convertCamel ( planter ) ) ;
5781 } else {
5882 throw 'Connector not defined' ;
5983 }
6084 } catch ( e ) {
6185 console . log ( e ) ;
62- return await this . find ( filter ) ;
86+ return await this . find ( filter , options ) ;
6387 }
6488 }
6589
66- async countWithOrg ( where ?: Where < Planter > ) : Promise < Count > {
67- if ( ! where ) {
68- return await this . count ( where ) ;
90+ async countWithOrg (
91+ where ?: Where < Planter > ,
92+ deviceIdentifier ?: string ,
93+ options ?: Options ,
94+ ) : Promise < Count > {
95+ if ( ! where || deviceIdentifier === null ) {
96+ return await this . count ( where , options ) ;
6997 }
7098
7199 try {
72- const selectStmt = `SELECT COUNT(*) FROM planter ` ;
100+ let selectStmt ;
101+ if ( deviceIdentifier ) {
102+ selectStmt = `SELECT COUNT(*) FROM planter ${ this . getPlanterRegistrationJoinClause (
103+ deviceIdentifier ,
104+ ) } `;
105+ } else {
106+ selectStmt = `SELECT COUNT(*) FROM planter` ;
107+ }
73108
74109 const params = {
75110 filter : { where } ,
0 commit comments