Skip to content

Commit

Permalink
fix: show course review labels only
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlhw committed Dec 11, 2024
1 parent 7f641bf commit c7d8cc6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export default async function Image({ params }: { params: { code: string } }) {
const course = await api.courses.getByCourseCode({ code: courseCode });
if (!course) return null;

const courseReviewStats = await api.reviews.getMetadataByCourseCode({
code: courseCode,
});
const { averageRating, reviewCount, reviewLabels } =
await api.reviews.getMetadataByCourseCode({
code: courseCode,
});

const profCount = await api.professors.countByCourseCode({ courseCode });

Expand All @@ -46,14 +47,12 @@ export default async function Image({ params }: { params: { code: string } }) {
{course?.name}
</OgImage.Title>
<OgImage.Content
rating={courseReviewStats.averageRating.toFixed(2)}
reviewCount={courseReviewStats.reviewCount}
rating={averageRating.toFixed(2)}
reviewCount={reviewCount}
profCount={profCount}
statItems={courseReviewStats.reviewLabels.map((label) => ({
statItems={reviewLabels.map((label) => ({
label: toTitleCase(label.name),
value: formatPercentage(
label.count / courseReviewStats.reviewCount,
),
value: formatPercentage(label.count && label.count / reviewCount),
}))}
/>
</OgImage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export default async function Image({ params }: { params: { slug: string } }) {
const prof = await api.professors.getBySlug({ slug });
if (!prof) return null;

const professorReviewStats = await api.reviews.getMetadataByProfSlug({
slug,
});
const { averageRating, reviewCount, reviewLabels } =
await api.reviews.getMetadataByProfSlug({
slug,
});
const courseCount = await api.courses.countByProfSlug({ slug });

return new ImageResponse(
Expand All @@ -45,14 +46,12 @@ export default async function Image({ params }: { params: { slug: string } }) {
{prof.name}
</OgImage.Title>
<OgImage.Content
rating={professorReviewStats.averageRating.toFixed(2)}
reviewCount={professorReviewStats.reviewCount}
rating={averageRating.toFixed(2)}
reviewCount={reviewCount}
courseCount={courseCount}
statItems={professorReviewStats.reviewLabels.map((label) => ({
statItems={reviewLabels.map((label) => ({
label: toTitleCase(label.name),
value: formatPercentage(
label.count / professorReviewStats.reviewCount,
),
value: formatPercentage(label.count && label.count / reviewCount),
}))}
/>
</OgImage>
Expand Down
99 changes: 51 additions & 48 deletions src/server/api/routers/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Prisma } from "@prisma/client";
import { type Review } from "@/modules/reviews/types";
import { reviewFormSchema } from "@/common/tools/zod/schemas";
import { ReviewableEnum } from "@/modules/submit/types";
import { toTitleCase } from "@/common/functions";

const DEFAULT_PAGE_SIZE = 10;

Expand Down Expand Up @@ -510,9 +511,20 @@ export const reviewsRouter = createTRPCRouter({
},
});

// alternative to rawQuery, we can use:
// alternative to prisma query, we can use:
/*
db.reviewLabels.groupBy(
SELECT l.name, count(l.id) FROM review_labels rl
JOIN labels l ON rl.label_id = l.id
WHERE rl.review_id IN (
SELECT id FROM reviews
WHERE reviewed_professor_id = (
SELECT id FROM professors
WHERE slug = ${input.slug}
)
)
GROUP BY l.name
*/
const reviewLabelsMetadataForThisProf = await ctx.db.reviewLabels.groupBy(
{
by: ["labelId"],
_count: {
Expand All @@ -525,31 +537,21 @@ export const reviewsRouter = createTRPCRouter({
},
},
);
*/
type ReviewLabelsMetadata = {
name: string;
count: bigint; // db aggregate query returns BigInt
};
const reviewLabelsMetadataForThisProf: ReviewLabelsMetadata[] = await ctx
.db.$queryRaw`
SELECT l.name, count(l.id) FROM review_labels rl
JOIN labels l ON rl.label_id = l.id
WHERE rl.review_id IN (
SELECT id FROM reviews
WHERE reviewed_professor_id = (
SELECT id FROM professors
WHERE slug = ${input.slug}
)
)
GROUP BY l.name
`;
const professorReviewLabels = await ctx.db.labels.findMany({
where: {
typeOf: "PROFESSOR",
},
});

return {
averageRating: reviewsMetadataForThisProf._avg.rating ?? 0,
reviewCount: reviewsMetadataForThisProf._count._all,
reviewLabels: reviewLabelsMetadataForThisProf.map((label) => ({
name: label.name,
count: Number(label.count),
reviewLabels: professorReviewLabels.sort().map((label) => ({
name: toTitleCase(label.name.replaceAll("_", " ")),
count:
reviewLabelsMetadataForThisProf.find(
(rl) => rl.labelId === label.id,
)?._count.labelId ?? 0,
})),
};
}),
Expand All @@ -573,10 +575,21 @@ export const reviewsRouter = createTRPCRouter({
},
});

// alternative to rawQuery, we can use:
// alternative to prisma query, we can use:
/*
db.reviewLabels.groupBy(
{
SELECT l.name, count(l.id) FROM labels l
JOIN review_labels rl ON rl.label_id = l.id
WHERE rl.review_id IN (
SELECT id FROM reviews
WHERE reviewed_course_id = (
SELECT id FROM courses
WHERE code = ${input.code}
)
)
GROUP BY l.name
*/
const reviewLabelsMetadataForThisCourse =
await ctx.db.reviewLabels.groupBy({
by: ["labelId"],
_count: {
labelId: true,
Expand All @@ -586,33 +599,23 @@ export const reviewsRouter = createTRPCRouter({
reviewedCourse: { code: input.code },
},
},
});

const courseReviewLabels = await ctx.db.labels.findMany({
where: {
typeOf: "COURSE",
},
);
*/
type ReviewLabelsMetadata = {
name: string;
count: bigint; // db aggregate query returns BigInt
};
const reviewLabelsMetadataForThisCourse: ReviewLabelsMetadata[] =
await ctx.db.$queryRaw`
SELECT l.name, count(l.id) FROM review_labels rl
JOIN labels l ON rl.label_id = l.id
WHERE rl.review_id IN (
SELECT id FROM reviews
WHERE reviewed_course_id = (
SELECT id FROM courses
WHERE code = ${input.code}
)
)
GROUP BY l.name
`;
});

return {
averageRating: reviewsMetadataForThisCourse._avg.rating ?? 0,
reviewCount: reviewsMetadataForThisCourse._count._all,
reviewLabels: reviewLabelsMetadataForThisCourse.map((label) => ({
name: label.name,
count: Number(label.count),
reviewLabels: courseReviewLabels.sort().map((label) => ({
name: toTitleCase(label.name.replaceAll("_", " ")),
count:
reviewLabelsMetadataForThisCourse.find(
(rl) => rl.labelId === label.id,
)?._count.labelId ?? 0,
})),
};
}),
Expand Down

0 comments on commit c7d8cc6

Please sign in to comment.