diff --git a/components/apiprovider.tsx b/components/apiprovider.tsx index 55f5f1a..c10ca15 100644 --- a/components/apiprovider.tsx +++ b/components/apiprovider.tsx @@ -1,10 +1,17 @@ import { createContext, useState, useEffect, useContext } from "react"; -import { Book, Rule, Comment, Category, ApiReturnMsg } from "lib/types"; +import { + Book, + BookInfo, + Rule, + Comment, + Category, + ApiReturnMsg, +} from "lib/types"; import { useRouter } from "next/router"; interface ApiContextI { fetchAll: () => void; - books: Book[]; + books: BookInfo[]; currentBook: Book; rules: Rule[]; categories: Category[]; @@ -30,7 +37,7 @@ const emptyBook = () => ({ }); export function ApiProvider(props: { children: any }) { const { query } = useRouter(); - const [books, setBooks] = useState([]); + const [books, setBooks] = useState([]); const [currentBook, setCurrentBook] = useState(emptyBook()); const [rules, setRules] = useState([]); useEffect(() => { @@ -66,7 +73,7 @@ export function ApiProvider(props: { children: any }) { const fetchAll = () => { void (async () => { const res = await fetch("/api/fetch_book"); - const resData = (await res.json()) as Book[]; + const resData = (await res.json()) as BookInfo[]; setBooks(resData); })(); }; diff --git a/components/bookitem.tsx b/components/bookitem.tsx index 4c1a9fd..2507275 100644 --- a/components/bookitem.tsx +++ b/components/bookitem.tsx @@ -61,7 +61,7 @@ export const BookItem = (props: { size="small" startIcon={} > - {/*book.rules.length*/} + {book.rulesNum} @@ -71,7 +71,7 @@ export const BookItem = (props: { size="small" startIcon={} > - {/*book.rules.reduce((prev, r) => prev + r.comments.length, 0)*/} + {book.commentsNum} diff --git a/lib/types.ts b/lib/types.ts index 3e05d25..5619cfa 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -13,6 +13,7 @@ export const bookInclude = Prisma.validator()({ }); const book = Prisma.validator()({ include: bookInclude }); export type Book = Prisma.BookGetPayload; +export type BookInfo = Book & { rulesNum: number; commentsNum: number }; export const ruleInclude = Prisma.validator()({ comments: { diff --git a/pages/api/fetch_book.ts b/pages/api/fetch_book.ts index d0c706a..cab4039 100644 --- a/pages/api/fetch_book.ts +++ b/pages/api/fetch_book.ts @@ -1,6 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "lib/prisma"; -import { bookInclude } from "lib/types"; +import { bookInclude, bookInclude2 } from "lib/types"; export default function fetchBooks(req: NextApiRequest, res: NextApiResponse) { void (async (req, res) => { @@ -13,7 +13,28 @@ export default function fetchBooks(req: NextApiRequest, res: NextApiResponse) { status = 500; console.log(err); }); + const dataInfo = await Promise.all( + data?.map(async (b) => ({ + ...b, + rulesNum: ( + await prisma.rule.findMany({ + select: { id: true }, + where: { bookId: b.id }, + }) + ).length, + commentsNum: ( + await prisma.comment.findMany({ + select: { id: true }, + where: { + category: { + bookId: b.id, + }, + }, + }) + ).length, + })) + ); await prisma.$disconnect(); - res.status(status).json(data || []); + res.status(status).json(dataInfo || []); })(req, res); }