import { Count , CountSchema , Filter , FilterExcludingWhere , repository , Where , } from '@loopback/repository' ; import { post , param , get , getModelSchemaRef , patch , put , del , requestBody , } from '@loopback/rest' ; import { CONTENT_TYPE , OPERATION_SECURITY_SPEC , STATUS_CODE , } from '@sourceloop/core' ; import { authenticate , STRATEGY } from 'loopback4-authentication' ; import { authorize } from 'loopback4-authorization' ; import { PermissionKey } from '../enums' ; import { Message } from '../models' ; import { MessageRepository } from '../repositories' ; const basePath = '/messages' ; export class MessageController { constructor ( @ repository ( MessageRepository ) public messageRepository ) { } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . CreateMessage ] } ) @ post ( basePath , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . OK ] : { description : 'Message model instance' , content : { [ CONTENT_TYPE . JSON ] : { schema : getModelSchemaRef ( Message ) } } , } , } , } ) async create ( @ requestBody ( { content : { [ CONTENT_TYPE . JSON ] : { schema : getModelSchemaRef ( Message , { title : 'NewMessage' , exclude : [ 'id' ] , } ) , } , } , } ) message ) { return this . messageRepository . create ( message ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . ViewMessage ] } ) @ get ( `${ basePath }/count` , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . OK ] : { description : 'Message model count' , content : { [ CONTENT_TYPE . JSON ] : { schema : CountSchema } } , } , } , } ) async count ( @ param . where ( Message ) where ? ) { return this . messageRepository . count ( where ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . ViewMessage ] } ) @ get ( '/messages' , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . OK ] : { description : 'Array of Message model instances' , content : { [ CONTENT_TYPE . JSON ] : { schema : { type : 'array' , items : getModelSchemaRef ( Message , { includeRelations : true } ) , } , } , } , } , } , } ) async find ( @ param . filter ( Message ) filter ? ) { return this . messageRepository . find ( filter ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . UpdateMessage ] } ) @ patch ( basePath , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . OK ] : { description : 'Message PATCH success count' , content : { [ CONTENT_TYPE . JSON ] : { schema : CountSchema } } , } , } , } ) async updateAll ( @ requestBody ( { content : { [ CONTENT_TYPE . JSON ] : { schema : getModelSchemaRef ( Message , { partial : true } ) , } , } , } ) message , @ param . where ( Message ) where ? ) { return this . messageRepository . updateAll ( message , where ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . ViewMessage ] } ) @ get ( `${ basePath }/{id}` , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . OK ] : { description : 'Message model instance' , content : { [ CONTENT_TYPE . JSON ] : { schema : getModelSchemaRef ( Message , { includeRelations : true } ) , } , } , } , } , } ) async findById ( @ param . path . string ( 'id' ) id , @ param . filter ( Message , { exclude : 'where' } ) filter ? ) { return this . messageRepository . findById ( id , filter ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . UpdateMessage ] } ) @ patch ( `${ basePath }/{id}` , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . NO_CONTENT ] : { description : 'Message PATCH success' , } , } , } ) async updateById ( @ param . path . string ( 'id' ) id , @ requestBody ( { content : { [ CONTENT_TYPE . JSON ] : { schema : getModelSchemaRef ( Message , { partial : true } ) , } , } , } ) message ) { await this . messageRepository . updateById ( id , message ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . UpdateMessage ] } ) @ put ( `${ basePath }/{id}` , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . NO_CONTENT ] : { description : 'Message PUT success' , } , } , } ) async replaceById ( @ param . path . string ( 'id' ) id , @ requestBody ( ) message ) { await this . messageRepository . replaceById ( id , message ) ; } @ authenticate ( STRATEGY . BEARER ) @ authorize ( { permissions : [ PermissionKey . DeleteMessage ] } ) @ del ( `${ basePath }/{id}` , { security : OPERATION_SECURITY_SPEC , responses : { [ STATUS_CODE . NO_CONTENT ] : { description : 'Message DELETE success' , } , } , } ) async deleteById ( @ param . path . string ( 'id' ) id ) { await this . messageRepository . deleteById ( id ) ; } }