From b365977816fe296f755f96ef3cae4ac0a03c7ea0 Mon Sep 17 00:00:00 2001 From: ChoiYongWon Date: Mon, 5 Dec 2022 17:17:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20#37=20=EC=96=B4=EB=93=9C=EB=AF=BC=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EB=AC=BC=20=EC=A1=B0=ED=9A=8C=20API=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/admin/admin.controller.ts | 49 ++++++++++++++++-- src/admin/admin.module.ts | 2 + src/admin/dto/RequestDeletePost.dto.ts | 6 +++ src/admin/dto/RequestUpdatePost.dto.ts | 18 +++++++ src/admin/dto/ResponseGetAllPost.dto.ts | 24 +++++++++ src/friends/friends.service.ts | 68 +++++++++++++------------ src/posting/posting.module.ts | 1 + src/posting/posting.service.ts | 46 +++++++++++++++++ src/user/user.service.ts | 42 +++++++-------- 9 files changed, 200 insertions(+), 56 deletions(-) create mode 100644 src/admin/dto/RequestDeletePost.dto.ts create mode 100644 src/admin/dto/RequestUpdatePost.dto.ts create mode 100644 src/admin/dto/ResponseGetAllPost.dto.ts diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 558f2ef..4408193 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -26,6 +26,10 @@ import { RequestUpdateFriendRelationDto } from './dto/RequestUpdateFriendRelatio import { RequestDeleteUserDto } from './dto/ReqeustDeleteUser.dto'; import { ResponseGetAllFriendByAdminDto } from './dto/ResponseGetAllFriendByAdmin.dto'; import { ResponseReadAllUserDto } from './dto/ResponseReadAllUser.dto'; +import { PostingService } from 'src/posting/posting.service'; +import { RequestUpdatePostDto } from './dto/RequestUpdatePost.dto'; +import { RequestDeletePostDto } from './dto/RequestDeletePost.dto'; +import { ResponseGetAllPostDto } from './dto/ResponseGetAllPost.dto'; @ApiTags('admin') @ApiBearerAuth() @@ -37,6 +41,7 @@ export class AdminController { private adminService: AdminService, private userService: UserService, private friendsService: FriendsService, + private postingService: PostingService, ) {} @Get('/all') @@ -45,6 +50,8 @@ export class AdminController { return await this.adminService.findAll(); } + /* User 제어 */ + // 생성 @Post('/user') @Roles([Role.Admin]) @@ -96,6 +103,8 @@ export class AdminController { return await this.userService.delete(body.user_id); } + /* Friends 제어 */ + @Get('/friends/all') @ApiCreatedResponse({ status: 200, @@ -105,7 +114,7 @@ export class AdminController { }) @Roles([Role.Admin]) async getAllFriendList() { - return await this.friendsService.getAllFriendList(); + return await this.friendsService.getAllFriend_Admin(); } @Patch('/friends') @@ -115,7 +124,7 @@ export class AdminController { }) @Roles([Role.Admin]) async updateFriendRelation(@Body() body: RequestUpdateFriendRelationDto) { - return await this.friendsService.updateRelation( + return await this.friendsService.updateRelation_Admin( body.friend_id, body.relation_type, ); @@ -128,6 +137,40 @@ export class AdminController { }) @Roles([Role.Admin]) async deleteFriendRelation(@Body() body: RequestUpdateFriendRelationDto) { - return await this.friendsService.deleteFriendByFriendId(body.friend_id); + return await this.friendsService.deleteFriend_Admin(body.friend_id); + } + + /* Posting 제어 */ + + @Get('/post/all') + @ApiCreatedResponse({ + status: 200, + type: ResponseGetAllPostDto, + description: '모든 우연 목록을 조회합니다.', + isArray: true, + }) + @Roles([Role.Admin]) + async getAllPostList() { + return await this.postingService.getAllPost_Admin(); + } + + @Patch('/post') + @ApiCreatedResponse({ + status: 200, + description: '특정 우연을 수정합니다.', + }) + @Roles([Role.Admin]) + async updatePost(@Body() body: RequestUpdatePostDto) { + return await this.postingService.updatePost_Admin(body); + } + + @Delete('/post') + @ApiCreatedResponse({ + status: 200, + description: '특정 우연을 삭제합니다.', + }) + @Roles([Role.Admin]) + async deletePost(@Body() body: RequestDeletePostDto) { + return await this.postingService.deletePost_Admin(body.post_id); } } diff --git a/src/admin/admin.module.ts b/src/admin/admin.module.ts index ece877b..eebd408 100644 --- a/src/admin/admin.module.ts +++ b/src/admin/admin.module.ts @@ -9,6 +9,7 @@ import { AuthModule } from 'src/auth/auth.module'; import { JwtModule } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; import { FriendsModule } from 'src/friends/friends.module'; +import { PostingModule } from 'src/posting/posting.module'; @Module({ imports: [ @@ -25,6 +26,7 @@ import { FriendsModule } from 'src/friends/friends.module'; TypeOrmModule.forFeature([User]), UserModule, FriendsModule, + PostingModule, ], providers: [AdminService], controllers: [AdminController], diff --git a/src/admin/dto/RequestDeletePost.dto.ts b/src/admin/dto/RequestDeletePost.dto.ts new file mode 100644 index 0000000..4957e2e --- /dev/null +++ b/src/admin/dto/RequestDeletePost.dto.ts @@ -0,0 +1,6 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class RequestDeletePostDto { + @ApiProperty() + post_id: string; +} diff --git a/src/admin/dto/RequestUpdatePost.dto.ts b/src/admin/dto/RequestUpdatePost.dto.ts new file mode 100644 index 0000000..165dbba --- /dev/null +++ b/src/admin/dto/RequestUpdatePost.dto.ts @@ -0,0 +1,18 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class RequestUpdatePostDto { + @ApiProperty() + post_id: string; + + @ApiProperty() + content: string; + + @ApiProperty() + forFriend: number; + + @ApiProperty() + latitude: number; + + @ApiProperty() + longitude: number; +} diff --git a/src/admin/dto/ResponseGetAllPost.dto.ts b/src/admin/dto/ResponseGetAllPost.dto.ts new file mode 100644 index 0000000..b262687 --- /dev/null +++ b/src/admin/dto/ResponseGetAllPost.dto.ts @@ -0,0 +1,24 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class ResponseGetAllPostDto { + @ApiProperty() + post_id: string; + + @ApiProperty() + content: string; + + @ApiProperty() + forFriend: number; + + @ApiProperty() + latitude: number; + + @ApiProperty() + longitude: number; + + @ApiProperty() + user_id: string; + + @ApiProperty() + name: string; +} diff --git a/src/friends/friends.service.ts b/src/friends/friends.service.ts index 545a2e5..c4ded18 100644 --- a/src/friends/friends.service.ts +++ b/src/friends/friends.service.ts @@ -18,25 +18,6 @@ export class FriendsService { private userRepository: Repository, ) {} - async getAllFriendList() { - return await this.friendsRepository.find({ - relations: { - follower: true, - following: true, - }, - select: { - follower: { - name: true, - }, - following: { - name: true, - }, - friend_id: true, - relation_type: true, - }, - }); - } - //친구 신청 async createRelation(follower_uuid: string, following_uuid: string) { const follower = await this.userRepository.findOneBy({ @@ -253,13 +234,46 @@ export class FriendsService { }); } - async deleteFriendByFriendId(friend_id: string) { + async isFriend(follower_uuid: string, following_uuid: string) { + const isFriend = await this.friendsRepository + .createQueryBuilder('friend') + .where('friend.follower = :follower_uuid', { follower_uuid }) + .orWhere('friend.follower = :following_uuid', { following_uuid }) + .andWhere('friend.following = :following_uuid', { following_uuid }) + .orWhere('friend.following = :follower_uuid', { follower_uuid }) + .andWhere('friend.relation_type = 1') + .getOne(); + return isFriend != null; + } + + /* 어드민 전용 API */ + + async getAllFriend_Admin() { + return await this.friendsRepository.find({ + relations: { + follower: true, + following: true, + }, + select: { + follower: { + name: true, + }, + following: { + name: true, + }, + friend_id: true, + relation_type: true, + }, + }); + } + + async deleteFriend_Admin(friend_id: string) { return await this.friendsRepository.delete({ friend_id, }); } - async updateRelation(friend_id: string, relation_type: number) { + async updateRelation_Admin(friend_id: string, relation_type: number) { const relation = await this.friendsRepository.findOne({ where: { friend_id, @@ -270,16 +284,4 @@ export class FriendsService { return await this.friendsRepository.save(relation); } - - async isFriend(follower_uuid: string, following_uuid: string) { - const isFriend = await this.friendsRepository - .createQueryBuilder('friend') - .where('friend.follower = :follower_uuid', { follower_uuid }) - .orWhere('friend.follower = :following_uuid', { following_uuid }) - .andWhere('friend.following = :following_uuid', { following_uuid }) - .orWhere('friend.following = :follower_uuid', { follower_uuid }) - .andWhere('friend.relation_type = 1') - .getOne(); - return isFriend != null; - } } diff --git a/src/posting/posting.module.ts b/src/posting/posting.module.ts index 610f91e..8974aa1 100644 --- a/src/posting/posting.module.ts +++ b/src/posting/posting.module.ts @@ -17,5 +17,6 @@ import { FootprintModule } from 'src/footprint/footprint.module'; ], controllers: [PostingController], providers: [PostingService], + exports: [PostingService], }) export class PostingModule {} diff --git a/src/posting/posting.service.ts b/src/posting/posting.service.ts index 1c926f1..c927f02 100644 --- a/src/posting/posting.service.ts +++ b/src/posting/posting.service.ts @@ -12,6 +12,7 @@ import { S3Service } from 'src/s3/s3.service'; import { UserService } from 'src/user/user.service'; import { Image } from 'src/image/image.entity'; import { FootprintService } from 'src/footprint/footprint.service'; +import { RequestUpdatePostDto } from 'src/admin/dto/RequestUpdatePost.dto'; @Injectable() export class PostingService { private readonly awsS3: AWS.S3; @@ -155,4 +156,49 @@ forFriend = 0 인 게시물 중에 return await this.postingRepository.delete({ post_id }); } + + /* Admin 전용 API */ + + async getAllPost_Admin() { + let posts: any = await this.postingRepository.find({ + relations: { + user_id: true, + }, + select: { + post_id: true, + user_id: { + user_id: true, + name: true, + }, + content: true, + latitude: true, + longitude: true, + forFriend: true, + }, + }); + + posts = posts.map((post) => ({ + ...post, + user_id: post.user_id.user_id, + name: post.user_id.name, + })); + + return posts; + } + + async updatePost_Admin(post: RequestUpdatePostDto) { + let posts = await this.postingRepository.find({ + where: { + post_id: post.post_id, + }, + }); + + const updated_post = { ...posts, ...post }; + + return await this.postingRepository.save(updated_post); + } + + async deletePost_Admin(post_id: string) { + return await this.postingRepository.delete({ post_id }); + } } diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 27e146e..f5da762 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -12,23 +12,9 @@ export class UserService { private usersRepository: Repository, ) {} - async findAll(): Promise { - return await this.usersRepository - .createQueryBuilder('user') - // .andWhere('following.relation_type = 1') - .loadRelationCountAndMap( - 'user.follower_count', - 'user.follower', - 'follower', - (qb) => qb.where('follower.relation_type = 1'), - ) - .loadRelationCountAndMap( - 'user.following_count', - 'user.following', - 'following', - (qb) => qb.where('following.relation_type = 1'), - ) - .getMany(); + async create(userData: RequestCreateUserDto) { + const user = this.usersRepository.create(userData); + return await this.usersRepository.insert(user); } async findOne(user_id: string): Promise { @@ -65,8 +51,24 @@ export class UserService { await this.usersRepository.delete(userID); } - async create(userData: RequestCreateUserDto) { - const user = this.usersRepository.create(userData); - return await this.usersRepository.insert(user); + /* Admin 전용 API */ + + async findAll(): Promise { + return await this.usersRepository + .createQueryBuilder('user') + // .andWhere('following.relation_type = 1') + .loadRelationCountAndMap( + 'user.follower_count', + 'user.follower', + 'follower', + (qb) => qb.where('follower.relation_type = 1'), + ) + .loadRelationCountAndMap( + 'user.following_count', + 'user.following', + 'following', + (qb) => qb.where('following.relation_type = 1'), + ) + .getMany(); } }