import { Count , CountSchema , Filter , repository , Where , } from '@loopback/repository' ; import { post , param , get , getFilterSchemaFor , getModelSchemaRef , getWhereSchemaFor , patch , put , del , requestBody , } from '@loopback/rest' ; import { authenticate } from '@loopback/authentication' ; import { authorize } from '@loopback/authorization' ; import { Product } from '../models' ; import { ProductRepository } from '../repositories' ; import { basicAuthorization } from '../services' ; import { OPERATION_SECURITY_SPEC } from '../utils' ; export class ProductController { constructor ( @ repository ( ProductRepository ) public productRepository ) { } @ post ( '/products' , { security : OPERATION_SECURITY_SPEC , responses : { '200' : { description : 'Product model instance' , content : { 'application/json' : { schema : getModelSchemaRef ( Product ) } } , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'admin' ] , voters : [ basicAuthorization ] } ) async create ( @ requestBody ( { content : { 'application/json' : { schema : getModelSchemaRef ( Product , { title : 'NewProduct' , exclude : [ 'productId' ] , } ) , } , } , } ) product ) { return this . productRepository . create ( product ) ; } @ get ( '/products/count' , { responses : { '200' : { description : 'Product model count' , content : { 'application/json' : { schema : CountSchema } } , } , } , } ) async count ( @ param . query . object ( 'where' , getWhereSchemaFor ( Product ) ) where ? ) { return this . productRepository . count ( where ) ; } @ get ( '/products' , { responses : { '200' : { description : 'Array of Product model instances' , content : { 'application/json' : { schema : { type : 'array' , items : getModelSchemaRef ( Product , { includeRelations : true } ) , } , } , } , } , } , } ) async find ( @ param . query . object ( 'filter' , getFilterSchemaFor ( Product ) ) filter ? ) { return this . productRepository . find ( filter ) ; } @ patch ( '/products' , { security : OPERATION_SECURITY_SPEC , responses : { '200' : { description : 'Product PATCH success count' , content : { 'application/json' : { schema : CountSchema } } , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'admin' ] , voters : [ basicAuthorization ] } ) async updateAll ( @ requestBody ( { content : { 'application/json' : { schema : getModelSchemaRef ( Product , { partial : true } ) , } , } , } ) product , @ param . query . object ( 'where' , getWhereSchemaFor ( Product ) ) where ? ) { return this . productRepository . updateAll ( product , where ) ; } @ get ( '/products/{id}' , { responses : { '200' : { description : 'Product model instance' , content : { 'application/json' : { schema : getModelSchemaRef ( Product , { includeRelations : true } ) , } , } , } , } , } ) async findById ( @ param . path . string ( 'id' ) id , @ param . query . object ( 'filter' , getFilterSchemaFor ( Product ) ) filter ? ) { return this . productRepository . findById ( id , filter ) ; } @ patch ( '/products/{id}' , { security : OPERATION_SECURITY_SPEC , responses : { '204' : { description : 'Product PATCH success' , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'admin' ] , voters : [ basicAuthorization ] } ) async updateById ( @ param . path . string ( 'id' ) id , @ requestBody ( { content : { 'application/json' : { schema : getModelSchemaRef ( Product , { partial : true } ) , } , } , } ) product ) { await this . productRepository . updateById ( id , product ) ; } @ put ( '/products/{id}' , { security : OPERATION_SECURITY_SPEC , responses : { '204' : { description : 'Product PUT success' , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'admin' ] , voters : [ basicAuthorization ] } ) async replaceById ( @ param . path . string ( 'id' ) id , @ requestBody ( ) product ) { await this . productRepository . replaceById ( id , product ) ; } @ del ( '/products/{id}' , { security : OPERATION_SECURITY_SPEC , responses : { '204' : { description : 'Product DELETE success' , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'admin' ] , voters : [ basicAuthorization ] } ) async deleteById ( @ param . path . string ( 'id' ) id ) { await this . productRepository . deleteById ( id ) ; } }