Skip to content

Commit

Permalink
feat(reviews): get reviews in tutories detail; cast avgRating and `…
Browse files Browse the repository at this point in the history
…totalReviews` to number
  • Loading branch information
Veirt committed Dec 2, 2024
1 parent 642171c commit 25c913f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/module/review/review.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db as dbType } from "@/db/config";
import { reviews, orders, learners, tutories } from "@/db/schema";
import { and, desc, eq, inArray, sql } from "drizzle-orm";
import { desc, eq, inArray, sql } from "drizzle-orm";

export class ReviewRepository {
constructor(private readonly db: typeof dbType) {}
Expand Down Expand Up @@ -51,8 +51,8 @@ export class ReviewRepository {
async getAverageRating(tutoriesId: string) {
const result = await this.db
.select({
avgRating: sql<number>`AVG(${reviews.rating})::numeric(10,1)`,
totalReviews: sql<number>`COUNT(${reviews.id})`,
avgRating: sql<number>`AVG(${reviews.rating})::numeric(10,1)::float`,
totalReviews: sql<number>`COUNT(${reviews.id})::int`,
})
.from(reviews)
.innerJoin(orders, eq(reviews.orderId, orders.id))
Expand All @@ -68,7 +68,7 @@ export class ReviewRepository {
.select({
tutoriesId: orders.tutoriesId,
avgRating: sql<number>`COALESCE(AVG(${reviews.rating})::numeric(10,1), 0)`,
totalReviews: sql<number>`COUNT(${reviews.id})`,
totalReviews: sql<number>`COUNT(${reviews.id})::int`,
})
.from(reviews)
.innerJoin(orders, eq(reviews.orderId, orders.id))
Expand Down
14 changes: 8 additions & 6 deletions src/module/tutories/tutories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ export class TutoriesService {

async getTutoriesDetail(tutoriesId: string) {
try {
const [tutories, { avgRating, totalReviews }] = await Promise.all([
await this.tutoriesRepository.getTutoriesDetail(tutoriesId),
await this.reviewRepository.getAverageRating(tutoriesId),
]);

return { ...tutories, avgRating, totalReviews };
const [tutories, reviews, { avgRating, totalReviews }] =
await Promise.all([
await this.tutoriesRepository.getTutoriesDetail(tutoriesId),
await this.reviewRepository.getTutoriesReviews(tutoriesId),
await this.reviewRepository.getAverageRating(tutoriesId),
]);

return { ...tutories, reviews, avgRating, totalReviews };
} catch (error) {
logger.error(`Failed to get tutories detail: ${error}`);
}
Expand Down

0 comments on commit 25c913f

Please sign in to comment.