// Copyright (c) 2023 Sourcefuse Technologies // // This software is released under the MIT License. // https://opensource.org/licenses/MIT import { Count, CountSchema, Filter, FilterExcludingWhere, repository, Where, } from '@loopback/repository'; import {del, get, param, patch, post, put, requestBody} from '@loopback/rest'; import { CONTENT_TYPE, getModelSchemaRefSF, OPERATION_SECURITY_SPEC, STATUS_CODE, } from '@sourceloop/core'; import {authenticate, STRATEGY} from 'loopback4-authentication'; import {authorize} from 'loopback4-authorization'; import {Attachment} from '../models'; import {PermissionKey} from '../models/enums/permission-key.enum'; import {AttachmentRepository} from '../repositories'; const basePath = '/attachments'; export class AttachmentController { constructor( @repository(AttachmentRepository) public attachmentRepository: AttachmentRepository, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.CreateAttachment, PermissionKey.CreateAttachmentNum, ], }) @post(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attachment model instance', content: { [CONTENT_TYPE.JSON]: {schema: getModelSchemaRefSF(Attachment)}, }, }, }, }) /** * This TypeScript function creates a new Attachment object by excluding the 'id' field and returns a * Promise of the created Attachment. * @param attachment - The `attachment` parameter in the `create` function is an object that * represents an attachment. It is of type `Omit`, which means it includes all * properties of the `Attachment` type except for the `id` property. This object is used to create a * @returns The `create` method is returning a Promise that resolves to an `Attachment` object. */ async create( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attachment, { title: 'NewAttachment', exclude: ['id'], }), }, }, }) attachment: Omit, ): Promise { return this.attachmentRepository.create(attachment); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.ViewAttachment, PermissionKey.ViewAttachmentNum, ], }) @get(`${basePath}/count`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attachment model count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async count( @param.where(Attachment) where?: Where, ): Promise { return this.attachmentRepository.count(where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.ViewAttachment, PermissionKey.ViewAttachmentNum, ], }) @get(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of Attachment model instances', content: { [CONTENT_TYPE.JSON]: { schema: { type: 'array', items: getModelSchemaRefSF(Attachment, {includeRelations: true}), }, }, }, }, }, }) async find( @param.filter(Attachment) filter?: Filter, ): Promise { return this.attachmentRepository.find(filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateAttachment, PermissionKey.UpdateAttachmentNum, ], }) @patch(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attachment PATCH success count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attachment, {partial: true}), }, }, }) attachment: Attachment, @param.where(Attachment) where?: Where, ): Promise { return this.attachmentRepository.updateAll(attachment, where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.ViewAttachment, PermissionKey.ViewAttachmentNum, ], }) @get(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attachment model instance', content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attachment, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(Attachment, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.attachmentRepository.findById(id, filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateAttachment, PermissionKey.UpdateAttachmentNum, ], }) @patch(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Attachment PATCH success', }, }, }) async updateById( @param.path.string('id') id: string, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attachment, {partial: true}), }, }, }) attachment: Attachment, ): Promise { await this.attachmentRepository.updateById(id, attachment); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateAttachment, PermissionKey.UpdateAttachmentNum, ], }) @put(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Attachment PUT success', }, }, }) async replaceById( @param.path.string('id') id: string, @requestBody() attachment: Attachment, ): Promise { await this.attachmentRepository.replaceById(id, attachment); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.DeleteAttachment, PermissionKey.DeleteAttachmentNum, ], }) @del(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Attachment DELETE success', }, }, }) async deleteById(@param.path.string('id') id: string): Promise { await this.attachmentRepository.deleteById(id); } }