Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update todo-list example #4412

Merged
merged 3 commits into from
Jan 23, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(example-todo): use latest cli code
nabdelgadir committed Jan 23, 2020
commit 11ab4bd6b8c9a3aca4e1d323de401d8edbac35c2
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
nabdelgadir marked this conversation as resolved.
Show resolved Hide resolved

# 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
@@ -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,
@@ -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);
});

4 changes: 2 additions & 2 deletions examples/todo/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -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';

/*
==============================================================================
@@ -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;
Original file line number Diff line number Diff line change
@@ -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,
@@ -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;
@@ -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
@@ -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;

18 changes: 9 additions & 9 deletions examples/todo/src/controllers/todo.controller.ts
Original file line number Diff line number Diff line change
@@ -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', {
@@ -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}', {
@@ -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', {
@@ -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}', {
@@ -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}', {
@@ -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}', {
@@ -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
@@ -10,6 +10,7 @@ export class Todo extends Entity {
@property({
type: 'number',
id: true,
generated: false,
})
id?: number;

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 {
/**
@@ -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);
}
}