mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Feat: add user posts functionality for account page
This commit is contained in:
47
src/services/BeerPost/getBeerPostsByPostedById.ts
Normal file
47
src/services/BeerPost/getBeerPostsByPostedById.ts
Normal 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;
|
||||
58
src/services/BreweryPost/getAllBreweryPostsByPostedById.ts
Normal file
58
src/services/BreweryPost/getAllBreweryPostsByPostedById.ts
Normal 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;
|
||||
Reference in New Issue
Block a user