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