Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE/#458] 신고하기 API 구현 #459

Merged
merged 7 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LoginModule } from './login/login.module';
import { ChatModule } from './chat/chat.module';
import { CacheModule } from '@nestjs/cache-manager';
import { RedisConfigProvider } from './config/redis.config';
import { ReportModule } from './report/report.module';

@Module({
imports: [
Expand All @@ -39,6 +40,7 @@ import { RedisConfigProvider } from './config/redis.config';
UsersModule,
LoginModule,
ChatModule,
ReportModule,
],
controllers: [AppController],
providers: [
Expand Down
2 changes: 2 additions & 0 deletions BE/src/config/mysql.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BlockPostEntity } from '../entities/blockPost.entity';
import { ChatRoomEntity } from 'src/entities/chatRoom.entity';
import { RegistrationTokenEntity } from '../entities/registrationToken.entity';
import { ChatEntity } from 'src/entities/chat.entity';
import { ReportEntity } from '../entities/report.entity';

@Injectable()
export class MysqlConfigProvider implements TypeOrmOptionsFactory {
Expand All @@ -31,6 +32,7 @@ export class MysqlConfigProvider implements TypeOrmOptionsFactory {
ChatRoomEntity,
RegistrationTokenEntity,
ChatEntity,
ReportEntity,
],
synchronize: false,
};
Expand Down
4 changes: 4 additions & 0 deletions BE/src/entities/post.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UserEntity } from './user.entity';
import { PostImageEntity } from './postImage.entity';
import { BlockPostEntity } from './blockPost.entity';
import { ChatRoomEntity } from './chatRoom.entity';
import { ReportEntity } from './report.entity';

@Entity('post')
export class PostEntity {
Expand Down Expand Up @@ -67,4 +68,7 @@ export class PostEntity {

@OneToMany(() => BlockPostEntity, (post_image) => post_image.blocked_post)
blocked_posts: BlockPostEntity[];

@OneToMany(() => ReportEntity, (report) => report.post_id)
report: ReportEntity[];
}
39 changes: 39 additions & 0 deletions BE/src/entities/report.entity.ts
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;
}
8 changes: 7 additions & 1 deletion BE/src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
CreateDateColumn,
DeleteDateColumn,
Entity,
ManyToMany,
OneToMany,
OneToOne,
PrimaryGeneratedColumn,
Expand All @@ -15,6 +14,7 @@ import { BlockPostEntity } from './blockPost.entity';
import { RegistrationTokenEntity } from './registrationToken.entity';
import { ChatEntity } from './chat.entity';
import { ChatRoomEntity } from './chatRoom.entity';
import { ReportEntity } from './report.entity';

@Entity('user')
export class UserEntity {
Expand All @@ -30,6 +30,12 @@ export class UserEntity {
@OneToMany(() => BlockPostEntity, (blockUser) => blockUser.blocker)
blocker_post: BlockPostEntity[];

@OneToMany(() => ReportEntity, (report) => report.user_hash)
reported: ReportEntity[];

@OneToMany(() => ReportEntity, (report) => report.reporter)
reporting: ReportEntity[];

@OneToOne(
() => RegistrationTokenEntity,
(registrationToken) => registrationToken.user_hash,
Expand Down
12 changes: 12 additions & 0 deletions BE/src/report/createReport.dto.ts
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;
}
18 changes: 18 additions & 0 deletions BE/src/report/report.controller.ts
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);
}
}
14 changes: 14 additions & 0 deletions BE/src/report/report.module.ts
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 {}
43 changes: 43 additions & 0 deletions BE/src/report/report.service.ts
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);
}
Comment on lines +21 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

꼼꼼한 오류처리 좋습니다~

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);
}
}
Loading