import { Count, CountSchema, Filter, FilterExcludingWhere, repository, Where, } from '@loopback/repository'; import {post, param, get, patch, put, del, requestBody} from '@loopback/rest'; import {Plan} from '../models'; import {PlanRepository} 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 = '/plans'; export class PlanController { constructor( @repository(PlanRepository) public planRepository: PlanRepository, ) {} @authorize({ permissions: [PermissionKey.CreatePlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @post(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Plan model instance', content: { 'application/json': {schema: getModelSchemaRefSF(Plan)}, }, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRefSF(Plan, { title: 'NewPlan', exclude: ['id'], }), }, }, }) plan: Omit, ): Promise { return this.planRepository.create(plan); } @authorize({ permissions: [PermissionKey.ViewPlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @get(`${basePath}/count`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Plan model count', content: {'application/json': {schema: CountSchema}}, }, }, }) async count(@param.where(Plan) where?: Where): Promise { return this.planRepository.count(where); } @authorize({ permissions: [PermissionKey.ViewPlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @get(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of Plan model instances', content: { 'application/json': { schema: { type: 'array', items: getModelSchemaRefSF(Plan, {includeRelations: true}), }, }, }, }, }, }) async find(@param.filter(Plan) filter?: Filter): Promise { return this.planRepository.find(filter); } @authorize({ permissions: [PermissionKey.UpdatePlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @patch(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Plan PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { 'application/json': { schema: getModelSchemaRefSF(Plan, {partial: true}), }, }, }) plan: Plan, @param.where(Plan) where?: Where, ): Promise { return this.planRepository.updateAll(plan, where); } @authorize({ permissions: [PermissionKey.ViewPlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @get(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Plan model instance', content: { 'application/json': { schema: getModelSchemaRefSF(Plan, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(Plan, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.planRepository.findById(id, filter); } @authorize({ permissions: [PermissionKey.UpdatePlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @patch(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Plan PATCH success', }, }, }) async updateById( @param.path.string('id') id: string, @requestBody({ content: { 'application/json': { schema: getModelSchemaRefSF(Plan, {partial: true}), }, }, }) plan: Plan, ): Promise { await this.planRepository.updateById(id, plan); } @authorize({ permissions: [PermissionKey.UpdatePlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @put(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Plan PUT success', }, }, }) async replaceById( @param.path.string('id') id: string, @requestBody() plan: Plan, ): Promise { await this.planRepository.replaceById(id, plan); } @authorize({ permissions: [PermissionKey.DeletePlan], }) @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @del(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Plan DELETE success', }, }, }) async deleteById(@param.path.string('id') id: string): Promise { await this.planRepository.deleteById(id); } }