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 c315b34 commit 1cca605
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/report/dto/RequestCreateReportDto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNumber } from "class-validator";
import { IsNumber, IsString } from "class-validator";

export class RequestCreateReportDto {
@ApiProperty()
@IsNumber()
report_type: number;


@ApiProperty()
@IsString()
post_id: string;
}
23 changes: 21 additions & 2 deletions src/report/report.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { Controller } from '@nestjs/common';
import { Body, Controller, Get, Post, 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';

@ApiTags('report')
@ApiBearerAuth()
@UseGuards(RolesGuard)
@UseGuards(JwtAuthGuard)
@Controller('report')
export class ReportController {
constructor(private reportService: ReportService) { }


// 신고
@Post()
@Roles([Role.User])
async Report(@Req() req, @Body() body: RequestCreateReportDto) {
return await this.reportService.addReport(
body.report_type,
req.user.user_id,
body.post_id,
);
}
}
65 changes: 63 additions & 2 deletions src/report/report.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Posting } from 'src/posting/posting.entity';
import { User } from 'src/user/user.entity';
import { Repository, ReturningStatementNotSupportedError } from 'typeorm';
import { Report } from './report.entity';

@Injectable()
Expand All @@ -9,6 +11,65 @@ export class ReportService {
@InjectRepository(Report)
private reportRepository: Repository<Report>,

@InjectRepository(User)
private userRepository: Repository<User>,

@InjectRepository(Posting)
private postingRepository: Repository<Posting>,
) { }

// 본인 게시글인지 확인
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({
user_id,
});
const post = await this.postingRepository.findOne({
relations: {
user_id: true,
},
where: {
post_id,
},
});

// 유저랑 게시글이 존재하지 않을 때
if (user == null) {
throw new BadRequestException({
status: HttpStatus.BAD_REQUEST,
message: '유저가 존재하지 않습니다.',
});
}
if (post == null) {
throw new BadRequestException({
status: HttpStatus.BAD_REQUEST,
message: '게시글이 존재하지 않습니다.',
});
}

// 본인 게시물 조회 미포함
if (this.checkWriter) {
return;
}

const isReported = await this.reportRepository
.createQueryBuilder('report')
.where('report.user_id = :user_id', { user_id })
.andWhere('report.post_id = :post_id', { post_id })
.getOne();

// 이미 신고했으면 미포함
if (isReported != null) return;


}
}

0 comments on commit 1cca605

Please sign in to comment.