import { Count, CountSchema, Filter, FilterExcludingWhere, repository, Where, } from '@loopback/repository'; import {post, param, get, 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, getModelSchemaRefSF, } 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: getModelSchemaRefSF(Resource)}}, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRefSF(Resource, { title: 'NewResource', exclude: ['id'], }), }, }, }) resource: Omit, ): Promise { 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): Promise { 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: getModelSchemaRefSF(Resource, {includeRelations: true}), }, }, }, }, }, }) async find( @param.filter(Resource) filter?: Filter, ): Promise { 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: getModelSchemaRefSF(Resource, {partial: true}), }, }, }) resource: Resource, @param.where(Resource) where?: Where, ): Promise { 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: getModelSchemaRefSF(Resource, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(Resource, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { 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: getModelSchemaRefSF(Resource, {partial: true}), }, }, }) resource: Resource, ): Promise { 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 { 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 { await this.resourceRepository.deleteById(id); } }