Skip to content

Commit

Permalink
fix(example-todo): use latest cli code
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jan 23, 2020
1 parent e1c79e6 commit 11ab4bd
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 25 deletions.
5 changes: 5 additions & 0 deletions examples/todo/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
/dist
# Cache used by TypeScript's incremental build
*.tsbuildinfo
28 changes: 28 additions & 0 deletions examples/todo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

RUN npm install

# Bundle app source code
COPY --chown=node . .

RUN npm run build

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}
CMD [ "node", "." ]
4 changes: 2 additions & 2 deletions examples/todo/src/__tests__/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import {TodoListApplication} from '../../application';
import {Todo} from '../../models/';
import {TodoRepository} from '../../repositories/';
import {GeocoderService} from '../../services';
import {Geocoder} from '../../services';
import {
aLocation,
getProxiedGeoCoderConfig,
Expand All @@ -40,7 +40,7 @@ describe('TodoApplication', () => {
before(async function() {
// eslint-disable-next-line no-invalid-this
this.timeout(30 * 1000);
const service = await app.get<GeocoderService>('services.GeocoderService');
const service = await app.get<Geocoder>('services.Geocoder');
available = await isGeoCoderServiceAvailable(service);
});

Expand Down
4 changes: 2 additions & 2 deletions examples/todo/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {merge} from 'lodash';
import path from 'path';
import GEO_CODER_CONFIG from '../datasources/geocoder.datasource.config.json';
import {Todo} from '../models/index';
import {GeocoderService, GeoPoint} from '../services/geocoder.service';
import {Geocoder, GeoPoint} from '../services/geocoder.service';

/*
==============================================================================
Expand Down Expand Up @@ -74,7 +74,7 @@ export async function givenCachingProxy() {
return proxy;
}

export async function isGeoCoderServiceAvailable(service: GeocoderService) {
export async function isGeoCoderServiceAvailable(service: Geocoder) {
try {
await service.geocode(aLocation.address);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {expect} from '@loopback/testlab';
import {GeocoderDataSource} from '../../../datasources/geocoder.datasource';
import {GeocoderService, GeocoderServiceProvider} from '../../../services';
import {Geocoder, GeocoderProvider} from '../../../services';
import {
aLocation,
getProxiedGeoCoderConfig,
Expand All @@ -22,7 +22,7 @@ describe('GeoLookupService', function() {
before(async () => (cachingProxy = await givenCachingProxy()));
after(() => cachingProxy.stop());

let service: GeocoderService;
let service: Geocoder;
before(givenGeoService);

let available = true;
Expand All @@ -42,6 +42,6 @@ describe('GeoLookupService', function() {
async function givenGeoService() {
const config = getProxiedGeoCoderConfig(cachingProxy);
const dataSource = new GeocoderDataSource(config);
service = await new GeocoderServiceProvider(dataSource).value();
service = await new GeocoderProvider(dataSource).value();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
import {TodoController} from '../../../controllers';
import {Todo} from '../../../models/index';
import {TodoRepository} from '../../../repositories';
import {GeocoderService} from '../../../services';
import {Geocoder} from '../../../services';
import {aLocation, givenTodo} from '../../helpers';

describe('TodoController', () => {
let todoRepo: StubbedInstanceWithSinonAccessor<TodoRepository>;
let geoService: GeocoderService;
let geoService: Geocoder;

let geocode: sinon.SinonStub;

Expand Down
18 changes: 9 additions & 9 deletions examples/todo/src/controllers/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import {
} from '@loopback/rest';
import {Todo} from '../models';
import {TodoRepository} from '../repositories';
import {GeocoderService} from '../services';
import {Geocoder} from '../services';

export class TodoController {
constructor(
@repository(TodoRepository) protected todoRepo: TodoRepository,
@inject('services.GeocoderService') protected geoService: GeocoderService,
@repository(TodoRepository) protected todoRepository: TodoRepository,
@inject('services.Geocoder') protected geoService: Geocoder,
) {}

@post('/todos', {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class TodoController {
// https://gis.stackexchange.com/q/7379
todo.remindAtGeo = `${geo[0].y},${geo[0].x}`;
}
return this.todoRepo.create(todo);
return this.todoRepository.create(todo);
}

@get('/todos/{id}', {
Expand All @@ -67,7 +67,7 @@ export class TodoController {
@param.path.number('id') id: number,
@param.query.boolean('items') items?: boolean,
): Promise<Todo> {
return this.todoRepo.findById(id);
return this.todoRepository.findById(id);
}

@get('/todos', {
Expand All @@ -86,7 +86,7 @@ export class TodoController {
@param.query.object('filter', getFilterSchemaFor(Todo))
filter?: Filter<Todo>,
): Promise<Todo[]> {
return this.todoRepo.find(filter);
return this.todoRepository.find(filter);
}

@put('/todos/{id}', {
Expand All @@ -100,7 +100,7 @@ export class TodoController {
@param.path.number('id') id: number,
@requestBody() todo: Todo,
): Promise<void> {
await this.todoRepo.replaceById(id, todo);
await this.todoRepository.replaceById(id, todo);
}

@patch('/todos/{id}', {
Expand All @@ -121,7 +121,7 @@ export class TodoController {
})
todo: Partial<Todo>,
): Promise<void> {
await this.todoRepo.updateById(id, todo);
await this.todoRepository.updateById(id, todo);
}

@del('/todos/{id}', {
Expand All @@ -132,6 +132,6 @@ export class TodoController {
},
})
async deleteTodo(@param.path.number('id') id: number): Promise<void> {
await this.todoRepo.deleteById(id);
await this.todoRepository.deleteById(id);
}
}
1 change: 1 addition & 0 deletions examples/todo/src/models/todo.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class Todo extends Entity {
@property({
type: 'number',
id: true,
generated: false,
})
id?: number;

Expand Down
15 changes: 8 additions & 7 deletions examples/todo/src/services/geocoder.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: @loopback/example-todo
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {getService, juggler} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {GeocoderDataSource} from '../datasources/geocoder.datasource';
import {getService} from '@loopback/service-proxy';
import {GeocoderDataSource} from '../datasources';

export interface GeoPoint {
/**
Expand All @@ -19,17 +19,18 @@ export interface GeoPoint {
x: number;
}

export interface GeocoderService {
export interface Geocoder {
geocode(address: string): Promise<GeoPoint[]>;
}

export class GeocoderServiceProvider implements Provider<GeocoderService> {
export class GeocoderProvider implements Provider<Geocoder> {
constructor(
// geocoder must match the name property in the datasource json file
@inject('datasources.geocoder')
protected dataSource: juggler.DataSource = new GeocoderDataSource(),
protected dataSource: GeocoderDataSource = new GeocoderDataSource(),
) {}

value(): Promise<GeocoderService> {
value(): Promise<Geocoder> {
return getService(this.dataSource);
}
}

0 comments on commit 11ab4bd

Please sign in to comment.