otaki0413 / YelpCamp-with-Remix

YelpCampをモダンな技術で置き換えるプロジェクト
https://yelp-hotspring-otaki.koyeb.app/
6 stars 0 forks source link

温泉一覧画面:温泉写真とスター数の平均とレビュー数を表示する #32

Closed otaki0413 closed 6 months ago

otaki0413 commented 6 months ago

完了の定義

一覧画面(/hotsprings)の各カードに下記温泉情報を表示させる

補足情報

現状のイメージ

CleanShot 2024-02-24 at 18 23 27@2x

otaki0413 commented 6 months ago

現状、こんな感じ

CleanShot 2024-02-25 at 14 58 21@2x

otaki0413 commented 6 months ago

分からない点

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");
  }
}

解決につながりそうな案

参考サイト

otaki0413 commented 6 months ago

下記については、現状対応が難しそうなため、#32 でコード上にコメント残した。 https://github.com/otaki0413/YelpCamp-with-Remix/issues/32#issuecomment-1962826634