Work on brewery page, refactors

Refactor query types to explicitly use z.infer
This commit is contained in:
Aaron William Po
2023-03-31 21:13:35 -04:00
parent d8a8dad37f
commit b69dbc95b4
33 changed files with 308 additions and 243 deletions

View File

@@ -1,13 +1,14 @@
import DBClient from '@/prisma/DBClient';
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
import { BeerCommentQueryResultArrayT } from './schema/BeerCommentQueryResult';
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { z } from 'zod';
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
const getAllBeerComments = async (
{ id }: Pick<BeerPostQueryResult, 'id'>,
{ id }: Pick<z.infer<typeof beerPostQueryResult>, 'id'>,
{ pageSize, pageNum = 0 }: { pageSize: number; pageNum?: number },
) => {
const skip = (pageNum - 1) * pageSize;
const beerComments: BeerCommentQueryResultArrayT =
const beerComments: z.infer<typeof BeerCommentQueryResult>[] =
await DBClient.instance.beerComment.findMany({
skip,
take: pageSize,

View File

@@ -1,8 +1,8 @@
import { z } from 'zod';
export const BeerCommentQueryResult = z.object({
const BeerCommentQueryResult = z.object({
id: z.string().uuid(),
content: z.string().min(1).max(300),
content: z.string().min(1).max(500),
rating: z.number().int().min(1).max(5),
createdAt: z.coerce.date(),
postedBy: z.object({
@@ -10,6 +10,5 @@ export const BeerCommentQueryResult = z.object({
username: z.string().min(1).max(50),
}),
});
export const BeerCommentQueryResultArray = z.array(BeerCommentQueryResult);
export type BeerCommentQueryResultT = z.infer<typeof BeerCommentQueryResult>;
export type BeerCommentQueryResultArrayT = z.infer<typeof BeerCommentQueryResultArray>;
export default BeerCommentQueryResult;

View File

@@ -1,6 +1,6 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import { BeerPostQueryResult } from './schema/BeerPostQueryResult';
import beerPostQueryResult from './schema/BeerPostQueryResult';
import CreateBeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
const CreateBeerPostWithUserSchema = CreateBeerPostValidationSchema.extend({
@@ -16,29 +16,30 @@ const createNewBeerPost = async ({
breweryId,
userId,
}: z.infer<typeof CreateBeerPostWithUserSchema>) => {
const newBeerPost: BeerPostQueryResult = await DBClient.instance.beerPost.create({
data: {
name,
description,
abv,
ibu,
type: { connect: { id: typeId } },
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 } },
},
});
const newBeerPost: z.infer<typeof beerPostQueryResult> =
await DBClient.instance.beerPost.create({
data: {
name,
description,
abv,
ibu,
type: { connect: { id: typeId } },
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;
};

View File

@@ -1,27 +1,30 @@
import DBClient from '@/prisma/DBClient';
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { z } from 'zod';
const prisma = DBClient.instance;
const getAllBeerPosts = async (pageNum: number, pageSize: number) => {
const skip = (pageNum - 1) * pageSize;
const beerPosts: BeerPostQueryResult[] = await prisma.beerPost.findMany({
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 } },
const beerPosts: z.infer<typeof beerPostQueryResult>[] = await prisma.beerPost.findMany(
{
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: pageSize,
skip,
},
take: pageSize,
skip,
});
);
return beerPosts;
};

View File

@@ -1,24 +1,26 @@
import DBClient from '@/prisma/DBClient';
import { BeerPostQueryResult } from '@/services/BeerPost/schema/BeerPostQueryResult';
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
import { z } from 'zod';
const prisma = DBClient.instance;
const getBeerPostById = async (id: string) => {
const beerPost: BeerPostQueryResult | null = await prisma.beerPost.findFirst({
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
type: { select: { name: true, id: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
where: { id },
});
const beerPost: z.infer<typeof beerPostQueryResult> | null =
await prisma.beerPost.findFirst({
select: {
id: true,
name: true,
ibu: true,
abv: true,
createdAt: true,
description: true,
postedBy: { select: { username: true, id: true } },
brewery: { select: { name: true, id: true } },
type: { select: { name: true, id: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
where: { id },
});
return beerPost;
};

View File

@@ -1,8 +1,9 @@
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<BeerPostQueryResult, 'type' | 'brewery' | 'id'>,
beerPost: Pick<z.infer<typeof beerPostQueryResult>, 'type' | 'brewery' | 'id'>,
) => {
const beerRecommendations = await DBClient.instance.beerPost.findMany({
where: {

View File

@@ -1,36 +1,18 @@
import { z } from 'zod';
export const beerPostQueryResultSchema = z.object({
const beerPostQueryResult = z.object({
id: z.string(),
name: z.string(),
brewery: 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(),
}),
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(),
}),
type: z.object({ id: z.string(), name: z.string() }),
postedBy: z.object({ id: z.string(), username: z.string() }),
createdAt: z.coerce.date(),
});
export const beerPostQueryResultArraySchema = z.array(beerPostQueryResultSchema);
export type BeerPostQueryResult = z.infer<typeof beerPostQueryResultSchema>;
export type BeerPostQueryResultArray = z.infer<typeof beerPostQueryResultArraySchema>;
export default beerPostQueryResult;

View File

@@ -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;
};

View File

@@ -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;
};

View File

@@ -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;