Skip to content

Commit

Permalink
Merge pull request #172 from boostcampwm2023/BE-DeleteAPI-#118
Browse files Browse the repository at this point in the history
[BE/#118] DELETE API 구현 및 삭제 관련 로직 수정
  • Loading branch information
namewhat99 authored Nov 25, 2023
2 parents a3b190f + 2bffe80 commit 37cb0ac
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 58 deletions.
3 changes: 0 additions & 3 deletions BE/src/entities/blockPost.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export class BlockPostEntity {
@PrimaryColumn()
blocked_post: number;

@Column({ nullable: false, default: true })
status: boolean;

@DeleteDateColumn()
delete_date: Date;

Expand Down
3 changes: 0 additions & 3 deletions BE/src/entities/blockUser.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export class BlockUserEntity {
@PrimaryColumn()
blocked_user: string;

@Column({ nullable: false, default: true })
status: boolean;

@DeleteDateColumn()
delete_date: Date;

Expand Down
3 changes: 0 additions & 3 deletions BE/src/entities/post.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions BE/src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion BE/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions BE/src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
// 이미지 추가
Expand All @@ -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 });
}
}
12 changes: 8 additions & 4 deletions BE/src/posts-block/posts-block.controller.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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);
}
}
24 changes: 20 additions & 4 deletions BE/src/posts-block/posts-block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,30 @@ 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);
}

async findBlockedPosts(blockerId: string) {
const blockLists = await this.blockPostRepository.find({
where: {
blocker: blockerId,
status: true,
},
relations: ['blockedPost'],
});
Expand All @@ -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,
});
}
}
10 changes: 6 additions & 4 deletions BE/src/users-block/users-block.controller.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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);
}
}
37 changes: 21 additions & 16 deletions BE/src/users-block/users-block.service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -14,41 +13,38 @@ export class UsersBlockService {
private userRepository: Repository<UserEntity>,
) {}

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

async getBlockUser(id: string) {
const res = await this.blockUserRepository.find({
where: { blocker: id, status: true },
where: { blocker: id },
relations: ['blockedUser'],
});

Expand All @@ -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,
});
}
}
5 changes: 2 additions & 3 deletions BE/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 13 additions & 1 deletion BE/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
Loading

0 comments on commit 37cb0ac

Please sign in to comment.