// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved. // Node module: @loopback/example-express-composition // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import { Count, CountSchema, Filter, repository, Where, } from '@loopback/repository'; import { del, get, getModelSchemaRef, param, patch, post, put, requestBody, } from '@loopback/rest'; import {Note} from '../models'; import {NoteRepository} from '../repositories'; export class NoteController { constructor( @repository(NoteRepository) public noteRepository: NoteRepository, ) {} @post('/notes', { responses: { '200': { description: 'Note model instance', content: {'application/json': {schema: getModelSchemaRef(Note)}}, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Note, {title: 'NewNote', exclude: ['id']}), }, }, }) note: Omit, ): Promise { return this.noteRepository.create(note); } @get('/notes/count', { responses: { '200': { description: 'Note model count', content: {'application/json': {schema: CountSchema}}, }, }, }) async count(@param.where(Note) where?: Where): Promise { return this.noteRepository.count(where); } @get('/notes', { responses: { '200': { description: 'Array of Note model instances', content: { 'application/json': { schema: {type: 'array', items: getModelSchemaRef(Note)}, }, }, }, }, }) async find( @param.filter(Note) filter?: Filter, ): Promise { return this.noteRepository.find(filter); } @patch('/notes', { responses: { '200': { description: 'Note PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Note, {partial: true}), }, }, }) note: Partial, @param.where(Note) where?: Where, ): Promise { return this.noteRepository.updateAll(note, where); } @get('/notes/{id}', { responses: { '200': { description: 'Note model instance', content: {'application/json': {schema: getModelSchemaRef(Note)}}, }, }, }) async findById(@param.path.number('id') id: number): Promise { return this.noteRepository.findById(id); } @patch('/notes/{id}', { responses: { '204': { description: 'Note PATCH success', }, }, }) async updateById( @param.path.number('id') id: number, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Note, {partial: true}), }, }, }) note: Partial, ): Promise { await this.noteRepository.updateById(id, note); } @put('/notes/{id}', { responses: { '204': { description: 'Note PUT success', }, }, }) async replaceById( @param.path.number('id') id: number, @requestBody() note: Note, ): Promise { await this.noteRepository.replaceById(id, note); } @del('/notes/{id}', { responses: { '204': { description: 'Note DELETE success', }, }, }) async deleteById(@param.path.number('id') id: number): Promise { await this.noteRepository.deleteById(id); } }