Skip to content

Commit

Permalink
feat: #37 어드민 게시물 조회 API 추가 및 코드 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoiYongWon committed Dec 5, 2022
1 parent a29d102 commit b365977
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 56 deletions.
49 changes: 46 additions & 3 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -37,6 +41,7 @@ export class AdminController {
private adminService: AdminService,
private userService: UserService,
private friendsService: FriendsService,
private postingService: PostingService,
) {}

@Get('/all')
Expand All @@ -45,6 +50,8 @@ export class AdminController {
return await this.adminService.findAll();
}

/* User 제어 */

// 생성
@Post('/user')
@Roles([Role.Admin])
Expand Down Expand Up @@ -96,6 +103,8 @@ export class AdminController {
return await this.userService.delete(body.user_id);
}

/* Friends 제어 */

@Get('/friends/all')
@ApiCreatedResponse({
status: 200,
Expand All @@ -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')
Expand All @@ -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,
);
Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/admin/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -25,6 +26,7 @@ import { FriendsModule } from 'src/friends/friends.module';
TypeOrmModule.forFeature([User]),
UserModule,
FriendsModule,
PostingModule,
],
providers: [AdminService],
controllers: [AdminController],
Expand Down
6 changes: 6 additions & 0 deletions src/admin/dto/RequestDeletePost.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class RequestDeletePostDto {
@ApiProperty()
post_id: string;
}
18 changes: 18 additions & 0 deletions src/admin/dto/RequestUpdatePost.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions src/admin/dto/ResponseGetAllPost.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
68 changes: 35 additions & 33 deletions src/friends/friends.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@ export class FriendsService {
private userRepository: Repository<User>,
) {}

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({
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/posting/posting.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ import { FootprintModule } from 'src/footprint/footprint.module';
],
controllers: [PostingController],
providers: [PostingService],
exports: [PostingService],
})
export class PostingModule {}
46 changes: 46 additions & 0 deletions src/posting/posting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
}
}
42 changes: 22 additions & 20 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,9 @@ export class UserService {
private usersRepository: Repository<User>,
) {}

async findAll(): Promise<User[]> {
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<User> {
Expand Down Expand Up @@ -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<User[]> {
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();
}
}

0 comments on commit b365977

Please sign in to comment.