From ac8c5d52ec0fd2fcea46becc25579c6c037f52ec Mon Sep 17 00:00:00 2001 From: Maksym Date: Mon, 14 Oct 2024 19:19:52 +0200 Subject: [PATCH] deleted: src/app.controller.spec.ts modified: src/app.module.ts modified: src/app.service.ts new file: src/bus/bus.controller.ts deleted: src/strip.controller.ts new file: src/strip/strip.controller.ts --- src/app.controller.spec.ts | 22 ----------- src/app.module.ts | 5 ++- src/app.service.ts | 8 ++++ src/bus/bus.controller.ts | 58 +++++++++++++++++++++++++++++ src/strip.controller.ts | 40 -------------------- src/strip/strip.controller.ts | 69 +++++++++++++++++++++++++++++++++++ 6 files changed, 138 insertions(+), 64 deletions(-) delete mode 100644 src/app.controller.spec.ts create mode 100644 src/bus/bus.controller.ts delete mode 100644 src/strip.controller.ts create mode 100644 src/strip/strip.controller.ts diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f389..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/app.module.ts b/src/app.module.ts index 2d379d5..3700ae0 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,11 +1,12 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { StripController } from './strip.controller'; +import { StripController } from './strip/strip.controller'; +import { Bus } from './bus/bus.controller'; @Module({ imports: [], controllers: [AppController, StripController], - providers: [AppService], + providers: [AppService, Bus], }) export class AppModule {} diff --git a/src/app.service.ts b/src/app.service.ts index 22af028..aacc9e2 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -47,4 +47,12 @@ export class AppService implements OnModuleInit { setGain(IOType: IOType, id: number, gain: number) { return (this.strips[id].Gain = gain); } + + getMuteState(IOType: IOType, id: number) { + return this.strips[id].Mute; + } + + setMuteState(IOType: IOType, id: number, state: boolean) { + return (this.strips[id].Mute = state); + } } diff --git a/src/bus/bus.controller.ts b/src/bus/bus.controller.ts new file mode 100644 index 0000000..2871058 --- /dev/null +++ b/src/bus/bus.controller.ts @@ -0,0 +1,58 @@ +import { + Body, + Controller, + Get, + HttpException, + HttpStatus, + Param, + Patch, +} from '@nestjs/common'; +import { AppService, IOType } from 'src/app.service'; + +@Controller('/bus/:busId') +export class Bus { + constructor(private readonly appService: AppService) {} + + @Get('/gain') + getGain(@Param('busId') busId: string) { + return { gain: this.appService.getGain(IOType.BUS, +busId) }; + } + @Patch('/gain') + setGain(@Param('busId') busId: string, @Body() { gain }: { gain: number }) { + if (gain === undefined) + throw new HttpException( + '"gain" parameter not specified', + HttpStatus.BAD_REQUEST, + ); + if (typeof gain !== 'number' || isNaN(gain)) + throw new HttpException( + 'Invalid "gain" parameter type. Must be number', + HttpStatus.BAD_REQUEST, + ); + return { gain: this.appService.setGain(IOType.BUS, +busId, gain) }; + } + + @Get('/mute') + getMuteState(@Param('busId') busId: string) { + return { muteState: this.appService.getMuteState(IOType.BUS, +busId) }; + } + @Patch('/mute') + setMuteState( + @Param('busId') busId: string, + @Body() { muteState }: { muteState: boolean }, + ) { + if (muteState === undefined) + throw new HttpException( + '"muteState" parameter not specified', + HttpStatus.BAD_REQUEST, + ); + if (typeof muteState !== 'boolean') + throw new HttpException( + 'Invalid "muteState" parameter type. Must be boolean', + HttpStatus.BAD_REQUEST, + ); + return { + muteState: this.appService.setMuteState(IOType.BUS, +busId, muteState), + }; + } +} diff --git a/src/strip.controller.ts b/src/strip.controller.ts deleted file mode 100644 index 4db0bbe..0000000 --- a/src/strip.controller.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { - Body, - Controller, - Get, - Header, - HostParam, - HttpException, - HttpStatus, - Param, - Patch, - Post, - Query, -} from '@nestjs/common'; -import { AppService, IOType } from './app.service'; - -@Controller('/strip/:stripId') -export class StripController { - constructor(private readonly appService: AppService) {} - @Get('/gain') - getGain(@Param('stripId') stripId: string) { - return { gain: this.appService.getGain(IOType.STRIP, +stripId) }; - } - @Patch('/gain') - setGain( - @Param('stripId') stripId: string, - @Body() { gain }: { gain: number }, - ) { - if (gain === undefined) - throw new HttpException( - 'Gain parameter not specified', - HttpStatus.BAD_REQUEST, - ); - if (typeof gain !== 'number' || isNaN(gain)) - throw new HttpException( - 'Invalid gain parameter type. Must be a number', - HttpStatus.BAD_REQUEST, - ); - return { gain: this.appService.setGain(IOType.STRIP, +stripId, gain) }; - } -} diff --git a/src/strip/strip.controller.ts b/src/strip/strip.controller.ts new file mode 100644 index 0000000..1b49a4d --- /dev/null +++ b/src/strip/strip.controller.ts @@ -0,0 +1,69 @@ +import { + Body, + Controller, + Get, + Header, + HostParam, + HttpException, + HttpStatus, + Param, + Patch, + Post, + Query, +} from '@nestjs/common'; +import { AppService, IOType } from '../app.service'; + +@Controller('/strip/:stripId') +export class StripController { + constructor(private readonly appService: AppService) {} + + @Get('/gain') + getGain(@Param('stripId') stripId: string) { + return { gain: this.appService.getGain(IOType.STRIP, +stripId) }; + } + @Patch('/gain') + setGain( + @Param('stripId') stripId: string, + @Body() { gain }: { gain: number }, + ) { + if (gain === undefined) + throw new HttpException( + '"gain" parameter not specified', + HttpStatus.BAD_REQUEST, + ); + if (typeof gain !== 'number' || isNaN(gain)) + throw new HttpException( + 'Invalid "gain" parameter type. Must be number', + HttpStatus.BAD_REQUEST, + ); + return { gain: this.appService.setGain(IOType.STRIP, +stripId, gain) }; + } + + @Get('/mute') + getMuteState(@Param('stripId') stripId: string) { + return { muteState: this.appService.getMuteState(IOType.STRIP, +stripId) }; + } + @Patch('/mute') + setMuteState( + @Param('stripId') stripId: string, + @Body() { muteState }: { muteState: boolean }, + ) { + if (muteState === undefined) + throw new HttpException( + '"muteState" parameter not specified', + HttpStatus.BAD_REQUEST, + ); + if (typeof muteState !== 'boolean') + throw new HttpException( + 'Invalid "muteState" parameter type. Must be boolean', + HttpStatus.BAD_REQUEST, + ); + return { + muteState: this.appService.setMuteState( + IOType.STRIP, + +stripId, + muteState, + ), + }; + } +}