Skip to content

Commit

Permalink
feat: #39 신고 수정+신고 취소 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
lywg committed Dec 6, 2022
1 parent f1697fa commit 7e3370f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions src/report/dto/RequestDeleteReport.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from "@nestjs/swagger";

export class RequestDeleteReportDto {
@ApiProperty()
report_id: string;
}
23 changes: 21 additions & 2 deletions src/report/report.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import { Body, Controller, Delete, Get, HttpException, HttpStatus, Post, Query, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { RolesGuard } from 'src/auth/guards/roles.guard';
import { Roles } from 'src/libs/decorators/roles.decorator';
import { Role } from 'src/libs/enums/role.enum';
import { ReportService } from './report.service';
import { RequestCreateReportDto } from './dto/RequestCreateReportDto';
import { RequestCreateReportDto } from './dto/RequestCreateReport.dto';
import { RequestDeleteReportDto } from './dto/RequestDeleteReport.dto';

@ApiTags('report')
@ApiBearerAuth()
Expand All @@ -25,4 +26,22 @@ export class ReportController {
body.post_id,
);
}

// 신고 취소
@Delete()
@Roles([Role.User])
async CancelReport(@Body() body: RequestDeleteReportDto, @Req() req) {
const user_id = req.user.user_id;
const post_id = req.post.post_id;
return await this.reportService
.deleteReport(user_id, post_id, body.report_id)
.catch((err) => {
throw new HttpException(
{
message: err.message,
},
HttpStatus.BAD_REQUEST,
);
});
}
}
35 changes: 21 additions & 14 deletions src/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ export class ReportService {
0: 심한 욕설 1: 혐오 발언 2: 도배
3: 선정적인 게시물 4: 도박성 게시물 5: 광고성 게시물 */

// 본인 게시글인지 확인
checkWriter(user, post) {
// 본인 게시글이면 false
if (user.user_id == post.user_id.user_id) {
return false;
}
else
return true;
}

// 신고
async addReport(report_type: number, user_id: string, post_id: string) {
const user = await this.userRepository.findOneBy({
Expand Down Expand Up @@ -60,11 +50,10 @@ export class ReportService {
});
}

// 본인 게시물 조회 미포함
if (this.checkWriter) {
return;
}
// 본인 게시물 신고 미포함
if (user.user_id == post.user_id.user_id) return;

// report_type이 달라도 user, post id가 존재하면 이미 신고됨
const isReported = await this.reportRepository
.createQueryBuilder('report')
.where('report.user_id = :user_id', { user_id })
Expand All @@ -83,4 +72,22 @@ export class ReportService {

return await this.reportRepository.save(rp);
}

// 신고 취소
async deleteReport(user_id: string, post_id: string, report_id: string) {
let query = await this.reportRepository
.createQueryBuilder('report')
.where('report.user_id = :user_id', { user_id })
.andWhere('report.post_id = :post_id', { post_id })
.getOne();

// 본인이 신고한 게시글이 아니면 잘못된 요청
if (query == null)
throw new BadRequestException({
status: HttpStatus.BAD_REQUEST,
message: '잘못된 요청입니다.',
});

return await this.reportRepository.delete({ report_id });
}
}

0 comments on commit 7e3370f

Please sign in to comment.