// Copyright IBM Corp. and LoopBack contributors 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 {inject} from '@loopback/core'; import { Count, CountSchema, Filter, FilterExcludingWhere, repository, Where, } from '@loopback/repository'; import { del, get, getModelSchemaRef, HttpErrors, param, patch, post, put, requestBody, } from '@loopback/rest'; import {Todo} from '../models'; import {TodoRepository} from '../repositories'; import {Geocoder} from '../services'; export class TodoController { constructor( @repository(TodoRepository) public todoRepository: TodoRepository, @inject('services.Geocoder') protected geoService: Geocoder, ) {} @post('/todos', { responses: { '200': { description: 'Todo model instance', content: {'application/json': {schema: getModelSchemaRef(Todo)}}, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, { title: 'NewTodo', exclude: ['id'], }), }, }, }) todo: Omit, ): Promise { if (todo.remindAtAddress) { const geo = await this.geoService.geocode(todo.remindAtAddress); // ignoring because if the service is down, the following section will // not be covered /* istanbul ignore next */ if (!geo[0]) { // address not found throw new HttpErrors.BadRequest( `Address not found: ${todo.remindAtAddress}`, ); } // Encode the coordinates as "lat,lng" (Google Maps API format). See also // https://stackoverflow.com/q/7309121/69868 // https://gis.stackexchange.com/q/7379 todo.remindAtGeo = `${geo[0].y},${geo[0].x}`; } return this.todoRepository.create(todo); } @get('/todos/{id}', { responses: { '200': { description: 'Todo model instance', content: { 'application/json': { schema: getModelSchemaRef(Todo, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.number('id') id: number, @param.filter(Todo, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.todoRepository.findById(id, filter); } @get('/todos', { responses: { '200': { description: 'Array of Todo model instances', content: { 'application/json': { schema: { type: 'array', items: getModelSchemaRef(Todo, {includeRelations: true}), }, }, }, }, }, }) async find(@param.filter(Todo) filter?: Filter): Promise { return this.todoRepository.find(filter); } @put('/todos/{id}', { responses: { '204': { description: 'Todo PUT success', }, }, }) async replaceById( @param.path.number('id') id: number, @requestBody() todo: Todo, ): Promise { await this.todoRepository.replaceById(id, todo); } @patch('/todos/{id}', { responses: { '204': { description: 'Todo PATCH success', }, }, }) async updateById( @param.path.number('id') id: number, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, {partial: true}), }, }, }) todo: Todo, ): Promise { await this.todoRepository.updateById(id, todo); } @del('/todos/{id}', { responses: { '204': { description: 'Todo DELETE success', }, }, }) async deleteById(@param.path.number('id') id: number): Promise { await this.todoRepository.deleteById(id); } @get('/todos/count', { responses: { '200': { description: 'Todo model count', content: {'application/json': {schema: CountSchema}}, }, }, }) async count(@param.where(Todo) where?: Where): Promise { return this.todoRepository.count(where); } @patch('/todos', { responses: { '200': { description: 'Todo PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, {partial: true}), }, }, }) todo: Todo, @param.where(Todo) where?: Where, ): Promise { return this.todoRepository.updateAll(todo, where); } }