mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 02:39:03 +00:00
cleanup
This commit is contained in:
@@ -45,12 +45,7 @@ const editBeerPost = async (
|
||||
req: EditBeerPostRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const {
|
||||
body,
|
||||
query: { id },
|
||||
} = req;
|
||||
|
||||
await editBeerPostById(id, body);
|
||||
await editBeerPostById({ id: req.query.id, data: req.body });
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Beer post updated successfully',
|
||||
@@ -62,8 +57,7 @@ const editBeerPost = async (
|
||||
const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
||||
const { id } = req.query;
|
||||
|
||||
const deleted = deleteBeerPostById(id);
|
||||
|
||||
const deleted = deleteBeerPostById({ beerPostId: id });
|
||||
if (!deleted) {
|
||||
throw new ServerError('Beer post not found', 404);
|
||||
}
|
||||
@@ -74,26 +68,28 @@ const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
||||
statusCode: 200,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
EditBeerPostRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.put(
|
||||
validateRequest({
|
||||
bodySchema: EditBeerPostValidationSchema,
|
||||
querySchema: z.object({ id: z.string() }),
|
||||
}),
|
||||
getCurrentUser,
|
||||
checkIfBeerPostOwner,
|
||||
editBeerPost,
|
||||
);
|
||||
router.delete(
|
||||
validateRequest({ querySchema: z.object({ id: z.string() }) }),
|
||||
getCurrentUser,
|
||||
checkIfBeerPostOwner,
|
||||
deleteBeerPost,
|
||||
);
|
||||
router
|
||||
.put(
|
||||
validateRequest({
|
||||
bodySchema: EditBeerPostValidationSchema,
|
||||
querySchema: z.object({ id: z.string() }),
|
||||
}),
|
||||
getCurrentUser,
|
||||
checkIfBeerPostOwner,
|
||||
editBeerPost,
|
||||
)
|
||||
.delete(
|
||||
validateRequest({ querySchema: z.object({ id: z.string() }) }),
|
||||
getCurrentUser,
|
||||
checkIfBeerPostOwner,
|
||||
deleteBeerPost,
|
||||
);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ const getBeerPosts = async (
|
||||
const pageNum = parseInt(req.query.page_num, 10);
|
||||
const pageSize = parseInt(req.query.page_size, 10);
|
||||
|
||||
const beerPosts = await getAllBeerPosts(pageNum, pageSize);
|
||||
const beerPosts = await getAllBeerPosts({ pageNum, pageSize });
|
||||
const beerPostCount = await DBClient.instance.beerPost.count();
|
||||
|
||||
res.setHeader('X-Total-Count', beerPostCount);
|
||||
|
||||
@@ -26,6 +26,7 @@ const search = async (req: SearchAPIRequest, res: NextApiResponse) => {
|
||||
ibu: true,
|
||||
abv: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
description: true,
|
||||
postedBy: { select: { username: true, id: true } },
|
||||
brewery: { select: { name: true, id: true } },
|
||||
|
||||
@@ -29,6 +29,7 @@ const getAllBeersByBrewery = async (
|
||||
ibu: true,
|
||||
abv: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
description: true,
|
||||
postedBy: { select: { username: true, id: true } },
|
||||
brewery: { select: { name: true, id: true } },
|
||||
|
||||
@@ -19,7 +19,7 @@ const getBreweryPosts = async (
|
||||
const pageNum = parseInt(req.query.page_num, 10);
|
||||
const pageSize = parseInt(req.query.page_size, 10);
|
||||
|
||||
const breweryPosts = await getAllBreweryPosts(pageNum, pageSize);
|
||||
const breweryPosts = await getAllBreweryPosts({ pageNum, pageSize });
|
||||
const breweryPostCount = await DBClient.instance.breweryPost.count();
|
||||
|
||||
res.setHeader('X-Total-Count', breweryPostCount);
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const CreateCommentValidationSchema = z.object({
|
||||
userId: z.string().uuid(),
|
||||
beerPostId: z.string().uuid(),
|
||||
});
|
||||
|
||||
export default CreateCommentValidationSchema;
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 } },
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
30
src/services/BeerStyles/editBeerStyleById.ts
Normal file
30
src/services/BeerStyles/editBeerStyleById.ts
Normal 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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user