// 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 {Theme} from '../models'; import {PermissionKey} from '../models/enums/permission-key.enum'; import {ThemeRepository} from '../repositories'; const basePath = '/themes'; export class ThemeController { constructor( @repository(ThemeRepository) public themeRepository: ThemeRepository, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.CreateTheme, PermissionKey.CreateThemeNum], }) @post(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Theme model instance', content: {[CONTENT_TYPE.JSON]: {schema: getModelSchemaRefSF(Theme)}}, }, }, }) async create( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Theme, { title: 'NewTheme', exclude: ['id'], }), }, }, }) theme: Omit, ): Promise { return this.themeRepository.create(theme); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewTheme, PermissionKey.ViewThemeNum], }) @get(`${basePath}/count`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Theme model count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async count(@param.where(Theme) where?: Where): Promise { return this.themeRepository.count(where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewTheme, PermissionKey.ViewThemeNum], }) @get(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of Theme model instances', content: { [CONTENT_TYPE.JSON]: { schema: { type: 'array', items: getModelSchemaRefSF(Theme, {includeRelations: true}), }, }, }, }, }, }) async find(@param.filter(Theme) filter?: Filter): Promise { return this.themeRepository.find(filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.UpdateTheme, PermissionKey.UpdateThemeNum], }) @patch(basePath, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Theme PATCH success count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Theme, {partial: true}), }, }, }) theme: Theme, @param.where(Theme) where?: Where, ): Promise { return this.themeRepository.updateAll(theme, where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({ permissions: [PermissionKey.ViewTheme, PermissionKey.ViewThemeNum], }) @get(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Theme model instance', content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Theme, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(Theme, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.themeRepository.findById(id, filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: [PermissionKey.UpdateTheme]}) @patch(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Theme PATCH success', }, }, }) async updateById( @param.path.string('id') id: string, @requestBody({ content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRefSF(Theme, {partial: true}), }, }, }) theme: Theme, ): Promise { await this.themeRepository.updateById(id, theme); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: [PermissionKey.UpdateTheme]}) @put(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Theme PUT success', }, }, }) async replaceById( @param.path.string('id') id: string, @requestBody() theme: Theme, ): Promise { await this.themeRepository.replaceById(id, theme); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorize({permissions: [PermissionKey.DeleteTheme]}) @del(`${basePath}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.NO_CONTENT]: { description: 'Theme DELETE success', }, }, }) async deleteById(@param.path.string('id') id: string): Promise { await this.themeRepository.deleteById(id); } }