Skip to content

Instantly share code, notes, and snippets.

@dhmlau
Last active March 25, 2020 07:30
Show Gist options
  • Save dhmlau/e5e44ae29a613634fa03a8fbf02a4c05 to your computer and use it in GitHub Desktop.
Save dhmlau/e5e44ae29a613634fa03a8fbf02a4c05 to your computer and use it in GitHub Desktop.
LoopBack 4 controller - endpoint with date
import {
Count,
CountSchema,
Filter,
FilterBuilder,
FilterExcludingWhere,
repository,
Where,
WhereBuilder,
} from '@loopback/repository';
import {
del,
get,
getModelSchemaRef,
param,
patch,
post,
put,
requestBody,
} from '@loopback/rest';
import {Customer} from '../models';
import {CustomerRepository} from '../repositories';
export class CustomerController {
constructor(
@repository(CustomerRepository)
public customerRepository: CustomerRepository,
) {}
@post('/customers', {
responses: {
'200': {
description: 'Customer model instance',
content: {'application/json': {schema: getModelSchemaRef(Customer)}},
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Customer, {
title: 'NewCustomer',
exclude: ['id'],
}),
},
},
})
customer: Omit<Customer, 'id'>,
): Promise<Customer> {
return this.customerRepository.create(customer);
}
@get('/customers/count', {
responses: {
'200': {
description: 'Customer model count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async count(@param.where(Customer) where?: Where<Customer>): Promise<Count> {
return this.customerRepository.count(where);
}
@get('/customers', {
responses: {
'200': {
description: 'Array of Customer model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Customer, {includeRelations: true}),
},
},
},
},
},
})
async find(
@param.filter(Customer) filter?: Filter<Customer>,
): Promise<Customer[]> {
return this.customerRepository.find(filter);
}
@get('/customers/{date}', {
responses: {
'200': {
description: 'Array of Customer model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Customer, {includeRelations: true}),
},
},
},
},
},
})
async findCustomerByDate(
@param.path.string('date') creationDate: Date,
@param.filter(Customer) filter?: Filter<Customer>,
): Promise<Customer[]> {
let whereBuilder: WhereBuilder = new WhereBuilder();
whereBuilder.eq('createDate', creationDate);
let filterBuilder: FilterBuilder = new FilterBuilder(whereBuilder);
return this.customerRepository.find(filterBuilder.filter);
}
@patch('/customers', {
responses: {
'200': {
description: 'Customer PATCH success count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async updateAll(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Customer, {partial: true}),
},
},
})
customer: Customer,
@param.where(Customer) where?: Where<Customer>,
): Promise<Count> {
return this.customerRepository.updateAll(customer, where);
}
@get('/customers/{id}', {
responses: {
'200': {
description: 'Customer model instance',
content: {
'application/json': {
schema: getModelSchemaRef(Customer, {includeRelations: true}),
},
},
},
},
})
async findById(
@param.path.string('id') id: string,
@param.filter(Customer, {exclude: 'where'})
filter?: FilterExcludingWhere<Customer>,
): Promise<Customer> {
return this.customerRepository.findById(id, filter);
}
@patch('/customers/{id}', {
responses: {
'204': {
description: 'Customer PATCH success',
},
},
})
async updateById(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Customer, {partial: true}),
},
},
})
customer: Customer,
): Promise<void> {
await this.customerRepository.updateById(id, customer);
}
@put('/customers/{id}', {
responses: {
'204': {
description: 'Customer PUT success',
},
},
})
async replaceById(
@param.path.string('id') id: string,
@requestBody() customer: Customer,
): Promise<void> {
await this.customerRepository.replaceById(id, customer);
}
@del('/customers/{id}', {
responses: {
'204': {
description: 'Customer DELETE success',
},
},
})
async deleteById(@param.path.string('id') id: string): Promise<void> {
await this.customerRepository.deleteById(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment