Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/87/liked reviews #88

Merged
merged 3 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/common/interfaces/dto/user/user.request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transform } from 'class-transformer';
import { IsArray, IsOptional, IsString } from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { IsArray, IsNumber, IsOptional, IsString } from 'class-validator';
import {
OrderDefaultValidator,
_PROHIBITED_FIELD_PATTERN,
Expand All @@ -13,3 +13,22 @@ export class UserTakenCoursesQueryDto {
@OrderDefaultValidator(_PROHIBITED_FIELD_PATTERN)
order?: string[];
}

export class ReviewLikedQueryDto {
@IsOptional()
@Transform(({ value }) => (typeof value === 'string' ? [value] : value))
@IsArray()
@IsString({ each: true })
@OrderDefaultValidator(_PROHIBITED_FIELD_PATTERN)
order?: string[];

@IsOptional()
@IsNumber()
@Type(() => Number)
offset?: number;

@IsOptional()
@IsNumber()
@Type(() => Number)
limit?: number;
Comment on lines +25 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IsInt()
@Min(0)

@IsInt()
@Min(0)
@Max(최대값)

으로 해주는 것은 어떨까 싶은데 어떻게 생각하시나요? 대부분의 코드가 작성하신 것처럼 되어있긴 한데 나중에라도 이런 식으로 바꿔주면 좋을 것 같아요.

Copy link
Contributor

@Jiuuung Jiuuung Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsInt()를 사용하는게 맞는것 같습니다! 일단 이 부분은 수정해서 다시 커밋할게요 다른 부분에 isnumber사용된 것들이 많은데 그것들은 pr들 merge끝난 후에 한번에 고치는게 좋을 것 같습니다!
min max도 저렇게 추가할게요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doxylee 이거 근데 지금 paginationDTO로 분리해낼까 싶어서요. PaginationDTO로 분리해내고, extends 하는 클래스에서 MinMax 설정하는 식으로 해야할 거 같습니다. extends 하는 클래스에서 무조건 Max를 설정하게끔 강제하는 방법이 있을까요..?

}
19 changes: 18 additions & 1 deletion src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { Controller, Get, HttpException, Param, Query } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import { GetUser } from 'src/common/decorators/get-user.decorator';
import { CourseResponseDtoNested } from 'src/common/interfaces/dto/course/course.response.dto';
import { UserTakenCoursesQueryDto } from 'src/common/interfaces/dto/user/user.request.dto';
import {
ReviewLikedQueryDto,
UserTakenCoursesQueryDto,
} from 'src/common/interfaces/dto/user/user.request.dto';
import { UserService } from './user.service';
import { ReviewResponseDto } from '../../common/interfaces/dto/reviews/review.response.dto';

@Controller('api/users')
export class UserController {
Expand All @@ -21,4 +25,17 @@ export class UserController {
throw new HttpException("Can't find user", 401);
}
}

@Get(':user_id/liked-reviews')
async getUserLikedReviews(
@Query() query: ReviewLikedQueryDto,
@Param('user_id') userId: number,
@GetUser() user: session_userprofile,
): Promise<(ReviewResponseDto & { userspecific_is_liked: boolean })[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userspecific_is_liked도 포함된 타입을 정의해주면 좋을 것 같습니다. 다른 부분에서도 이런 경우 많았던 것 같아요.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 다같이 한번 논의해보는게 좋을 것 같습니다!

if (userId === user.id) {
return await this.userService.getUserLikedReviews(user, userId, query);
} else {
throw new HttpException("Can't find user", 401);
}
}
}
37 changes: 36 additions & 1 deletion src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { session_userprofile } from '@prisma/client';
import { CourseResponseDtoNested } from 'src/common/interfaces/dto/course/course.response.dto';
import { UserTakenCoursesQueryDto } from 'src/common/interfaces/dto/user/user.request.dto';
import {
ReviewLikedQueryDto,
UserTakenCoursesQueryDto,
} from 'src/common/interfaces/dto/user/user.request.dto';
import { ProfileDto } from 'src/common/interfaces/dto/user/user.response.dto';
import { toJsonCourse } from 'src/common/interfaces/serializer/course.serializer';
import { ResearchLecture } from '../../common/interfaces/constants/lecture';
Expand All @@ -14,6 +17,7 @@ import { LectureRepository } from '../../prisma/repositories/lecture.repository'
import { ReviewsRepository } from '../../prisma/repositories/review.repository';
import { UserRepository } from '../../prisma/repositories/user.repository';
import { CourseRepository } from './../../prisma/repositories/course.repository';
import { ReviewResponseDto } from '../../common/interfaces/dto/reviews/review.response.dto';

@Injectable()
export class UserService {
Expand Down Expand Up @@ -120,4 +124,35 @@ export class UserService {
}),
);
}

async getUserLikedReviews(
user: session_userprofile,
userId: number,
query: ReviewLikedQueryDto,
): Promise<(ReviewResponseDto & { userspecific_is_liked: boolean })[]> {
const MAX_LIMIT = 300;
const DEFAULT_ORDER = ['-written_datetime', '-id'];

const reviews = await this.reviewRepository.getLikedReviews(
userId,
query.order ?? DEFAULT_ORDER,
query.offset ?? 0,
query.limit ?? MAX_LIMIT,
);

const result = await Promise.all(
reviews.map(async (review) => {
const result = toJsonReview(review);
const isLiked: boolean = await this.reviewRepository.isLiked(
review.id,
user.id,
);
return Object.assign(result, {
userspecific_is_liked: isLiked,
});
Comment on lines +146 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 모든 review의 userspecific_is_liked가 true가 되지 않을까 생각이 들었는데,
본인 외의 유저가 like한 review들을 찾는 경우 때문에 필요한 부분일까요?
이 메서드 이름 자체를 getMyLikedReviews로 바꾸고 기능을 그렇게 만들어주는 방법도 있을 것 같은데 어떻게 생각하시나요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이번주 task부분에 적어놓은것과 동일한 문제같아요! 그 부분 해결되면 해결책 여기에 적용시키겠습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doxylee 저도 일단 1) 모두 true가 될 거 같다는 생각을 하구 2) 근데 언제 false가 되는지 파악이 안 되긴 합니다.

기능 확인해보고 메서드 이름 수정해보도록 하시져

}),
);

return result;
}
}
37 changes: 37 additions & 0 deletions src/prisma/repositories/review.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ export class ReviewsRepository {
},
});
}

async getReviewsByIds(reviewIds: number[]) {
return this.prisma.review_review.findMany({
where: {
id: {
in: reviewIds,
},
},
include: EReview.Details.include,
});
}

public async getReviews(
order: string[],
offset: number,
Expand Down Expand Up @@ -207,6 +219,31 @@ export class ReviewsRepository {
},
}));
}

public async getLikedReviews(
userId: number,
order: string[],
offset: number,
limit: number,
): Promise<ReviewDetails[]> {
const likedReviews = await this.prisma.review_review.findMany({
where: {
review_reviewvote: {
some: {
userprofile_id: userId,
},
},
},
include: reviewDetails.include,
take: limit,
skip: offset,
orderBy: orderFilter(order),
});

// const likedReviews = this.getReviewsByIds(likedReviewIds);
return likedReviews;
}

public async getReviewsCount(
lectureYear?: number,
lectureSemester?: number,
Expand Down