// 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 {Settings} from '../models'; import {PermissionKey} from '../models/enums/permission-key.enum'; import {SettingsRepository} from '../repositories'; const basePath = '/settings'; export class SettingsController { constructor( @repository(SettingsRepository) public settingsRepository: SettingsRepository, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.CreateSettings, PermissionKey.CreateSettingsNum, ], }) @post(basePath, { description: 'Create any new settings', security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Settings model instance', content: {[CONTENT_TYPE.JSON]: {schema: getModelSchemaRefSF(Settings)}}, }, }, }) async create( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Settings, { title: 'NewSettings', exclude: ['id'], }), }, }, }) settings: Omit, ): Promise { return this.settingsRepository.create(settings); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewSettings, PermissionKey.ViewSettingsNum], }) @get(`${basePath}/count`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Settings model count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async count(@param.where(Settings) where?: Where): Promise { return this.settingsRepository.count(where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewSettings, PermissionKey.ViewSettingsNum], }) @get(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of Settings model instances', content: { [CONTENT_TYPE.JSON]: { schema: { type: 'array', items: getModelSchemaRefSF(Settings, {includeRelations: true}), }, }, }, }, }, }) async find( @param.filter(Settings) filter?: Filter, ): Promise { return this.settingsRepository.find(filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateSettings, PermissionKey.UpdateSettingsNum, ], }) @patch(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Settings PATCH success count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Settings, {partial: true}), }, }, }) settings: Settings, @param.where(Settings) where?: Where, ): Promise { return this.settingsRepository.updateAll(settings, where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewSettings, PermissionKey.ViewSettingsNum], }) @get(`${basePath}/{id}`, { description: 'These requests will be available to everyone in the setting to look at.', security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Settings model instance', content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Settings, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(Settings, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.settingsRepository.findById(id, filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateSettings, PermissionKey.UpdateSettingsNum, ], }) @patch(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Settings PATCH success', }, }, }) async updateById( @param.path.string('id') id: string, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Settings, {partial: true}), }, }, }) settings: Settings, ): Promise { await this.settingsRepository.updateById(id, settings); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.UpdateSettings, PermissionKey.UpdateSettingsNum, ], }) @put(`${basePath}/{id}`, { description: 'Update setting.', security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Settings PUT success', }, }, }) async replaceById( @param.path.string('id') id: string, @requestBody() settings: Settings, ): Promise { await this.settingsRepository.replaceById(id, settings); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [ PermissionKey.DeleteSettings, PermissionKey.DeleteSettingsNum, ], }) @del(`${basePath}/{id}`, { description: 'Delete setting.', security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Settings DELETE success', }, }, }) async deleteById(@param.path.string('id') id: string): Promise { await this.settingsRepository.deleteById(id); } }