mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
BeerPostQueryResult type now inferred from zod schema
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BeerPostQueryResult from '../BeerPost/schema/BeerPostQueryResult';
|
||||
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
import { BeerCommentQueryResultArrayT } from './schema/BeerCommentQueryResult';
|
||||
|
||||
const getAllBeerComments = async (
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import BeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
|
||||
import { BeerPostQueryResult } from './schema/BeerPostQueryResult';
|
||||
import CreateBeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
|
||||
|
||||
const CreateBeerPostWithUserSchema = BeerPostValidationSchema.extend({
|
||||
const CreateBeerPostWithUserSchema = CreateBeerPostValidationSchema.extend({
|
||||
userId: z.string().uuid(),
|
||||
});
|
||||
|
||||
@@ -15,7 +16,7 @@ const createNewBeerPost = async ({
|
||||
breweryId,
|
||||
userId,
|
||||
}: z.infer<typeof CreateBeerPostWithUserSchema>) => {
|
||||
const newBeerPost = await DBClient.instance.beerPost.create({
|
||||
const newBeerPost: BeerPostQueryResult = await DBClient.instance.beerPost.create({
|
||||
data: {
|
||||
name,
|
||||
description,
|
||||
@@ -25,6 +26,18 @@ const createNewBeerPost = async ({
|
||||
postedBy: { connect: { id: userId } },
|
||||
brewery: { connect: { id: breweryId } },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
abv: true,
|
||||
ibu: true,
|
||||
createdAt: true,
|
||||
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
|
||||
brewery: { select: { id: true, name: true } },
|
||||
type: { select: { id: true, name: true } },
|
||||
postedBy: { select: { id: true, username: true } },
|
||||
},
|
||||
});
|
||||
return newBeerPost;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
||||
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
|
||||
const prisma = DBClient.instance;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
||||
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
|
||||
const prisma = DBClient.instance;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
||||
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
|
||||
const getBeerRecommendations = async (
|
||||
beerPost: Pick<BeerPostQueryResult, 'type' | 'brewery' | 'id'>,
|
||||
|
||||
@@ -1,28 +1,36 @@
|
||||
export default interface BeerPostQueryResult {
|
||||
id: string;
|
||||
name: string;
|
||||
brewery: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
description: string;
|
||||
beerImages: {
|
||||
path: string;
|
||||
caption: string;
|
||||
id: string;
|
||||
alt: string;
|
||||
}[];
|
||||
import { z } from 'zod';
|
||||
|
||||
ibu: number;
|
||||
abv: number;
|
||||
type: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
postedBy: {
|
||||
id: string;
|
||||
username: string;
|
||||
};
|
||||
export const beerPostQueryResultSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
brewery: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
}),
|
||||
description: z.string(),
|
||||
beerImages: z.array(
|
||||
z.object({
|
||||
path: z.string(),
|
||||
caption: z.string(),
|
||||
id: z.string(),
|
||||
alt: z.string(),
|
||||
}),
|
||||
),
|
||||
ibu: z.number(),
|
||||
abv: z.number(),
|
||||
type: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
}),
|
||||
postedBy: z.object({
|
||||
id: z.string(),
|
||||
username: z.string(),
|
||||
}),
|
||||
createdAt: z.date(),
|
||||
});
|
||||
|
||||
createdAt: Date;
|
||||
}
|
||||
export const beerPostQueryResultArraySchema = z.array(beerPostQueryResultSchema);
|
||||
|
||||
export type BeerPostQueryResult = z.infer<typeof beerPostQueryResultSchema>;
|
||||
|
||||
export type BeerPostQueryResultArray = z.infer<typeof beerPostQueryResultArraySchema>;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const BeerPostValidationSchema = z.object({
|
||||
const CreateBeerPostValidationSchema = z.object({
|
||||
name: z
|
||||
.string({
|
||||
required_error: 'Beer name is required.',
|
||||
@@ -40,4 +40,4 @@ const BeerPostValidationSchema = z.object({
|
||||
.uuid({ message: 'Invalid brewery id.' }),
|
||||
});
|
||||
|
||||
export default BeerPostValidationSchema;
|
||||
export default CreateBeerPostValidationSchema;
|
||||
|
||||
Reference in New Issue
Block a user