mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Work on brewery page, refactors
Refactor query types to explicitly use z.infer
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BreweryPostQueryResult from './types/BreweryPostQueryResult';
|
||||
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
||||
import { z } from 'zod';
|
||||
|
||||
const prisma = DBClient.instance;
|
||||
|
||||
const getAllBreweryPosts = async () => {
|
||||
const breweryPosts: BreweryPostQueryResult[] = await prisma.breweryPost.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
location: true,
|
||||
name: true,
|
||||
postedBy: { select: { firstName: true, lastName: true, id: true } },
|
||||
},
|
||||
});
|
||||
const breweryPosts: z.infer<typeof BreweryPostQueryResult>[] =
|
||||
await prisma.breweryPost.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
location: true,
|
||||
name: true,
|
||||
postedBy: { select: { username: true, id: true } },
|
||||
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
|
||||
},
|
||||
});
|
||||
|
||||
return breweryPosts;
|
||||
};
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BreweryPostQueryResult from './types/BreweryPostQueryResult';
|
||||
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
||||
import { z } from 'zod';
|
||||
|
||||
const prisma = DBClient.instance;
|
||||
|
||||
const getBreweryPostById = async (id: string) => {
|
||||
const breweryPost: BreweryPostQueryResult | null = await prisma.breweryPost.findFirst({
|
||||
select: {
|
||||
id: true,
|
||||
location: true,
|
||||
name: true,
|
||||
postedBy: {
|
||||
select: {
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
id: true,
|
||||
},
|
||||
const breweryPost: z.infer<typeof BreweryPostQueryResult> | null =
|
||||
await prisma.breweryPost.findFirst({
|
||||
select: {
|
||||
id: true,
|
||||
location: true,
|
||||
name: true,
|
||||
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
|
||||
postedBy: { select: { username: true, id: true } },
|
||||
},
|
||||
},
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return breweryPost;
|
||||
};
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
export default interface BreweryPostQueryResult {
|
||||
id: string;
|
||||
location: string;
|
||||
name: string;
|
||||
postedBy: {
|
||||
id: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
};
|
||||
}
|
||||
import { z } from 'zod';
|
||||
|
||||
const BreweryPostQueryResult = z.object({
|
||||
id: z.string(),
|
||||
location: z.string(),
|
||||
name: z.string(),
|
||||
postedBy: z.object({ id: z.string(), username: z.string() }),
|
||||
breweryImages: z.array(
|
||||
z.object({ path: z.string(), caption: z.string(), id: z.string(), alt: z.string() }),
|
||||
),
|
||||
});
|
||||
|
||||
export default BreweryPostQueryResult;
|
||||
|
||||
Reference in New Issue
Block a user