Skip to content

Commit af45c33

Browse files
committed
Add admin DELETE comments/by-user
1 parent ae5caba commit af45c33

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/admin/admin.controller.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {Body, Controller, NotFoundException, Post, UseGuards} from '@nestjs/common';
1+
import {Body, Controller, Delete, NotFoundException, Post, UseGuards} from '@nestjs/common';
22
import {ApiKeyGuard} from "../common/apiKey.guard";
33
import {BlacklistDto} from "../dto/blacklist.dto";
44
import {AdminService} from "./admin.service";
55
import {AppService} from "../app.service";
66
import {UserService} from "../user/user.service";
7+
import {DeleteUserCommentsDto} from "../dto/comment.dto";
78

89
@Controller('admin')
910
export class AdminController {
@@ -34,4 +35,16 @@ export class AdminController {
3435

3536
throw new NotFoundException("tokenAddress or userAddress not specified");
3637
}
38+
39+
@UseGuards(ApiKeyGuard)
40+
@Delete('comments/by-user')
41+
async deleteUserComments(@Body() dto: DeleteUserCommentsDto) {
42+
const { userAddress } = dto
43+
44+
if(userAddress) {
45+
return await this.appService.deleteUserComments(userAddress)
46+
} else {
47+
throw new NotFoundException("userAddress is required");
48+
}
49+
}
3750
}

src/app.service.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Injectable, Logger} from '@nestjs/common';
2-
import {DataSource, EntityManager, LessThan, MoreThan} from "typeorm";
2+
import {DataSource, EntityManager, In, LessThan, MoreThan} from "typeorm";
33
import {
44
Comment,
55
CompetitionEntity,
@@ -44,6 +44,22 @@ export class AppService {
4444
})
4545
}
4646

47+
async deleteUserComments(userAddress: string){
48+
// TODO: refactor with single "delete" query
49+
const commentsToDelete = await this.dataSource.manager.find(Comment, {
50+
where: {
51+
user: {
52+
address: userAddress.toLowerCase()
53+
}
54+
}
55+
})
56+
const commentIds = commentsToDelete.map(item => item.id)
57+
const res = await this.dataSource.getRepository(Comment).delete({
58+
id: In(commentIds)
59+
})
60+
return res.affected
61+
}
62+
4763
async getTokens(dto: GetTokensDto){
4864
const {
4965
search,

src/dto/comment.dto.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ export class GetCommentsDto {
4343
@IsEnum(SortOrder, { message: 'Sort order must be ASC or DESC' })
4444
sortingOrder?: SortOrder;
4545
}
46+
47+
export class DeleteUserCommentsDto {
48+
@ApiProperty({ type: String, required: false })
49+
@Transform((address) => address.value.trim().toLowerCase())
50+
@Type(() => String)
51+
@IsString()
52+
userAddress: string;
53+
}

0 commit comments

Comments
 (0)