Skip to content

Commit

Permalink
Merge pull request #33 from wooyeonhan-inyeons/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ChoiYongWon authored Nov 27, 2022
2 parents 97c2a1e + 519879f commit 7cad263
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 20 deletions.
58 changes: 50 additions & 8 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import { RequestCreateUserDto } from '../user/dto/RequestCreateUser.dto';
import { RequestUpdateUserDto } from 'src/user/dto/RequestUpdateUser.dto';
import { ResponseReadUserDto } from './dto/ResponseReadUser.dto';
import { ResponseGetUserATDto } from './dto/ResponseGetUserAT.dto';
import { FriendsService } from 'src/friends/friends.service';
import { RequestUpdateFriendRelationDto } from './dto/RequestUpdateFriendRelation.dto';
import { RequestDeleteUserDto } from './dto/ReqeustDeleteUser.dto';
import { ResponseGetAllFriendByAdminDto } from './dto/ResponseGetAllFriendByAdmin.dto';
import { ResponseReadAllUserDto } from './dto/ResponseReadAllUser.dto';

@ApiTags('admin')
@ApiBearerAuth()
Expand All @@ -31,6 +36,7 @@ export class AdminController {
constructor(
private adminService: AdminService,
private userService: UserService,
private friendsService: FriendsService,
) {}

@Get('/all')
Expand All @@ -48,7 +54,11 @@ export class AdminController {

// 조회
@Get('/user/all')
@ApiCreatedResponse({ status: 200, type: ResponseReadUserDto, isArray: true })
@ApiCreatedResponse({
status: 200,
type: ResponseReadAllUserDto,
isArray: true,
})
@Roles([Role.Admin])
async getUserAll(): Promise<User[]> {
return await this.userService.findAll();
Expand All @@ -75,17 +85,49 @@ export class AdminController {
// 수정
@Patch('/user')
@Roles([Role.Admin])
async patchUser(
@Query('user_id') userID: string,
@Body() updateData: RequestUpdateUserDto,
) {
return await this.userService.update(userID, updateData);
async patchUser(@Body() body: RequestUpdateUserDto) {
return await this.userService.update(body.user_id, body);
}

// 삭제
@Delete('/user')
@Roles([Role.Admin])
async removeUser(@Query('user_id') userID: string) {
return await this.userService.delete(userID);
async removeUser(@Body() body: RequestDeleteUserDto) {
return await this.userService.delete(body.user_id);
}

@Get('/friends/all')
@ApiCreatedResponse({
status: 200,
type: ResponseGetAllFriendByAdminDto,
description: '모든 친구 요청 목록을 조회합니다.',
isArray: true,
})
@Roles([Role.Admin])
async getAllFriendList() {
return await this.friendsService.getAllFriendList();
}

@Patch('/friends')
@ApiCreatedResponse({
status: 200,
description: '친구 목록에서 관계를 수정합니다.',
})
@Roles([Role.Admin])
async updateFriendRelation(@Body() body: RequestUpdateFriendRelationDto) {
return await this.friendsService.updateRelation(
body.friend_id,
body.relation_type,
);
}

@Delete('/friends')
@ApiCreatedResponse({
status: 200,
description: '특정 친구 관계를 삭제합니다.',
})
@Roles([Role.Admin])
async deleteFriendRelation(@Body() body: RequestUpdateFriendRelationDto) {
return await this.friendsService.deleteFriendByFriendId(body.friend_id);
}
}
2 changes: 2 additions & 0 deletions src/admin/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { User } from 'src/user/user.entity';
import { AuthModule } from 'src/auth/auth.module';
import { JwtModule } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { FriendsModule } from 'src/friends/friends.module';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { ConfigService } from '@nestjs/config';
TypeOrmModule.forFeature([Admin]),
TypeOrmModule.forFeature([User]),
UserModule,
FriendsModule,
],
providers: [AdminService],
controllers: [AdminController],
Expand Down
6 changes: 6 additions & 0 deletions src/admin/dto/ReqeustDeleteFriend.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class RequestDeleteFriendDto {
@ApiProperty()
friend_id: string;
}
6 changes: 6 additions & 0 deletions src/admin/dto/ReqeustDeleteUser.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class RequestDeleteUserDto {
@ApiProperty()
user_id: string;
}
9 changes: 9 additions & 0 deletions src/admin/dto/RequestUpdateFriendRelation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';

export class RequestUpdateFriendRelationDto {
@ApiProperty()
friend_id: string;

@ApiProperty()
relation_type: number;
}
Empty file.
21 changes: 21 additions & 0 deletions src/admin/dto/ResponseGetAllFriendByAdmin.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ApiProperty } from '@nestjs/swagger';

class UserInfoDto {
@ApiProperty()
name: string;
}

export class ResponseGetAllFriendByAdminDto {
@ApiProperty({
type: UserInfoDto,
})
follower: UserInfoDto;

@ApiProperty({
type: UserInfoDto,
})
following: UserInfoDto;

@ApiProperty()
relation_type: number;
}
6 changes: 6 additions & 0 deletions src/admin/dto/ResponseReadAllUser.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export class ResponseReadAllUserDto {
@ApiProperty()
message: string;

@ApiProperty()
follower_count: number;

@ApiProperty()
following_count: number;

@ApiProperty()
created_at: Date;
}
6 changes: 6 additions & 0 deletions src/admin/dto/ResponseReadUser.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export class ResponseReadUserDto {
@ApiProperty()
message: string;

@ApiProperty()
follower_count: number;

@ApiProperty()
following_count: number;

@ApiProperty()
created_at: Date;
}
File renamed without changes.
2 changes: 2 additions & 0 deletions src/friends/dto/ResponseGetAllFriend.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class FollowingArrayDto {
export class ResponseGetAllFriendDto {
@ApiProperty({
type: FollowerArrayDto,
isArray: true,
})
follower: FollowerArrayDto;

@ApiProperty({
type: FollowingArrayDto,
isArray: true,
})
following: FollowingArrayDto;
}
3 changes: 1 addition & 2 deletions src/friends/friends.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { RolesGuard } from 'src/auth/guards/roles.guard';
import { Roles } from 'src/libs/decorators/roles.decorator';
import { Role } from 'src/libs/enums/role.enum';
import { RequestUpdateUserDto } from 'src/user/dto/RequestUpdateUser.dto';
import { RequestDeclineFriendDto } from './dto/RequeestDeclineFriend.dto';
import { RequestDeclineFriendDto } from './dto/RequestDeclineFriend.dto';
import { RequestAcceptFriendDto } from './dto/RequestAcceptFriend.dto';
import { RequestCreateFriendDto } from './dto/RequestCreateFriend.dto';
import { RequestDeleteFriendDto } from './dto/RequestDeleteFriend.dto';
Expand Down
11 changes: 6 additions & 5 deletions src/friends/friends.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { Friends } from './friends.entity';
import { User } from 'src/user/user.entity';

@Module({
imports: [TypeOrmModule.forFeature([Friends]), TypeOrmModule.forFeature([User])],
imports: [
TypeOrmModule.forFeature([Friends]),
TypeOrmModule.forFeature([User]),
],
controllers: [FriendsController],
providers: [FriendsService]
providers: [FriendsService],
exports: [FriendsService],
})
export class FriendsModule {}



39 changes: 38 additions & 1 deletion src/friends/friends.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ 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 @@ -222,7 +241,7 @@ export class FriendsService {
friend_id,
});

//실제 존재하는 관계인지 조회
//요청한 유저가 포함된 관계인지 조회
if (!isFollower && !isFollowing)
throw new BadRequestException({
status: HttpStatus.BAD_REQUEST,
Expand All @@ -233,4 +252,22 @@ export class FriendsService {
friend_id,
});
}

async deleteFriendByFriendId(friend_id: string) {
return await this.friendsRepository.delete({
friend_id,
});
}

async updateRelation(friend_id: string, relation_type: number) {
const relation = await this.friendsRepository.findOne({
where: {
friend_id,
},
});

relation.relation_type = relation_type;

return await this.friendsRepository.save(relation);
}
}
6 changes: 3 additions & 3 deletions src/user/dto/RequestUpdateUser.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { ApiProperty } from '@nestjs/swagger';
export class RequestUpdateUserDto {
@ApiProperty()
@IsString()
name: string;
user_id: string;

@ApiProperty()
@IsEmail()
email: string;
@IsString()
name: string;

@ApiProperty()
@IsString()
Expand Down
17 changes: 16 additions & 1 deletion src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@ export class UserService {
) {}

async findAll(): Promise<User[]> {
return await this.usersRepository.find();
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 findOne(user_id: string): Promise<User> {
Expand Down

0 comments on commit 7cad263

Please sign in to comment.