diff --git a/BE/src/entities/blockPost.entity.ts b/BE/src/entities/blockPost.entity.ts index d156c38..1382360 100644 --- a/BE/src/entities/blockPost.entity.ts +++ b/BE/src/entities/blockPost.entity.ts @@ -17,9 +17,6 @@ export class BlockPostEntity { @PrimaryColumn() blocked_post: number; - @Column({ nullable: false, default: true }) - status: boolean; - @DeleteDateColumn() delete_date: Date; diff --git a/BE/src/entities/blockUser.entity.ts b/BE/src/entities/blockUser.entity.ts index ea1ac0e..8456275 100644 --- a/BE/src/entities/blockUser.entity.ts +++ b/BE/src/entities/blockUser.entity.ts @@ -17,9 +17,6 @@ export class BlockUserEntity { @PrimaryColumn() blocked_user: string; - @Column({ nullable: false, default: true }) - status: boolean; - @DeleteDateColumn() delete_date: Date; diff --git a/BE/src/entities/post.entity.ts b/BE/src/entities/post.entity.ts index a935736..838dae5 100644 --- a/BE/src/entities/post.entity.ts +++ b/BE/src/entities/post.entity.ts @@ -34,9 +34,6 @@ export class PostEntity { @JoinColumn({ name: 'user_id' }) user: UserEntity; - @Column({ type: 'boolean', nullable: false }) - status: boolean; - @Column({ type: 'datetime', nullable: false }) start_date: Date; diff --git a/BE/src/entities/user.entity.ts b/BE/src/entities/user.entity.ts index b7a9ec2..4810200 100644 --- a/BE/src/entities/user.entity.ts +++ b/BE/src/entities/user.entity.ts @@ -49,9 +49,6 @@ export class UserEntity { }) update_date: Date; - @Column({ type: 'tinyint', nullable: false, default: true }) - status: boolean; - @Column({ length: 2048, nullable: true, charset: 'utf8' }) profile_img: string; diff --git a/BE/src/post/post.controller.ts b/BE/src/post/post.controller.ts index b8e3b20..0b3cd1b 100644 --- a/BE/src/post/post.controller.ts +++ b/BE/src/post/post.controller.ts @@ -83,7 +83,7 @@ export class PostController { @Delete('/:id') async postRemove(@Param('id') id: number) { - const isRemoved = await this.postService.deletePostById(id); + const isRemoved = await this.postService.removePost(id); if (isRemoved) { return HttpCode(200); diff --git a/BE/src/post/post.service.ts b/BE/src/post/post.service.ts index 85b6af6..0913f48 100644 --- a/BE/src/post/post.service.ts +++ b/BE/src/post/post.service.ts @@ -26,7 +26,7 @@ export class PostService { private s3Handler: S3Handler, ) {} makeWhereOption(query: PostListDto) { - const where = { status: true, is_request: undefined }; + const where = { is_request: undefined }; if (query.requestFilter !== undefined) { where.is_request = query.requestFilter !== 0; } @@ -106,9 +106,8 @@ export class PostService { relations: ['post_images', 'user'], }); - console.log(post); if (post === null) { - throw new HttpException('없는 게시물입니다.', 400); + throw new HttpException('없는 게시물입니다.', 404); } if (await this.isFiltered(post, userId)) { throw new HttpException('차단한 게시물입니다.', 400); @@ -207,7 +206,6 @@ export class PostService { post.is_request = createPostDto.is_request; post.start_date = createPostDto.start_date; post.end_date = createPostDto.end_date; - post.status = true; post.user_id = user.id; post.thumbnail = imageLocations.length > 0 ? imageLocations[0] : null; // 이미지 추가 @@ -226,16 +224,21 @@ export class PostService { } } - async deletePostById(postId: number) { + async removePost(postId: number) { const isDataExists = await this.postRepository.findOne({ - where: { id: postId, status: true }, + where: { id: postId }, }); if (!isDataExists) { return false; } else { - await this.postRepository.softDelete({ id: postId }); + await this.deleteCascadingPost(postId); return true; } } + async deleteCascadingPost(postId: number) { + await this.postImageRepository.softDelete({ post_id: postId }); + await this.blockPostRepository.softDelete({ blocked_post: postId }); + await this.postRepository.softDelete({ id: postId }); + } } diff --git a/BE/src/posts-block/posts-block.controller.ts b/BE/src/posts-block/posts-block.controller.ts index 4bb652d..1c6502e 100644 --- a/BE/src/posts-block/posts-block.controller.ts +++ b/BE/src/posts-block/posts-block.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Param, Post } from '@nestjs/common'; +import { Controller, Delete, Get, Param, Post } from '@nestjs/common'; import { PostsBlockService } from './posts-block.service'; @Controller('posts/block') @@ -13,8 +13,12 @@ export class PostsBlockController { @Get() async postsBlockList() { const blockerId: string = 'qwe'; - const blockedPosts = - await this.postsBlockService.findBlockedPosts(blockerId); - return blockedPosts; + return await this.postsBlockService.findBlockedPosts(blockerId); + } + + @Delete(':id') + async blockUserRemove(@Param('id') id: number) { + const userId = 'qwe'; + await this.postsBlockService.removeBlockPosts(id, userId); } } diff --git a/BE/src/posts-block/posts-block.service.ts b/BE/src/posts-block/posts-block.service.ts index 3b1924f..191eca7 100644 --- a/BE/src/posts-block/posts-block.service.ts +++ b/BE/src/posts-block/posts-block.service.ts @@ -19,19 +19,23 @@ export class PostsBlockService { if (!blockedPost) { throw new HttpException('없는 게시물입니다.', 404); } - const blockPostEntity = new BlockPostEntity(); + const isExist = await this.blockPostRepository.findOne({ where: { blocker: blockerId, blocked_post: postId, }, + withDeleted: true, }); - if (isExist !== null && isExist.status === true) { + + if (isExist !== null && isExist.delete_date === null) { throw new HttpException('이미 차단 되었습니다.', 400); } + + const blockPostEntity = new BlockPostEntity(); blockPostEntity.blocked_post = postId; blockPostEntity.blocker = blockerId; - blockPostEntity.status = true; + blockPostEntity.delete_date = null; await this.blockPostRepository.save(blockPostEntity); } @@ -39,7 +43,6 @@ export class PostsBlockService { const blockLists = await this.blockPostRepository.find({ where: { blocker: blockerId, - status: true, }, relations: ['blockedPost'], }); @@ -52,4 +55,17 @@ export class PostsBlockService { }; }); } + + async removeBlockPosts(blockedPostId: number, userId: string) { + const blockedPost = await this.blockPostRepository.findOne({ + where: { blocked_post: blockedPostId, blocker: userId }, + }); + if (!blockedPost) { + throw new HttpException('차단된 유저가 없습니다.', 404); + } + await this.blockPostRepository.softDelete({ + blocked_post: blockedPostId, + blocker: userId, + }); + } } diff --git a/BE/src/users-block/users-block.controller.ts b/BE/src/users-block/users-block.controller.ts index c63e6db..8a7cb26 100644 --- a/BE/src/users-block/users-block.controller.ts +++ b/BE/src/users-block/users-block.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Param, Delete } from '@nestjs/common'; +import { Controller, Get, Post, Param, Delete } from '@nestjs/common'; import { UsersBlockService } from './users-block.service'; @Controller('users/block') @@ -13,11 +13,13 @@ export class UsersBlockController { @Post('/:id') async blockUserAdd(@Param('id') id: string) { - await this.usersBlockService.addBlockUser(id); + const userId = 'qwe'; + await this.usersBlockService.addBlockUser(id, userId); } @Delete(':id') - remove(@Param('id') id: string) { - return this.usersBlockService.remove(+id); + async blockUserRemove(@Param('id') id: string) { + const userId = 'qwe'; + await this.usersBlockService.removeBlockUser(id, userId); } } diff --git a/BE/src/users-block/users-block.service.ts b/BE/src/users-block/users-block.service.ts index ee314ce..8be4559 100644 --- a/BE/src/users-block/users-block.service.ts +++ b/BE/src/users-block/users-block.service.ts @@ -1,8 +1,7 @@ -import { Injectable } from '@nestjs/common'; +import { HttpException, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { BlockUserEntity } from 'src/entities/blockUser.entity'; import { Repository } from 'typeorm'; -import { HttpException } from '@nestjs/common'; import { UserEntity } from 'src/entities/user.entity'; @Injectable() @@ -14,33 +13,30 @@ export class UsersBlockService { private userRepository: Repository, ) {} - async addBlockUser(id: string) { + async addBlockUser(id: string, userId: string) { const isExistUser = await this.userRepository.findOne({ where: { user_hash: id }, }); if (!isExistUser) { - throw new HttpException('존재하지 않는 유저입니다', 400); + throw new HttpException('존재하지 않는 유저입니다', 404); } const isBlockedUser = await this.blockUserRepository.findOne({ - where: { blocked_user: id, blocker: 'qwe', status: true }, + where: { blocked_user: id, blocker: userId }, + withDeleted: true, }); - console.log(isBlockedUser); - - if (isBlockedUser) { + if (isBlockedUser !== null && isBlockedUser.delete_date === null) { throw new HttpException('이미 차단된 유저입니다', 400); } - const blockUserEntity = new BlockUserEntity(); - blockUserEntity.blocker = 'qwe'; + blockUserEntity.blocker = userId; blockUserEntity.blocked_user = id; - blockUserEntity.status = true; + blockUserEntity.delete_date = null; try { - const res = await this.blockUserRepository.save(blockUserEntity); - return res; + return await this.blockUserRepository.save(blockUserEntity); } catch (e) { throw new HttpException('서버 오류입니다', 500); } @@ -48,7 +44,7 @@ export class UsersBlockService { async getBlockUser(id: string) { const res = await this.blockUserRepository.find({ - where: { blocker: id, status: true }, + where: { blocker: id }, relations: ['blockedUser'], }); @@ -66,7 +62,16 @@ export class UsersBlockService { return blockedUsers; } - remove(id: number) { - return `This action removes a #${id} usersBlock`; + async removeBlockUser(blockedUserId: string, userId: string) { + const blockedUser = await this.blockUserRepository.findOne({ + where: { blocked_user: blockedUserId, blocker: userId }, + }); + if (!blockedUser) { + throw new HttpException('차단된 유저가 없습니다.', 404); + } + await this.blockUserRepository.softDelete({ + blocked_user: blockedUserId, + blocker: userId, + }); } } diff --git a/BE/src/users/users.controller.ts b/BE/src/users/users.controller.ts index 70cd04f..578f93e 100644 --- a/BE/src/users/users.controller.ts +++ b/BE/src/users/users.controller.ts @@ -2,7 +2,6 @@ import { Controller, Get, Post, - Body, Patch, Param, Delete, @@ -50,8 +49,8 @@ export class UsersController { } @Delete(':id') - usersRemove(@Param('id') id: string) { - return this.usersService.removeUser(+id); + async usersRemove(@Param('id') id: string) { + await this.usersService.removeUser(id); } @Patch(':id') diff --git a/BE/src/users/users.module.ts b/BE/src/users/users.module.ts index 06d0fc5..df9dd70 100644 --- a/BE/src/users/users.module.ts +++ b/BE/src/users/users.module.ts @@ -4,9 +4,21 @@ import { UsersController } from './users.controller'; import { TypeOrmModule } from '@nestjs/typeorm'; import { S3Handler } from 'src/utils/S3Handler'; import { UserEntity } from '../entities/user.entity'; +import { PostEntity } from '../entities/post.entity'; +import { PostImageEntity } from '../entities/postImage.entity'; +import { BlockUserEntity } from '../entities/blockUser.entity'; +import { BlockPostEntity } from '../entities/blockPost.entity'; @Module({ - imports: [TypeOrmModule.forFeature([UserEntity])], + imports: [ + TypeOrmModule.forFeature([ + PostEntity, + UserEntity, + PostImageEntity, + BlockUserEntity, + BlockPostEntity, + ]), + ], controllers: [UsersController], providers: [UsersService, S3Handler], }) diff --git a/BE/src/users/users.service.ts b/BE/src/users/users.service.ts index 38de392..347b362 100644 --- a/BE/src/users/users.service.ts +++ b/BE/src/users/users.service.ts @@ -3,16 +3,28 @@ import { CreateUserDto } from './createUser.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { UserEntity } from 'src/entities/user.entity'; import { Repository } from 'typeorm'; -import { UpdatePostDto } from '../post/dto/postUpdate.dto'; import { UpdateUsersDto } from './usersUpdate.dto'; import { S3Handler } from '../utils/S3Handler'; import { hashMaker } from 'src/utils/hashMaker'; +import { PostEntity } from '../entities/post.entity'; +import { PostImageEntity } from '../entities/postImage.entity'; +import { BlockUserEntity } from '../entities/blockUser.entity'; +import { BlockPostEntity } from '../entities/blockPost.entity'; +import { UsersController } from './users.controller'; @Injectable() export class UsersService { constructor( + @InjectRepository(PostEntity) + private postRepository: Repository, @InjectRepository(UserEntity) private userRepository: Repository, + @InjectRepository(PostImageEntity) + private postImageRepository: Repository, + @InjectRepository(BlockUserEntity) + private blockUserRepository: Repository, + @InjectRepository(BlockPostEntity) + private blockPostRepository: Repository, private s3Handler: S3Handler, ) {} @@ -23,14 +35,13 @@ export class UsersService { userEntity.OAuth_domain = createUserDto.OAuth_domain; userEntity.profile_img = imageLocation; userEntity.user_hash = hashMaker(createUserDto.nickname).slice(0, 8); - userEntity.status = true; const res = await this.userRepository.save(userEntity); return res; } async findUserById(userId: string) { const user: UserEntity = await this.userRepository.findOne({ - where: { user_hash: userId, status: true }, + where: { user_hash: userId }, }); if (user) { return { nickname: user.nickname, profile_img: user.profile_img }; @@ -39,8 +50,34 @@ export class UsersService { } } - removeUser(id: number) { - return `This action removes a #${id} user`; + async removeUser(id: string) { + const isDataExists = await this.userRepository.findOne({ + where: { user_hash: id }, + }); + if (!isDataExists) { + return false; + } else { + await this.deleteCascadingUser(isDataExists); + return true; + } + } + + async deleteCascadingUser(user: UserEntity) { + const postsByUser = await this.postRepository.find({ + where: { user_id: user.id }, + }); + for (const postByUser of postsByUser) { + await this.deleteCascadingPost(postByUser.id); + } + await this.blockPostRepository.softDelete({ blocker: user.user_hash }); + await this.blockUserRepository.softDelete({ blocker: user.user_hash }); + await this.userRepository.softDelete({ id: user.id }); + } + + async deleteCascadingPost(postId: number) { + await this.postImageRepository.softDelete({ post_id: postId }); + await this.blockPostRepository.softDelete({ blocked_post: postId }); + await this.postRepository.softDelete({ id: postId }); } async updateUserById( diff --git a/BE/src/utils/hashMaker.ts b/BE/src/utils/hashMaker.ts index 3066497..cd596d8 100644 --- a/BE/src/utils/hashMaker.ts +++ b/BE/src/utils/hashMaker.ts @@ -4,5 +4,5 @@ export const hashMaker = (nickname: string) => { return crypto .createHash('sha256') .update(nickname + new Date().getTime()) - .digest('base64'); + .digest('base64url'); };