-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the endpoint to retrieve the fflags
- Loading branch information
1 parent
6d33dae
commit 0c1935c
Showing
10 changed files
with
281 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
expect, stubExpressContext, | ||
} from '@loopback/testlab'; | ||
import { FeaturesController } from '../../controllers/features.controller'; | ||
import { FeaturesDataService } from '../../services'; | ||
|
||
describe('FeaturesController (unit)', () => { | ||
const mockedService = <FeaturesDataService>{}; | ||
let context = stubExpressContext(); | ||
|
||
|
||
describe('get()',() => { | ||
it('retrieves the features flags Information', async() => { | ||
const controller = new FeaturesController(context.response, mockedService); | ||
await controller.get(); | ||
let result = await context.result; | ||
expect(result.payload).not.null(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
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,68 @@ | ||
import { inject } from '@loopback/core'; | ||
import { getLogger, Logger } from 'log4js'; | ||
import { RestBindings, get, getModelSchemaRef, Response, } from '@loopback/rest'; | ||
import { ServicesBindings } from '../dependency-injection-bindings'; | ||
import { FeaturesDataService } from '../services/features-data.service'; | ||
import { FeaturesDbDataModel } from '../models/features-data.model'; | ||
|
||
export class FeaturesController { | ||
logger: Logger; | ||
private featuresDatService: FeaturesDataService; | ||
HTTP_SUCCESS_OK = 200; | ||
HTTP_ERROR = 500; | ||
constructor( | ||
@inject(RestBindings.Http.RESPONSE) private response: Response, | ||
@inject(ServicesBindings.FEATURES_SERVICE) | ||
featuresDatService: FeaturesDataService, | ||
) { | ||
this.featuresDatService = featuresDatService; | ||
this.logger = getLogger('features-controller'); | ||
} | ||
|
||
@get('/features', { | ||
responses: { | ||
'200': { | ||
description: 'Get the feature flags info', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: getModelSchemaRef(FeaturesDbDataModel, { | ||
includeRelations: true, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
'500': { | ||
description: 'Could not retrieve the features', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: getModelSchemaRef(FeaturesDbDataModel, { | ||
includeRelations: true, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
public async get(): Promise<Response> { | ||
this.logger.debug('[get] started'); | ||
let retorno = [new FeaturesDbDataModel()]; | ||
let responseCode = this.HTTP_ERROR; | ||
try { | ||
retorno = await this.featuresDatService.getAll(); | ||
responseCode = this.HTTP_SUCCESS_OK; | ||
this.logger.info(`[get] Retrieved the features: ${retorno}`); | ||
} catch (e) { | ||
this.logger.warn(`[get] Got an error: ${e}`); | ||
} | ||
this.response.contentType('application/json').status(responseCode).send( | ||
retorno | ||
); | ||
return this.response; | ||
} | ||
} |
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,72 @@ | ||
import { SearchableModel } from "./rsk/searchable-model"; | ||
import {Entity, model, property} from '@loopback/repository'; | ||
|
||
export interface FeaturesDataModel { | ||
creationDate: Date; | ||
lastUpdateDate: Date; | ||
name: string; | ||
value: string; | ||
version: number; | ||
} | ||
|
||
export class FeaturesAppDataModel implements FeaturesDataModel{ | ||
constructor(data?: Partial<FeaturesAppDataModel>) { | ||
Object.assign(this, data); | ||
} | ||
|
||
creationDate: Date; | ||
lastUpdateDate: Date; | ||
name: string; | ||
value: string; | ||
version: number; | ||
} | ||
|
||
@model({settings: {strict: false}}) | ||
export class FeaturesDbDataModel implements SearchableModel, FeaturesDataModel { | ||
|
||
@property({ | ||
type: 'date', | ||
defaultFn: 'now' | ||
}) | ||
creationDate: Date; | ||
|
||
@property({ | ||
type: 'date', | ||
defaultFn: 'now' | ||
}) | ||
lastUpdateDate: Date; | ||
|
||
@property({ | ||
type: 'string', | ||
}) | ||
name: string; | ||
|
||
@property({ | ||
type: 'string', | ||
}) | ||
value: string; | ||
|
||
@property({ | ||
type: 'number', | ||
}) | ||
version: number; | ||
|
||
getId() { | ||
return this.name; | ||
} | ||
getIdFieldName(): string { | ||
return 'name'; | ||
} | ||
|
||
public static clone(other: FeaturesDbDataModel): FeaturesDbDataModel { | ||
const features: FeaturesDbDataModel = new FeaturesDbDataModel(); | ||
features.creationDate = other.creationDate; | ||
features.lastUpdateDate = other.lastUpdateDate; | ||
features.name = other.name; | ||
features.value = other.value; | ||
features.version = other.version; | ||
return features; | ||
} | ||
|
||
} | ||
|
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,56 @@ | ||
import {Entity, model, property} from '@loopback/repository'; | ||
|
||
|
||
export class Features extends Entity { | ||
|
||
@property({ | ||
type: 'date', | ||
defaultFn: 'now' | ||
}) | ||
creationDate: Date; | ||
|
||
@property({ | ||
type: 'date', | ||
defaultFn: 'now' | ||
}) | ||
lastUpdateDate: Date; | ||
|
||
@property({ | ||
type: 'string', | ||
}) | ||
name: string; | ||
|
||
@property({ | ||
type: 'string', | ||
}) | ||
value: string; | ||
|
||
@property({ | ||
type: 'number', | ||
}) | ||
version: number; | ||
|
||
@property({ | ||
type: 'number', | ||
id: true, | ||
generated: true, | ||
}) | ||
id?: number; | ||
|
||
// Define well-known properties here | ||
|
||
// Indexer property to allow additional data | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[prop: string]: any; | ||
|
||
constructor(data?: Partial<Features>) { | ||
super(data); | ||
} | ||
getIdFieldName(): string { | ||
return 'id'; | ||
} | ||
getId() { | ||
return this.id; | ||
} | ||
|
||
} |
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,6 @@ | ||
import { GenericDataService } from './generic-data-service'; | ||
import { FeaturesDbDataModel } from '../models/features-data.model'; | ||
|
||
export interface FeaturesDataService extends GenericDataService<FeaturesDbDataModel> { | ||
getAll(): Promise<FeaturesDbDataModel[]>; | ||
} |
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 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import mongoose from 'mongoose'; | ||
import { FeaturesDataService } from './features-data.service'; | ||
import {FeaturesDbDataModel} from '../models/features-data.model'; | ||
import {MongoDbDataService} from '../services/mongodb-data.service'; | ||
|
||
/* | ||
- THESE MODEL INTERFACES AND CLASSES ARE REQUIRED FOR MONGO BUT WE DON'T WANT THEM EXPOSED OUT OF THIS LAYER | ||
*/ | ||
interface FeaturesMongoModel extends mongoose.Document, FeaturesDbDataModel { | ||
} | ||
|
||
const FeaturesSchema = new mongoose.Schema({ | ||
creationDate: {type: Date}, | ||
lastUpdateDate: {type: Date}, | ||
name: {type: String, required: true}, | ||
value: {type: String, required: true}, | ||
version: {type: Number, required: true}, | ||
}); | ||
|
||
const FeaturesConnector = mongoose.model<FeaturesMongoModel>("Features", FeaturesSchema); | ||
|
||
export class FeaturesMongoDbDataService extends MongoDbDataService<FeaturesDbDataModel, FeaturesMongoModel> implements FeaturesDataService { | ||
protected getByIdFilter(id: any) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
protected getManyFilter(filter?: any) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
protected getLoggerName(): string { | ||
return 'FeaturesMongoService'; | ||
} | ||
protected getConnector(): mongoose.Model<FeaturesMongoModel, {}, {}> { | ||
this.verifyAndCreateConnectionIfIsNecessary(); | ||
return FeaturesConnector; | ||
} | ||
async verifyAndCreateConnectionIfIsNecessary() { | ||
await this.ensureConnection(); | ||
} | ||
public async getAll(): Promise<FeaturesDbDataModel[]> { | ||
const documents = await this.getConnector() | ||
.find({}) | ||
.exec(); | ||
return documents.map(FeaturesDbDataModel.clone); | ||
} | ||
|
||
} |
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