Feat: Update beer recs to be loaded on the client side

This commit is contained in:
Aaron William Po
2023-05-14 14:55:02 -04:00
parent 2d2cd8189a
commit 50c3e1a82b
10 changed files with 308 additions and 80 deletions

View File

@@ -1,22 +1,51 @@
import DBClient from '@/prisma/DBClient';
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { z } from 'zod';
const getBeerRecommendations = async (
beerPost: Pick<z.infer<typeof beerPostQueryResult>, 'type' | 'brewery' | 'id'>,
) => {
const beerRecommendations = await DBClient.instance.beerPost.findMany({
interface GetBeerRecommendationsArgs {
beerPost: z.infer<typeof BeerPostQueryResult>;
pageNum: number;
pageSize: number;
}
const getBeerRecommendations = async ({
beerPost,
pageNum,
pageSize,
}: GetBeerRecommendationsArgs) => {
const skip = (pageNum - 1) * pageSize;
const take = pageSize;
const beerRecommendations: z.infer<typeof BeerPostQueryResult>[] =
await DBClient.instance.beerPost.findMany({
where: {
OR: [{ typeId: beerPost.type.id }, { breweryId: beerPost.brewery.id }],
NOT: { id: beerPost.id },
},
select: {
id: true,
name: true,
ibu: true,
abv: true,
description: true,
createdAt: true,
type: { select: { name: true, id: true } },
brewery: { select: { name: true, id: true } },
postedBy: { select: { id: true, username: true } },
beerImages: { select: { path: true, caption: true, id: true, alt: true } },
},
take,
skip,
});
const count = await DBClient.instance.beerPost.count({
where: {
OR: [{ typeId: beerPost.type.id }, { breweryId: beerPost.brewery.id }],
NOT: { id: beerPost.id },
},
include: {
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
brewery: { select: { id: true, name: true } },
},
});
return beerRecommendations;
return { beerRecommendations, count };
};
export default getBeerRecommendations;