// 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 {Attendee} from '../models'; import {PermissionKey} from '../models/enums/permission-key.enum'; import {AttendeeRepository} from '../repositories'; const basePath = '/attendees'; export class AttendeeController { constructor( @repository(AttendeeRepository) public attendeeRepository: AttendeeRepository, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.CreateAttendee, PermissionKey.CreateAttendeeNum, ], }) @post(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attendee model instance', content: {[CONTENT_TYPE.JSON]: {schema: getModelSchemaRefSF(Attendee)}}, }, }, }) async create( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attendee, { title: 'NewAttendee', exclude: ['id'], }), }, }, }) attendee: Omit, ): Promise { return this.attendeeRepository.create(attendee); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewAttendee, PermissionKey.ViewAttendeeNum], }) @get(`${basePath}/count`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attendee model count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async count(@param.where(Attendee) where?: Where): Promise { return this.attendeeRepository.count(where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewAttendee, PermissionKey.ViewAttendeeNum], }) @get(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of Attendee model instances', content: { [CONTENT_TYPE.JSON]: { schema: { type: 'array', items: getModelSchemaRefSF(Attendee, {includeRelations: true}), }, }, }, }, }, }) async find( @param.filter(Attendee) filter?: Filter, ): Promise { return this.attendeeRepository.find(filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateAttendee, PermissionKey.UpdateAttendeeNum, ], }) @patch(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attendee PATCH success count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attendee, {partial: true}), }, }, }) attendee: Attendee, @param.where(Attendee) where?: Where, ): Promise { return this.attendeeRepository.updateAll(attendee, where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewAttendee, PermissionKey.ViewAttendeeNum], }) @get(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Attendee model instance', content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attendee, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(Attendee, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.attendeeRepository.findById(id, filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateAttendee, PermissionKey.UpdateAttendeeNum, ], }) @patch(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Attendee PATCH success', }, }, }) async updateById( @param.path.string('id') id: string, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Attendee, {partial: true}), }, }, }) attendee: Attendee, ): Promise { await this.attendeeRepository.updateById(id, attendee); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateAttendee, PermissionKey.UpdateAttendeeNum, ], }) @put(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Attendee PUT success', }, }, }) async replaceById( @param.path.string('id') id: string, @requestBody() attendee: Attendee, ): Promise { await this.attendeeRepository.replaceById(id, attendee); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.DeleteAttendee, PermissionKey.DeleteAttendeeNum, ], }) @del(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Attendee DELETE success', }, }, }) async deleteById(@param.path.string('id') id: string): Promise { await this.attendeeRepository.deleteById(id); } }