Skip to content

Commit

Permalink
Merge pull request #49 from DevKor-github/feature/club
Browse files Browse the repository at this point in the history
Feature/club
  • Loading branch information
JeongYeonSeung authored Jul 23, 2024
2 parents a2efd84 + 7388ac6 commit d670214
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { KuVerificationRepository } from './ku-verification.repository';
import { AdminStrategy } from './strategies/admin.strategy';
import { UserModule } from 'src/user/user.module';
import { CommonModule } from 'src/common/common.module';
import { OptionalJwtAuthGuard } from './guards/optional-jwt-auth.guard';

@Module({
imports: [
Expand All @@ -39,6 +40,7 @@ import { CommonModule } from 'src/common/common.module';
RefreshStrategy,
VerifyStrategy,
AdminStrategy,
OptionalJwtAuthGuard,
],
})
export class AuthModule {}
15 changes: 15 additions & 0 deletions src/auth/guards/optional-jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class OptionalJwtAuthGuard extends AuthGuard('jwt') {
handleRequest<TUser = any>(err: any, user: any): TUser {
// user가 존재한다면 그대로, 비로그인 or 미인증 유저의 경우 null 반환
return user || null;
}
// jwt 인증 실패해도 guard 통과 시킴
async canActivate(context: ExecutionContext) {
await super.canActivate(context);
return true;
}
}
15 changes: 8 additions & 7 deletions src/home/club/club.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import { GetClubResponseDto } from './dto/get-club-response.dto';
import { ClubSearchQueryDto } from './dto/club-search-query.dto';
import { GetHotClubResponseDto } from './dto/get-hot-club-response.dto';
import { GetRecommendClubResponseDto } from './dto/get-recommend-club-response.dto';
import { OptionalJwtAuthGuard } from 'src/auth/guards/optional-jwt-auth.guard';

@Controller('club')
@ApiTags('club')
@ApiBearerAuth('accessToken')
@UseGuards(JwtAuthGuard)
export class ClubController {
constructor(private readonly clubService: ClubService) {}

@UseGuards(OptionalJwtAuthGuard)
@Get()
@ApiOperation({
summary: '동아리 목록 조회',
Expand Down Expand Up @@ -56,13 +57,13 @@ export class ClubController {
type: GetClubResponseDto,
})
async getClubList(
@User() user: AuthorizedUserDto,
@User() user: AuthorizedUserDto | null,
@Query() clubSearchQueryDto: ClubSearchQueryDto,
): Promise<GetClubResponseDto[]> {
const userId = user.id;
return await this.clubService.getClubList(userId, clubSearchQueryDto);
return await this.clubService.getClubList(user, clubSearchQueryDto);
}

@UseGuards(JwtAuthGuard)
@Post('/like/:clubId')
@ApiOperation({
summary: '동아리 좋아요 등록/해제',
Expand Down Expand Up @@ -101,6 +102,7 @@ export class ClubController {
return await this.clubService.getHotClubList();
}

@UseGuards(OptionalJwtAuthGuard)
@Get('recommend')
@ApiOperation({
summary: 'Recommend Club 목록 조회',
Expand All @@ -112,8 +114,7 @@ export class ClubController {
isArray: true,
type: GetRecommendClubResponseDto,
})
async getRecommendClubList(@User() user: AuthorizedUserDto) {
const userId = user.id;
return await this.clubService.getRecommendClubList(userId);
async getRecommendClubList(@User() user: AuthorizedUserDto | null) {
return await this.clubService.getRecommendClubList(user);
}
}
23 changes: 16 additions & 7 deletions src/home/club/club.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DataSource } from 'typeorm';
import { ClubEntity } from 'src/entities/club.entity';
import { ClubLikeEntity } from 'src/entities/club-like.entity';
import { GetRecommendClubResponseDto } from './dto/get-recommend-club-response.dto';
import { AuthorizedUserDto } from 'src/auth/dto/authorized-user-dto';

@Injectable()
export class ClubService {
Expand All @@ -18,7 +19,7 @@ export class ClubService {
) {}

async getClubList(
userId: number,
user: AuthorizedUserDto | null,
clubSearchQueryDto: ClubSearchQueryDto,
): Promise<GetClubResponseDto[]> {
// 카테고리가 있는 경우 카테고리로 필터링
Expand All @@ -38,16 +39,16 @@ export class ClubService {
return [];
}

// 현재 접속 중인 유저의 각 동아리에 대한 찜 여부 함께 반환
// 현재 접속 중인 유저의 각 동아리에 대한 찜 여부 함께 반환. 유저 존재하지 않을 시 false
let clubList = clubs.map((club) => {
const isLiked = club.clubLikes.some(
(clubLike) => clubLike.user.id === userId,
const isLiked = club.clubLikes.some((clubLike) =>
user ? clubLike.user.id === user.id : false,
);
return new GetClubResponseDto(club, isLiked);
});

// 내가 좋아요를 누른 동아리만 보기
if (wishList) {
// 내가 좋아요를 누른 동아리만 보기 (유저 존재한다면)
if (user && wishList) {
clubList = clubList.filter((club) => club.isLiked === true);
}

Expand Down Expand Up @@ -145,8 +146,16 @@ export class ClubService {
}

async getRecommendClubList(
userId: number,
user: AuthorizedUserDto | null,
): Promise<GetRecommendClubResponseDto[]> {
// 비로그인 or 미인증 유저의 경우 랜덤으로 반환
if (!user) {
const randomClubs = await this.clubRepository.findClubsByRandom();
return randomClubs.map((club) => {
return new GetRecommendClubResponseDto(club);
});
}
const userId = user.id;
const likedClubCategories =
await this.clubLikeRepository.findLikedClubCategories(userId);

Expand Down

0 comments on commit d670214

Please sign in to comment.