This commit is contained in:
Aaron William Po
2023-10-07 13:27:01 -04:00
parent 5b287ed2ac
commit 2ee12d351f
21 changed files with 346 additions and 210 deletions

View File

@@ -1,8 +0,0 @@
import { z } from 'zod';
const CreateCommentValidationSchema = z.object({
userId: z.string().uuid(),
beerPostId: z.string().uuid(),
});
export default CreateCommentValidationSchema;

View File

@@ -7,7 +7,7 @@ const CreateBeerPostWithUserSchema = CreateBeerPostValidationSchema.extend({
userId: z.string().cuid(),
});
const createNewBeerPost = async ({
const createNewBeerPost = ({
name,
description,
abv,
@@ -15,32 +15,33 @@ const createNewBeerPost = async ({
styleId,
breweryId,
userId,
}: z.infer<typeof CreateBeerPostWithUserSchema>) => {
const newBeerPost: z.infer<typeof BeerPostQueryResult> =
await DBClient.instance.beerPost.create({
data: {
name,
description,
abv,
ibu,
style: { connect: { id: styleId } },
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 } },
style: { select: { id: true, name: true, description: true } },
postedBy: { select: { id: true, username: true } },
},
});
return newBeerPost;
}: z.infer<typeof CreateBeerPostWithUserSchema>): Promise<
z.infer<typeof BeerPostQueryResult>
> => {
return DBClient.instance.beerPost.create({
data: {
name,
description,
abv,
ibu,
style: { connect: { id: styleId } },
postedBy: { connect: { id: userId } },
brewery: { connect: { id: breweryId } },
},
select: {
id: true,
name: true,
description: true,
abv: true,
ibu: true,
createdAt: true,
updatedAt: true,
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
brewery: { select: { id: true, name: true } },
style: { select: { id: true, name: true, description: true } },
postedBy: { select: { id: true, username: true } },
},
});
};
export default createNewBeerPost;

View File

@@ -2,27 +2,29 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerPostQueryResult from './schema/BeerPostQueryResult';
const deleteBeerPostById = async (
id: string,
): Promise<z.infer<typeof BeerPostQueryResult> | null> => {
const deleted = await DBClient.instance.beerPost.delete({
where: { id },
interface DeleteBeerPostByIdArgs {
beerPostId: string;
}
const deleteBeerPostById = ({
beerPostId,
}: DeleteBeerPostByIdArgs): Promise<z.infer<typeof BeerPostQueryResult> | null> => {
return DBClient.instance.beerPost.delete({
where: { id: beerPostId },
select: {
abv: true,
createdAt: true,
description: true,
ibu: true,
id: true,
name: true,
brewery: { select: { id: true, name: true } },
description: true,
updatedAt: true,
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
ibu: true,
abv: true,
style: { select: { id: true, name: true, description: true } },
postedBy: { select: { id: true, username: true } },
createdAt: true,
updatedAt: true,
brewery: { select: { id: true, name: true } },
},
});
return deleted;
};
export default deleteBeerPostById;

View File

@@ -1,10 +1,36 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import EditBeerPostValidationSchema from './schema/EditBeerPostValidationSchema';
import BeerPostQueryResult from './schema/BeerPostQueryResult';
const schema = EditBeerPostValidationSchema.omit({ id: true });
const schema = EditBeerPostValidationSchema.omit({ id: true, styleId: true });
export default async function editBeerPostById(id: string, data: z.infer<typeof schema>) {
const beerPost = await DBClient.instance.beerPost.update({ where: { id }, data });
return beerPost;
interface EditBeerPostByIdArgs {
id: string;
data: z.infer<typeof schema>;
}
const editBeerPostById = ({
id,
data: { abv, ibu, name, description },
}: EditBeerPostByIdArgs): Promise<z.infer<typeof BeerPostQueryResult>> => {
return DBClient.instance.beerPost.update({
where: { id },
data: { abv, ibu, name, description },
select: {
id: true,
name: true,
description: true,
abv: true,
ibu: true,
createdAt: true,
updatedAt: true,
beerImages: { select: { id: true, path: true, caption: true, alt: true } },
brewery: { select: { id: true, name: true } },
style: { select: { id: true, name: true, description: true } },
postedBy: { select: { id: true, username: true } },
},
});
};
export default editBeerPostById;

View File

@@ -4,30 +4,33 @@ import { z } from 'zod';
const prisma = DBClient.instance;
const getAllBeerPosts = async (pageNum: number, pageSize: number) => {
const skip = (pageNum - 1) * pageSize;
interface GetAllBeerPostsArgs {
pageNum: number;
pageSize: number;
}
const beerPosts: z.infer<typeof BeerPostQueryResult>[] = await prisma.beerPost.findMany(
{
select: {
id: true,
name: true,
ibu: true,
abv: true,
description: true,
createdAt: true,
style: { select: { name: true, id: true, description: 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,
orderBy: { createdAt: 'desc' },
const getAllBeerPosts = ({
pageNum,
pageSize,
}: GetAllBeerPostsArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
return prisma.beerPost.findMany({
select: {
id: true,
name: true,
ibu: true,
abv: true,
description: true,
createdAt: true,
updatedAt: true,
style: { select: { name: true, id: true, description: true } },
brewery: { select: { name: true, id: true } },
postedBy: { select: { id: true, username: true } },
beerImages: { select: { path: true, caption: true, id: true, alt: true } },
},
);
return beerPosts;
take: pageSize,
skip: (pageNum - 1) * pageSize,
orderBy: { createdAt: 'desc' },
});
};
export default getAllBeerPosts;

View File

@@ -4,25 +4,25 @@ import { z } from 'zod';
const prisma = DBClient.instance;
const getBeerPostById = async (id: string) => {
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 } },
style: { select: { name: true, id: true, description: true } },
beerImages: { select: { alt: true, path: true, caption: true, id: true } },
},
where: { id },
});
return beerPost;
const getBeerPostById = async (
id: string,
): Promise<z.infer<typeof BeerPostQueryResult> | null> => {
return prisma.beerPost.findFirst({
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 } },
},
where: { id },
});
};
export default getBeerPostById;

View File

@@ -13,7 +13,10 @@ const getBeerRecommendations = async ({
beerPost,
pageNum,
pageSize,
}: GetBeerRecommendationsArgs) => {
}: GetBeerRecommendationsArgs): Promise<{
beerRecommendations: z.infer<typeof BeerPostQueryResult>[];
count: number;
}> => {
const skip = (pageNum - 1) * pageSize;
const take = pageSize;
@@ -30,6 +33,7 @@ const getBeerRecommendations = async ({
abv: true,
description: true,
createdAt: true,
updatedAt: true,
style: { select: { name: true, id: true, description: true } },
brewery: { select: { name: true, id: true } },
postedBy: { select: { id: true, username: true } },

View File

@@ -13,7 +13,7 @@ const BeerPostQueryResult = z.object({
style: z.object({ id: z.string(), name: z.string(), description: z.string() }),
postedBy: z.object({ id: z.string(), username: z.string() }),
createdAt: z.coerce.date(),
updatedAt: z.coerce.date().optional(),
updatedAt: z.coerce.date().nullable(),
});
export default BeerPostQueryResult;

View File

@@ -2,9 +2,15 @@ import { z } from 'zod';
import DBClient from '@/prisma/DBClient';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
const deleteBeerStyleById = async (id: string) => {
interface DeleteBeerStyleByIdArgs {
beerStyleId: string;
}
const deleteBeerStyleById = async ({
beerStyleId,
}: DeleteBeerStyleByIdArgs): Promise<z.infer<typeof BeerStyleQueryResult> | null> => {
const deleted = await DBClient.instance.beerStyle.delete({
where: { id },
where: { id: beerStyleId },
select: {
id: true,
name: true,
@@ -18,7 +24,11 @@ const deleteBeerStyleById = async (id: string) => {
},
});
return deleted as z.infer<typeof BeerStyleQueryResult> | null;
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return deleted as Awaited<ReturnType<typeof deleteBeerStyleById>>;
};
export default deleteBeerStyleById;

View File

@@ -0,0 +1,30 @@
import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
const editBeerStyleById = async (
id: string,
): Promise<z.infer<typeof BeerStyleQueryResult> | null> => {
const beerStyle = await DBClient.instance.beerStyle.findUnique({
where: { id },
select: {
id: true,
name: true,
postedBy: { select: { id: true, username: true } },
createdAt: true,
updatedAt: true,
abvRange: true,
ibuRange: true,
description: true,
glassware: { select: { id: true, name: true } },
},
});
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return beerStyle as Awaited<ReturnType<typeof editBeerStyleById>>;
};
export default editBeerStyleById;

View File

@@ -2,14 +2,16 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
interface GetAllBeerStylesArgs {
pageNum: number;
pageSize: number;
}
const getAllBeerStyles = async ({
pageNum,
pageSize,
}: {
pageNum: number;
pageSize: number;
}): Promise<z.infer<typeof BeerStyleQueryResult>[]> =>
DBClient.instance.beerStyle.findMany({
}: GetAllBeerStylesArgs): Promise<z.infer<typeof BeerStyleQueryResult>[]> => {
const beerStyles = await DBClient.instance.beerStyle.findMany({
take: pageSize,
skip: (pageNum - 1) * pageSize,
select: {
@@ -23,6 +25,13 @@ const getAllBeerStyles = async ({
description: true,
glassware: { select: { id: true, name: true } },
},
}) as ReturnType<typeof getAllBeerStyles>;
});
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return beerStyles as Awaited<ReturnType<typeof getAllBeerStyles>>;
};
export default getAllBeerStyles;

View File

@@ -2,8 +2,10 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
const getBeerStyleById = async (id: string) => {
const beerStyle = (await DBClient.instance.beerStyle.findUnique({
const getBeerStyleById = async (
id: string,
): Promise<z.infer<typeof BeerStyleQueryResult> | null> => {
const beerStyle = await DBClient.instance.beerStyle.findUnique({
where: { id },
select: {
id: true,
@@ -16,9 +18,13 @@ const getBeerStyleById = async (id: string) => {
description: true,
glassware: { select: { id: true, name: true } },
},
})) as z.infer<typeof BeerStyleQueryResult> | null;
});
return beerStyle;
/**
* Prisma does not support tuples, so we have to typecast the ibuRange and abvRange
* fields to [number, number] in order to satisfy the zod schema.
*/
return beerStyle as Awaited<ReturnType<typeof getBeerStyleById>>;
};
export default getBeerStyleById;

View File

@@ -0,0 +1,46 @@
import { z } from 'zod';
const CreateBeerStyleValidationSchema = z.object({
glasswareId: z
.string()
.cuid({
message: 'Glassware ID must be a valid CUID.',
})
.min(1, { message: 'Glassware ID is required.' }),
description: z
.string()
.min(1, { message: 'Description is required.' })
.max(500, { message: 'Description must be less than or equal to 500 characters.' }),
ibuRange: z
.tuple([
z
.number()
.min(0, { message: 'IBU range minimum must be greater than or equal to 0.' })
.max(100, { message: 'IBU range minimum must be less than or equal to 100.' }),
z
.number()
.min(0, { message: 'IBU range maximum must be greater than or equal to 0.' })
.max(100, { message: 'IBU range maximum must be less than or equal to 100.' }),
])
.refine((ibuRange) => ibuRange[0] <= ibuRange[1], {
message: 'IBU range minimum must be less than or equal to maximum.',
}),
abvRange: z
.tuple([
z
.number()
.min(0, { message: 'ABV range minimum must be greater than or equal to 0.' }),
z
.number()
.min(0, { message: 'ABV range maximum must be greater than or equal to 0.' }),
])
.refine((abvRange) => abvRange[0] <= abvRange[1], {
message: 'ABV range minimum must be less than or equal to maximum.',
}),
name: z
.string()
.min(1, { message: 'Name is required.' })
.max(100, { message: 'Name must be less than or equal to 100 characters.' }),
});
export default CreateBeerStyleValidationSchema;

View File

@@ -19,37 +19,37 @@ const createNewBreweryPost = async ({
locationId,
name,
userId,
}: z.infer<typeof CreateNewBreweryPostWithUserAndLocationSchema>) => {
const breweryPost: z.infer<typeof BreweryPostQueryResult> =
(await DBClient.instance.breweryPost.create({
data: {
name,
description,
dateEstablished,
location: { connect: { id: locationId } },
postedBy: { connect: { id: userId } },
},
select: {
id: true,
name: true,
description: true,
createdAt: true,
dateEstablished: true,
postedBy: { select: { id: true, username: true } },
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
}: z.infer<typeof CreateNewBreweryPostWithUserAndLocationSchema>): Promise<
z.infer<typeof BreweryPostQueryResult>
> => {
const post = (await DBClient.instance.breweryPost.create({
data: {
name,
description,
dateEstablished,
location: { connect: { id: locationId } },
postedBy: { connect: { id: userId } },
},
select: {
id: true,
name: true,
description: true,
createdAt: true,
dateEstablished: true,
postedBy: { select: { id: true, username: true } },
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
},
})) as z.infer<typeof BreweryPostQueryResult>;
},
})) as Awaited<ReturnType<typeof createNewBreweryPost>>;
return breweryPost;
return post;
};
export default createNewBreweryPost;

View File

@@ -5,36 +5,42 @@ import { z } from 'zod';
const prisma = DBClient.instance;
const getAllBreweryPosts = async (pageNum?: number, pageSize?: number) => {
const skip = pageNum && pageSize ? (pageNum - 1) * pageSize : undefined;
const take = pageNum && pageSize ? pageSize : undefined;
const breweryPosts: z.infer<typeof BreweryPostQueryResult>[] =
(await prisma.breweryPost.findMany({
skip,
take,
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
const getAllBreweryPosts = async ({
pageNum,
pageSize,
}: {
pageNum: number;
pageSize: number;
}): Promise<z.infer<typeof BreweryPostQueryResult>[]> => {
const breweryPosts = await prisma.breweryPost.findMany({
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,
dateEstablished: true,
},
orderBy: { createdAt: 'desc' },
})) as z.infer<typeof BreweryPostQueryResult>[];
description: true,
name: true,
postedBy: { select: { username: true, id: true } },
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
createdAt: true,
dateEstablished: true,
},
orderBy: { createdAt: 'desc' },
});
return breweryPosts;
/**
* 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 getAllBreweryPosts>>;
};
export default getAllBreweryPosts;

View File

@@ -5,30 +5,33 @@ import { z } from 'zod';
const prisma = DBClient.instance;
const getBreweryPostById = async (id: string) => {
const breweryPost: z.infer<typeof BreweryPostQueryResult> | null =
(await prisma.breweryPost.findFirst({
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
const breweryPost = await prisma.breweryPost.findFirst({
select: {
id: true,
location: {
select: {
city: true,
address: true,
coordinates: true,
country: true,
stateOrProvince: true,
},
description: true,
name: true,
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
postedBy: { select: { username: true, id: true } },
createdAt: true,
dateEstablished: true,
},
where: { id },
})) as z.infer<typeof BreweryPostQueryResult> | null;
description: true,
name: true,
breweryImages: { select: { path: true, caption: true, id: true, alt: true } },
postedBy: { select: { username: true, id: true } },
createdAt: true,
dateEstablished: true,
},
where: { id },
});
return breweryPost;
/**
* Prisma does not support tuples, so we have to typecast the coordinates field to
* [number, number] in order to satisfy the zod schema.
*/
return breweryPost as z.infer<typeof BreweryPostQueryResult> | null;
};
export default getBreweryPostById;