Closed otaki0413 closed 6 months ago
現状、こんな感じ
getHotSprings
で取得したいがリレーション先の集計値のためできないのではないか??getRatingAvg
を作って、平均値取得のみを行っているexport async function getHotSprings() {
return await prisma.hotSpring.findMany({
select: {
id: true,
title: true,
location: true,
images: true,
_count: {
select: { reviews: true },
},
},
orderBy: { updatedAt: "desc" },
});
}
export async function getRatingAvg(id: HotSpring["id"]) {
try {
const result = await prisma.review.aggregate({
_avg: {
rating: true,
},
where: {
hotSpringId: id,
},
});
return result._avg.rating ? parseFloat(result._avg.rating.toFixed(2)) : 0; // 小数点第2位まで出す
} catch (error) {
console.log(error);
throw new Error("Unexpected error");
}
}
$extends
メソッドを使用する下記については、現状対応が難しそうなため、#32 でコード上にコメント残した。 https://github.com/otaki0413/YelpCamp-with-Remix/issues/32#issuecomment-1962826634
完了の定義
一覧画面(/hotsprings)の各カードに下記温泉情報を表示させる
補足情報
現状のイメージ