Feat: add user posts functionality for account page

This commit is contained in:
Aaron William Po
2023-11-26 21:45:03 -05:00
parent c0d90a84d8
commit 00e980d71e
10 changed files with 552 additions and 3 deletions

View File

@@ -0,0 +1,47 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerPostQueryResult from './schema/BeerPostQueryResult';
interface GetBeerPostsByBeerStyleIdArgs {
postedById: string;
pageSize: number;
pageNum: number;
}
const getBeerPostsByPostedById = async ({
pageNum,
pageSize,
postedById,
}: GetBeerPostsByBeerStyleIdArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
const beers = await DBClient.instance.beerPost.findMany({
where: { postedBy: { id: postedById } },
take: pageSize,
skip: pageNum * pageSize,
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
updatedAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
style: { select: { name: true, id: true, description: true } },
beerImages: {
select: {
alt: true,
path: true,
caption: true,
id: true,
createdAt: true,
updatedAt: true,
},
},
},
});
return beers;
};
export default getBeerPostsByPostedById;

View File

@@ -0,0 +1,58 @@
import DBClient from '@/prisma/DBClient';
import BreweryPostQueryResult from '@/services/BreweryPost/schema/BreweryPostQueryResult';
import { z } from 'zod';
const prisma = DBClient.instance;
const getAllBreweryPostsByPostedById = async ({
pageNum,
pageSize,
postedById,
}: {
pageNum: number;
pageSize: number;
postedById: string;
}): Promise<z.infer<typeof BreweryPostQueryResult>[]> => {
const breweryPosts = await prisma.breweryPost.findMany({
where: { postedBy: { id: postedById } },
take: pageSize,
skip: (pageNum - 1) * pageSize,
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
},
description: true,
name: true,
postedBy: { select: { username: true, id: true } },
breweryImages: {
select: {
path: true,
caption: true,
id: true,
alt: true,
createdAt: true,
updatedAt: true,
},
},
createdAt: true,
dateEstablished: true,
},
orderBy: { createdAt: 'desc' },
});
/**
* Prisma does not support tuples, so we have to typecast the coordinates field to
* [number, number] in order to satisfy the zod schema.
*/
return breweryPosts as Awaited<ReturnType<typeof getAllBreweryPostsByPostedById>>;
};
export default getAllBreweryPostsByPostedById;