import {
Count,
CountSchema,
Filter,
FilterExcludingWhere,
repository,
Where,
} from '@loopback/repository';
import {
post,
param,
get,
getModelSchemaRef,
patch,
put,
del,
requestBody,
} from '@loopback/rest';
import {Resource} from '../models';
import {ResourceRepository} from '../repositories';
import {authorize} from 'loopback4-authorization';
import {authenticate, STRATEGY} from 'loopback4-authentication';
import {PermissionKey} from '../permissions';
import {OPERATION_SECURITY_SPEC, STATUS_CODE} from '@sourceloop/core';
const basePath = '/resources';
export class ResourceController {
constructor(
@repository(ResourceRepository)
public resourceRepository: ResourceRepository,
) {}
@authorize({
permissions: [PermissionKey.CreateResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@post(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Resource model instance',
content: {'application/json': {schema: getModelSchemaRef(Resource)}},
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Resource, {
title: 'NewResource',
exclude: ['id'],
}),
},
},
})
resource: Omit<Resource, 'id'>,
): Promise<Resource> {
return this.resourceRepository.create(resource);
}
@authorize({
permissions: [PermissionKey.ViewResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@get(`${basePath}/count`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Resource model count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async count(@param.where(Resource) where?: Where<Resource>): Promise<Count> {
return this.resourceRepository.count(where);
}
@authorize({
permissions: [PermissionKey.ViewResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@get(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Array of Resource model instances',
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(Resource, {includeRelations: true}),
},
},
},
},
},
})
async find(
@param.filter(Resource) filter?: Filter<Resource>,
): Promise<Resource[]> {
return this.resourceRepository.find(filter);
}
@authorize({
permissions: [PermissionKey.UpdateResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@patch(basePath, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Resource PATCH success count',
content: {'application/json': {schema: CountSchema}},
},
},
})
async updateAll(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Resource, {partial: true}),
},
},
})
resource: Resource,
@param.where(Resource) where?: Where<Resource>,
): Promise<Count> {
return this.resourceRepository.updateAll(resource, where);
}
@authorize({
permissions: [PermissionKey.ViewResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@get(`${basePath}/{id}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Resource model instance',
content: {
'application/json': {
schema: getModelSchemaRef(Resource, {includeRelations: true}),
},
},
},
},
})
async findById(
@param.path.string('id') id: string,
@param.filter(Resource, {exclude: 'where'})
filter?: FilterExcludingWhere<Resource>,
): Promise<Resource> {
return this.resourceRepository.findById(id, filter);
}
@authorize({
permissions: [PermissionKey.UpdateResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@patch(`${basePath}/{id}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'Resource PATCH success',
},
},
})
async updateById(
@param.path.string('id') id: string,
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Resource, {partial: true}),
},
},
})
resource: Resource,
): Promise<void> {
await this.resourceRepository.updateById(id, resource);
}
@authorize({
permissions: [PermissionKey.UpdateResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@put(`${basePath}/{id}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'Resource PUT success',
},
},
})
async replaceById(
@param.path.string('id') id: string,
@requestBody() resource: Resource,
): Promise<void> {
await this.resourceRepository.replaceById(id, resource);
}
@authorize({
permissions: [PermissionKey.DeleteResource],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@del(`${basePath}/{id}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'Resource DELETE success',
},
},
})
async deleteById(@param.path.string('id') id: string): Promise<void> {
await this.resourceRepository.deleteById(id);
}
}