Skip to content

Commit

Permalink
Implement fetching related reviews of a lecture (#82)
Browse files Browse the repository at this point in the history
* feat(lectures): define entry for GET /lectures/:lectureId/related-reviews

* feat(lecture): impl svc level queries

* feat(review): implement ReviewRepository#getRelatedReviewsOfLecture

* refactor(review): use common expression snippet

* refactor(repositories): use EReview.Details (@doxylee)

* refactor(repositories): use orderFilters of search utils (@doxylee)

* refactor(lectures): rename (@doxylee)

* docs(lectures): add comment
  • Loading branch information
inhibitor1217 authored Mar 27, 2024
1 parent ed3c692 commit 18039e5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/modules/lectures/lectures.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ export class LecturesController {
@Query() query: IReview.LectureReviewsQueryDto,
@Param('lectureId') lectureId: number,
@GetUser() user: session_userprofile,
// TODO: Consider using IReview.Basic
): Promise<(ReviewResponseDto & { userspecific_is_liked: boolean })[]> {
return await this.LectureService.getLectureReviews(user, lectureId, query);
}

@Public()
@Get(':lectureId/related-reviews')
async getLectureRelatedReviews(
@Query() query: IReview.LectureReviewsQueryDto,
@Param('lectureId') lectureId: number,
@GetUser() user: session_userprofile,
// TODO: Consider using IReview.Basic
): Promise<(ReviewResponseDto & { userspecific_is_liked: boolean })[]> {
return await this.LectureService.getLectureRelatedReviews(
user,
lectureId,
query,
);
}
}
39 changes: 38 additions & 1 deletion src/modules/lectures/lectures.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ReviewResponseDto } from 'src/common/interfaces/dto/reviews/review.resp
import { toJsonLecture } from 'src/common/interfaces/serializer/lecture.serializer';
import { toJsonReview } from 'src/common/interfaces/serializer/review.serializer';
import { ReviewsRepository } from 'src/prisma/repositories/review.repository';
import { LectureDetails } from '../../common/schemaTypes/types';
import { LectureDetails, ReviewDetails } from '../../common/schemaTypes/types';
import { LectureRepository } from './../../prisma/repositories/lecture.repository';

@Injectable()
Expand Down Expand Up @@ -66,6 +66,43 @@ export class LecturesService {
);
}

public async getLectureRelatedReviews(
user: session_userprofile,
lectureId: number,
query: IReview.LectureReviewsQueryDto,
): Promise<(ReviewResponseDto & { userspecific_is_liked: boolean })[]> {
const DEFAULT_LIMIT = 100;
const DEFAULT_ORDER = ['-written_datetime', '-id'];

const lecture = await this.LectureRepository.getLectureById(lectureId);
const reviews: ReviewDetails[] =
await this.reviewsRepository.getRelatedReviewsOfLecture(
query.order ?? DEFAULT_ORDER,
query.offset ?? 0,
query.limit ?? DEFAULT_LIMIT,
lecture,
);

return await Promise.all(
reviews.map(async (review) => {
const result = toJsonReview(review);
if (user) {
const isLiked: boolean = await this.reviewsRepository.isLiked(
review.id,
user.id,
);
return Object.assign(result, {
userspecific_is_liked: isLiked,
});
} else {
return Object.assign(result, {
userspecific_is_liked: false,
});
}
}),
);
}

public async getLecturesByIds(ids: number[]): Promise<LectureDetails[]> {
return await this.LectureRepository.getLectureByIds(ids);
}
Expand Down
49 changes: 48 additions & 1 deletion src/prisma/repositories/review.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
} from '@prisma/client';
import { EReview } from 'src/common/entities/EReview';
import { orderFilter } from 'src/common/utils/search.utils';
import { ReviewDetails, reviewDetails } from '../../common/schemaTypes/types';
import {
LectureDetails,
ReviewDetails,
reviewDetails,
} from '../../common/schemaTypes/types';
import { PrismaService } from '../prisma.service';

@Injectable()
Expand Down Expand Up @@ -150,6 +154,49 @@ export class ReviewsRepository {
});
}

public async getRelatedReviewsOfLecture(
order: string[],
offset: number,
limit: number,
lecture: LectureDetails,
): Promise<ReviewDetails[]> {
return await this.prisma.review_review.findMany({
...EReview.Details,
where: {
lecture: {
course_id: lecture.course_id,
subject_lecture_professors: {
some: {
professor_id: {
in: lecture.subject_lecture_professors.map(
(professor) => professor.professor_id,
),
},
},
},
},
},
skip: offset,
take: limit,
orderBy: orderFilter(order),
distinct: [
'id',
'course_id',
'lecture_id',
'content',
'grade',
'load',
'speech',
'writer_id',
'writer_label',
'updated_datetime',
'like',
'is_deleted',
'written_datetime',
],
});
}

async isLiked(reviewId: number, userId: number): Promise<boolean> {
return !!(await this.prisma.review_reviewvote.findUnique({
where: {
Expand Down

0 comments on commit 18039e5

Please sign in to comment.