diff --git a/src/common/TryCatchInterceptor.ts b/src/common/TryCatchInterceptor.ts new file mode 100644 index 0000000..a1a28bf --- /dev/null +++ b/src/common/TryCatchInterceptor.ts @@ -0,0 +1,25 @@ +import { + Injectable, + NestInterceptor, + ExecutionContext, + CallHandler, +} from '@nestjs/common'; +import { Observable, throwError } from 'rxjs'; +import { catchError } from 'rxjs/operators'; + +@Injectable() +export class TryCatchInterceptor implements NestInterceptor { + intercept(context: ExecutionContext, next: CallHandler): Observable { + return next.handle().pipe( + catchError((error) => { + // Логируйте ошибку или делайте что-то еще с ней здесь + console.error('Error occurred:', error); + // Возвращаем ошибку, если это необходимо + return throwError({ + status: 'ERROR', + error: error.message, + }); + }), + ); + } +} diff --git a/src/models/semiFinishedSettings.entity.ts b/src/models/semiFinishedSettings.entity.ts index 7b882bf..1127b0f 100644 --- a/src/models/semiFinishedSettings.entity.ts +++ b/src/models/semiFinishedSettings.entity.ts @@ -1,4 +1,4 @@ -import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; +import {Entity, Column, PrimaryGeneratedColumn, BeforeInsert} from 'typeorm'; @Entity({ name: 'semi_finished_settings' }) export class SemiFinishedSettings { @@ -8,14 +8,14 @@ export class SemiFinishedSettings { @Column() title: string; - @Column() + @Column({ default: true }) required: boolean; - @Column() + @Column({ default: 'radio' }) type: string; - @Column() - name: string; + @Column({ default: null }) + name?: string; @Column({ type: 'json' }) answers: string[]; diff --git a/src/modules/bar/bar.controller.ts b/src/modules/bar/bar.controller.ts index 958ebc0..4dd9f84 100644 --- a/src/modules/bar/bar.controller.ts +++ b/src/modules/bar/bar.controller.ts @@ -1,14 +1,35 @@ -import { Controller, Get, Post, Body, Query, ValidationPipe } from '@nestjs/common'; +import {Controller, Get, Post, Body, Query, ValidationPipe, UseInterceptors} from '@nestjs/common'; import { BarService } from './bar.service'; import { AddReportDto } from './dto/addReport.dto'; +import { SettingDto } from './dto/settings.dto'; +import {TryCatchInterceptor} from "../../common/TryCatchInterceptor"; @Controller('bar') export class BarController { constructor(private readonly barService: BarService) {} + @Post('update_setting') + async updateSettings(@Body() data: SettingDto) { + return await this.barService.updateSetting(data); + } + + @Post('remove_setting') + async removeSettings(@Body('idToRemove') idToRemove: number) { + await this.barService.removeSetting(idToRemove); + + return { + status: 'OK', + }; + } + + @Post('new_setting') + async saveNewSettings(@Body() data: SettingDto) { + return await this.barService.addNewSetting(data); + } + @Post('daily_report') async addReport(@Body() data: AddReportDto): Promise { - await this.barService.addReport(data); + return await this.barService.addReport(data); } @Get('reports') diff --git a/src/modules/bar/bar.module.ts b/src/modules/bar/bar.module.ts index d5bad6a..1388b79 100644 --- a/src/modules/bar/bar.module.ts +++ b/src/modules/bar/bar.module.ts @@ -14,8 +14,6 @@ import { SemiFinishedDaily } from '../../models/semiFinishedDaily.entity'; }), ], controllers: [BarController], - providers: [ - BarService, - ], + providers: [BarService], }) export class BarModule {} diff --git a/src/modules/bar/bar.service.ts b/src/modules/bar/bar.service.ts index 0d6e98d..5d210e7 100644 --- a/src/modules/bar/bar.service.ts +++ b/src/modules/bar/bar.service.ts @@ -4,6 +4,8 @@ import { Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { SemiFinishedDaily } from '../../models/semiFinishedDaily.entity'; import { AddReportDto } from './dto/addReport.dto'; +import {SettingDto} from "./dto/settings.dto"; +import {IsArray, IsNotEmpty, IsString} from "class-validator"; @Injectable() export class BarService { @@ -22,6 +24,53 @@ export class BarService { return `${year}-${month}-${day}`; } + async addNewSetting(setting) { + const row = await this.semiFinishedSettingsRepository.save({ + title: setting.title, + required: true, + type: setting.type, + answers: setting.answers, + }); + + row.name = `g${row.id}`; + + return row; + } + + async updateSetting(setting: SettingDto) { + if (!setting.id) { + return { + status: 'ERROR', + error: 'param id is required', + }; + } + + const updated: any = {}; + + if (setting.title) { + updated.title = setting.title; + } + + if (setting.type) { + updated.type = setting.type; + } + + if (setting.answers) { + updated.answers = setting.answers; + } + + return this.semiFinishedSettingsRepository.update( + { + id: setting.id, + }, + updated, + ); + } + + async removeSetting(settingId: number) { + return this.semiFinishedSettingsRepository.delete(settingId); + } + async addReport(message: AddReportDto): Promise { // Логика добавления или обновления списка на сегодняшний день // Здесь вы можете использовать методы TypeORM для сохранения данных в базу данных diff --git a/src/modules/bar/dto/settings.dto.ts b/src/modules/bar/dto/settings.dto.ts new file mode 100644 index 0000000..8269962 --- /dev/null +++ b/src/modules/bar/dto/settings.dto.ts @@ -0,0 +1,26 @@ +import { IsString, IsNotEmpty, IsArray, ValidateNested } from 'class-validator'; +import { Type } from 'class-transformer'; + +export class SettingDto { + @IsNotEmpty() + id?: number; + + @IsNotEmpty() + @IsString() + title: string; + + @IsNotEmpty() + required: boolean; + + @IsNotEmpty() + @IsString() + type: string; + + @IsNotEmpty() + @IsString() + name?: string; + + @IsArray() + @IsNotEmpty() + answers: string[]; +}