Добавил роуты для редактирования и удаления

This commit is contained in:
Dhaverd 2026-05-02 14:59:11 +08:00
parent cf2a49d92f
commit cf0e157ad4
4 changed files with 70 additions and 10 deletions

View File

@ -1,4 +1,14 @@
import { BadRequestException, Body, Controller, Get, Post, Query } from '@nestjs/common'; import {
BadRequestException,
Body,
Controller,
Get,
Param,
Post,
Delete,
Put,
Query,
} from '@nestjs/common';
import { BlacklistService } from './blacklist.service'; import { BlacklistService } from './blacklist.service';
import { BlacklistItemList } from './types/blacklistItemList'; import { BlacklistItemList } from './types/blacklistItemList';
import { BlacklistReport } from './models/blacklistReport.model'; import { BlacklistReport } from './models/blacklistReport.model';
@ -6,8 +16,7 @@ import * as blacklistReport from './dto/blacklistReport';
@Controller('api/v1/blacklist') @Controller('api/v1/blacklist')
export class BlacklistController { export class BlacklistController {
constructor(private readonly blacklistService: BlacklistService) { constructor(private readonly blacklistService: BlacklistService) {}
}
@Get('/health_check') @Get('/health_check')
getHello(): string { getHello(): string {
@ -41,4 +50,19 @@ export class BlacklistController {
blacklistReportDto, blacklistReportDto,
); );
} }
@Put('/edit')
async updateReport(
@Body()
blacklistReportDto: blacklistReport.BlacklistReportDto,
): Promise<BlacklistReport> {
return await this.blacklistService.updateBlacklistRecord(
blacklistReportDto,
);
}
@Delete('/delete/:steamLink')
async deleteReport(@Param('steamLink') steamLink: string): Promise<boolean> {
return await this.blacklistService.deleteBlacklistRecord(steamLink);
}
} }

View File

@ -4,6 +4,7 @@ import { BlacklistReport } from './models/blacklistReport.model';
import { BlacklistItem } from './types/blacklistItem'; import { BlacklistItem } from './types/blacklistItem';
import { BlacklistItemList } from './types/blacklistItemList'; import { BlacklistItemList } from './types/blacklistItemList';
import { BlacklistReportDto } from './dto/blacklistReport'; import { BlacklistReportDto } from './dto/blacklistReport';
import { BadRequestException } from '@nestjs/common';
@Injectable() @Injectable()
export class BlacklistService { export class BlacklistService {
@ -65,4 +66,38 @@ export class BlacklistService {
const report = BlacklistReport.build(blackListReportDto); const report = BlacklistReport.build(blackListReportDto);
return await report.save(); return await report.save();
} }
async updateBlacklistRecord(
blackListReportDto: BlacklistReportDto,
): Promise<BlacklistReport> {
const record = await this.blacklistReportModel.findOne({
where: { id: blackListReportDto.id },
raw: false,
});
if (!record) {
throw new BadRequestException('Record not found', {
cause: new Error(),
description: `Report with id ${blackListReportDto.id} does not exist in the blacklist`,
});
}
// Update fields with validation to ensure types are correct
record.steam_link = blackListReportDto.steam_link;
record.comment = blackListReportDto.comment;
record.toxic = blackListReportDto.toxic;
record.cheater = blackListReportDto.cheater;
record.afk = blackListReportDto.afk;
record.useless = blackListReportDto.useless;
record.griefer = blackListReportDto.griefer;
return await record.save();
}
async deleteBlacklistRecord(steamLink: string): Promise<boolean> {
const deletedCount = await this.blacklistReportModel.destroy({
where: { steam_link: steamLink },
});
return deletedCount > 0;
}
} }

View File

@ -1,4 +1,5 @@
export type BlacklistReportDto = { export type BlacklistReportDto = {
id: number;
steam_link: string; steam_link: string;
comment: string; comment: string;
toxic: boolean; toxic: boolean;

View File

@ -3,23 +3,23 @@ import { Column, Model, Table } from 'sequelize-typescript';
@Table @Table
export class BlacklistReport extends Model { export class BlacklistReport extends Model {
@Column @Column
steam_link: string; declare steam_link: string;
@Column @Column
comment: string; declare comment: string;
@Column({ defaultValue: false }) @Column({ defaultValue: false })
toxic: boolean; declare toxic: boolean;
@Column({ defaultValue: false }) @Column({ defaultValue: false })
cheater: boolean; declare cheater: boolean;
@Column({ defaultValue: false }) @Column({ defaultValue: false })
afk: boolean; declare afk: boolean;
@Column({ defaultValue: false }) @Column({ defaultValue: false })
useless: boolean; declare useless: boolean;
@Column({ defaultValue: false }) @Column({ defaultValue: false })
griefer: boolean; declare griefer: boolean;
} }