Skip to content

Commit

Permalink
Add new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandros12345 committed Apr 20, 2024
1 parent 088a03c commit 4e1fe79
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 10 deletions.
25 changes: 25 additions & 0 deletions src/common/TryCatchInterceptor.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
return next.handle().pipe(
catchError((error) => {
// Логируйте ошибку или делайте что-то еще с ней здесь
console.error('Error occurred:', error);
// Возвращаем ошибку, если это необходимо
return throwError({
status: 'ERROR',
error: error.message,
});
}),
);
}
}
10 changes: 5 additions & 5 deletions src/models/semiFinishedSettings.entity.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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[];
Expand Down
25 changes: 23 additions & 2 deletions src/modules/bar/bar.controller.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await this.barService.addReport(data);
return await this.barService.addReport(data);
}

@Get('reports')
Expand Down
4 changes: 1 addition & 3 deletions src/modules/bar/bar.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { SemiFinishedDaily } from '../../models/semiFinishedDaily.entity';
}),
],
controllers: [BarController],
providers: [
BarService,
],
providers: [BarService],
})
export class BarModule {}
49 changes: 49 additions & 0 deletions src/modules/bar/bar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<void> {
// Логика добавления или обновления списка на сегодняшний день
// Здесь вы можете использовать методы TypeORM для сохранения данных в базу данных
Expand Down
26 changes: 26 additions & 0 deletions src/modules/bar/dto/settings.dto.ts
Original file line number Diff line number Diff line change
@@ -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[];
}

0 comments on commit 4e1fe79

Please sign in to comment.