-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from boostcampwm2023/BE-Report-#458
[BE/#458] 신고하기 API 구현
- Loading branch information
Showing
9 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
Column, | ||
Entity, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
import { UserEntity } from './user.entity'; | ||
import { PostEntity } from './post.entity'; | ||
|
||
@Entity('report') | ||
export class ReportEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ nullable: false, charset: 'utf8' }) | ||
user_hash: string; | ||
|
||
@Column({ nullable: false }) | ||
post_id: number; | ||
|
||
@Column({ charset: 'utf8' }) | ||
description: string; | ||
|
||
@Column({ nullable: false, charset: 'utf8' }) | ||
reporter: string; | ||
|
||
@ManyToOne(() => UserEntity, (user) => user.user_hash) | ||
@JoinColumn({ name: 'user_hash', referencedColumnName: 'user_hash' }) | ||
reportedUser: UserEntity; | ||
|
||
@ManyToOne(() => PostEntity, (post) => post.id) | ||
@JoinColumn({ name: 'post_id', referencedColumnName: 'id' }) | ||
post: PostEntity; | ||
|
||
@ManyToOne(() => UserEntity, (user) => user.user_hash) | ||
@JoinColumn({ name: 'reporter', referencedColumnName: 'user_hash' }) | ||
reportingUser: UserEntity; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { IsNumber, IsString } from 'class-validator'; | ||
|
||
export class CreateReportDto { | ||
@IsString() | ||
description: string; | ||
|
||
@IsNumber() | ||
post_id: number; | ||
|
||
@IsString() | ||
user_id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Body, Controller, Post, UseGuards } from '@nestjs/common'; | ||
import { CreateReportDto } from './createReport.dto'; | ||
import { ReportService } from './report.service'; | ||
import { AuthGuard } from '../utils/auth.guard'; | ||
import { UserHash } from '../utils/auth.decorator'; | ||
|
||
@Controller('report') | ||
@UseGuards(AuthGuard) | ||
export class ReportController { | ||
constructor(private readonly reportService: ReportService) {} | ||
@Post() | ||
async createReport( | ||
@Body() body: CreateReportDto, | ||
@UserHash() userId: string, | ||
) { | ||
await this.reportService.createReport(body, userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ReportController } from './report.controller'; | ||
import { ReportService } from './report.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { ReportEntity } from '../entities/report.entity'; | ||
import { PostEntity } from '../entities/post.entity'; | ||
import { UserEntity } from '../entities/user.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([ReportEntity, PostEntity, UserEntity])], | ||
controllers: [ReportController], | ||
providers: [ReportService], | ||
}) | ||
export class ReportModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HttpException, Injectable } from '@nestjs/common'; | ||
import { CreateReportDto } from './createReport.dto'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { ReportEntity } from '../entities/report.entity'; | ||
import { PostEntity } from '../entities/post.entity'; | ||
import { UserEntity } from '../entities/user.entity'; | ||
|
||
@Injectable() | ||
export class ReportService { | ||
constructor( | ||
@InjectRepository(ReportEntity) | ||
private reportRepository: Repository<ReportEntity>, | ||
@InjectRepository(PostEntity) | ||
private postRepository: Repository<PostEntity>, | ||
@InjectRepository(UserEntity) | ||
private userRepository: Repository<UserEntity>, | ||
) {} | ||
async createReport(body: CreateReportDto, userId: string) { | ||
const isAllExist = await this.isExist(body.post_id, body.user_id); | ||
if (body.user_id === userId) { | ||
throw new HttpException('자신의 게시글은 신고 할 수 없습니다.', 400); | ||
} | ||
if (!isAllExist) { | ||
throw new HttpException('신고할 대상이 존재 하지 않습니다.', 404); | ||
} | ||
const reportEntity = new ReportEntity(); | ||
reportEntity.post_id = body.post_id; | ||
reportEntity.user_hash = body.user_id; | ||
reportEntity.description = body.description; | ||
reportEntity.reporter = userId; | ||
await this.reportRepository.save(reportEntity); | ||
} | ||
async isExist(postId, userId) { | ||
const isPostExist: boolean = await this.postRepository.exist({ | ||
where: { id: postId }, | ||
}); | ||
const isUserExist: boolean = await this.userRepository.exist({ | ||
where: { user_hash: userId }, | ||
}); | ||
return !!(isPostExist && isUserExist); | ||
} | ||
} |