mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor: update beer post services, comment services
This commit is contained in:
@@ -11,7 +11,6 @@ import {
|
|||||||
createBeerPostCommentService,
|
createBeerPostCommentService,
|
||||||
getAllBeerCommentsService,
|
getAllBeerCommentsService,
|
||||||
deleteBeerCommentByIdService,
|
deleteBeerCommentByIdService,
|
||||||
getBeerPostCommentCountService,
|
|
||||||
} from '@/services/comments/beer-comment';
|
} from '@/services/comments/beer-comment';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -98,14 +97,12 @@ export const getAllBeerPostComments = async (
|
|||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const { page_size, page_num } = req.query;
|
const { page_size, page_num } = req.query;
|
||||||
|
|
||||||
const comments = await getAllBeerCommentsService({
|
const { comments, count } = await getAllBeerCommentsService({
|
||||||
beerPostId,
|
beerPostId,
|
||||||
pageNum: parseInt(page_num, 10),
|
pageNum: parseInt(page_num, 10),
|
||||||
pageSize: parseInt(page_size, 10),
|
pageSize: parseInt(page_size, 10),
|
||||||
});
|
});
|
||||||
|
|
||||||
const count = await getBeerPostCommentCountService({ beerPostId });
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import ServerError from '@/config/util/ServerError';
|
import ServerError from '@/config/util/ServerError';
|
||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { NextHandler } from 'next-connect';
|
import { NextHandler } from 'next-connect';
|
||||||
@@ -103,16 +101,12 @@ export const getAll = async (
|
|||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const { page_size, page_num } = req.query;
|
const { page_size, page_num } = req.query;
|
||||||
|
|
||||||
const comments = await getAllBeerStyleComments({
|
const { comments, count } = await getAllBeerStyleComments({
|
||||||
beerStyleId,
|
beerStyleId,
|
||||||
pageNum: parseInt(page_num, 10),
|
pageNum: parseInt(page_num, 10),
|
||||||
pageSize: parseInt(page_size, 10),
|
pageSize: parseInt(page_size, 10),
|
||||||
});
|
});
|
||||||
|
|
||||||
const count = await DBClient.instance.beerStyleComment.count({
|
|
||||||
where: { beerStyleId },
|
|
||||||
});
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import {
|
|||||||
getAllBreweryComments,
|
getAllBreweryComments,
|
||||||
deleteBreweryCommentByIdService,
|
deleteBreweryCommentByIdService,
|
||||||
updateBreweryCommentById,
|
updateBreweryCommentById,
|
||||||
getBreweryCommentCount,
|
|
||||||
} from '@/services/comments/brewery-comment';
|
} from '@/services/comments/brewery-comment';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -70,7 +69,7 @@ export const deleteBreweryPostComment = async (
|
|||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Comment deleted successfully',
|
message: 'Brewery comment deleted successfully',
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -90,7 +89,7 @@ export const createComment = async (
|
|||||||
});
|
});
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
message: 'Beer comment created successfully',
|
message: 'Brewery comment created successfully',
|
||||||
statusCode: 201,
|
statusCode: 201,
|
||||||
payload: newBreweryComment,
|
payload: newBreweryComment,
|
||||||
success: true,
|
success: true,
|
||||||
@@ -105,18 +104,16 @@ export const getAll = async (
|
|||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const { page_size, page_num } = req.query;
|
const { page_size, page_num } = req.query;
|
||||||
|
|
||||||
const comments = await getAllBreweryComments({
|
const { comments, count } = await getAllBreweryComments({
|
||||||
id: breweryPostId,
|
id: breweryPostId,
|
||||||
pageNum: parseInt(page_num, 10),
|
pageNum: parseInt(page_num, 10),
|
||||||
pageSize: parseInt(page_size, 10),
|
pageSize: parseInt(page_size, 10),
|
||||||
});
|
});
|
||||||
|
|
||||||
const count = await getBreweryCommentCount({ breweryPostId });
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', count);
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Beer comments fetched successfully',
|
message: 'Brewery comments fetched successfully',
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
payload: comments,
|
payload: comments,
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||||
import ServerError from '@/config/util/ServerError';
|
import ServerError from '@/config/util/ServerError';
|
||||||
import getBeerPostById from '@/services/posts/beer-post/getBeerPostById';
|
|
||||||
import createBeerPostLike from '@/services/likes/beer-post-like/createBeerPostLike';
|
|
||||||
import findBeerPostLikeById from '@/services/likes/beer-post-like/findBeerPostLikeById';
|
|
||||||
import getBeerPostLikeCountByBeerPostId from '@/services/likes/beer-post-like/getBeerPostLikeCount';
|
|
||||||
import removeBeerPostLikeById from '@/services/likes/beer-post-like/removeBeerPostLikeById';
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiResponse, NextApiRequest } from 'next';
|
import { NextApiResponse, NextApiRequest } from 'next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { LikeRequest } from '../types';
|
import { LikeRequest } from '../types';
|
||||||
|
import createBeerPostLike from '@/services/likes/beer-post-like/createBeerPostLike';
|
||||||
|
import findBeerPostLikeById from '@/services/likes/beer-post-like/findBeerPostLikeById';
|
||||||
|
import getBeerPostLikeCountByBeerPostId from '@/services/likes/beer-post-like/getBeerPostLikeCount';
|
||||||
|
import removeBeerPostLikeById from '@/services/likes/beer-post-like/removeBeerPostLikeById';
|
||||||
|
import { getBeerPostById } from '@/services/posts/beer-post';
|
||||||
|
|
||||||
export const sendBeerPostLikeRequest = async (
|
export const sendBeerPostLikeRequest = async (
|
||||||
req: LikeRequest,
|
req: LikeRequest,
|
||||||
@@ -17,7 +18,7 @@ export const sendBeerPostLikeRequest = async (
|
|||||||
const user = req.user!;
|
const user = req.user!;
|
||||||
const id = req.query.id as string;
|
const id = req.query.id as string;
|
||||||
|
|
||||||
const beer = await getBeerPostById(id);
|
const beer = await getBeerPostById({ beerPostId: id });
|
||||||
if (!beer) {
|
if (!beer) {
|
||||||
throw new ServerError('Could not find a beer post with that id', 404);
|
throw new ServerError('Could not find a beer post with that id', 404);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
import ServerError from '@/config/util/ServerError';
|
import ServerError from '@/config/util/ServerError';
|
||||||
import deleteBeerPostById from '@/services/posts/beer-post/deleteBeerPostById';
|
|
||||||
import editBeerPostById from '@/services/posts/beer-post/editBeerPostById';
|
import {
|
||||||
import getBeerPostById from '@/services/posts/beer-post/getBeerPostById';
|
getBeerPostById,
|
||||||
|
editBeerPostByIdService,
|
||||||
|
deleteBeerPostByIdService,
|
||||||
|
getBeerRecommendationsService,
|
||||||
|
getAllBeerPostsService,
|
||||||
|
createNewBeerPost,
|
||||||
|
getBeerPostsByPostedByIdService,
|
||||||
|
} from '@/services/posts/beer-post';
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { NextHandler } from 'next-connect';
|
import { NextHandler } from 'next-connect';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import getBeerRecommendations from '@/services/posts/beer-post/getBeerRecommendations';
|
|
||||||
import getAllBeerPosts from '@/services/posts/beer-post/getAllBeerPosts';
|
|
||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import createNewBeerPost from '@/services/posts/beer-post/createNewBeerPost';
|
|
||||||
import getBeerPostsByPostedById from '@/services/posts/beer-post/getBeerPostsByPostedById';
|
|
||||||
import {
|
import {
|
||||||
BeerPostRequest,
|
BeerPostRequest,
|
||||||
CreateBeerPostRequest,
|
CreateBeerPostRequest,
|
||||||
@@ -28,7 +30,7 @@ export const checkIfBeerPostOwner = async <BeerPostRequestType extends BeerPostR
|
|||||||
const { user, query } = req;
|
const { user, query } = req;
|
||||||
const { id } = query;
|
const { id } = query;
|
||||||
|
|
||||||
const beerPost = await getBeerPostById(id);
|
const beerPost = await getBeerPostById({ beerPostId: id });
|
||||||
|
|
||||||
if (!beerPost) {
|
if (!beerPost) {
|
||||||
throw new ServerError('Beer post not found', 404);
|
throw new ServerError('Beer post not found', 404);
|
||||||
@@ -45,7 +47,7 @@ export const editBeerPost = async (
|
|||||||
req: EditBeerPostRequest,
|
req: EditBeerPostRequest,
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
) => {
|
) => {
|
||||||
await editBeerPostById({ id: req.query.id, data: req.body });
|
await editBeerPostByIdService({ beerPostId: req.query.id, body: req.body });
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Beer post updated successfully',
|
message: 'Beer post updated successfully',
|
||||||
@@ -57,7 +59,7 @@ export const editBeerPost = async (
|
|||||||
export const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
export const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => {
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
|
|
||||||
const deleted = await deleteBeerPostById({ beerPostId: id });
|
const deleted = await deleteBeerPostByIdService({ beerPostId: id });
|
||||||
if (!deleted) {
|
if (!deleted) {
|
||||||
throw new ServerError('Beer post not found', 404);
|
throw new ServerError('Beer post not found', 404);
|
||||||
}
|
}
|
||||||
@@ -75,7 +77,7 @@ export const getBeerPostRecommendations = async (
|
|||||||
) => {
|
) => {
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
|
|
||||||
const beerPost = await getBeerPostById(id);
|
const beerPost = await getBeerPostById({ beerPostId: id });
|
||||||
|
|
||||||
if (!beerPost) {
|
if (!beerPost) {
|
||||||
throw new ServerError('Beer post not found', 404);
|
throw new ServerError('Beer post not found', 404);
|
||||||
@@ -84,7 +86,7 @@ export const getBeerPostRecommendations = async (
|
|||||||
const pageNum = parseInt(req.query.page_num as string, 10);
|
const pageNum = parseInt(req.query.page_num as string, 10);
|
||||||
const pageSize = parseInt(req.query.page_size as string, 10);
|
const pageSize = parseInt(req.query.page_size as string, 10);
|
||||||
|
|
||||||
const { count, beerRecommendations } = await getBeerRecommendations({
|
const { beerRecommendations, count } = await getBeerRecommendationsService({
|
||||||
beerPost,
|
beerPost,
|
||||||
pageNum,
|
pageNum,
|
||||||
pageSize,
|
pageSize,
|
||||||
@@ -106,11 +108,9 @@ export const getBeerPosts = async (
|
|||||||
const pageNum = parseInt(req.query.page_num, 10);
|
const pageNum = parseInt(req.query.page_num, 10);
|
||||||
const pageSize = parseInt(req.query.page_size, 10);
|
const pageSize = parseInt(req.query.page_size, 10);
|
||||||
|
|
||||||
const beerPosts = await getAllBeerPosts({ pageNum, pageSize });
|
const { beerPosts, count } = await getAllBeerPostsService({ pageNum, pageSize });
|
||||||
|
|
||||||
const beerPostCount = await DBClient.instance.beerPost.count();
|
res.setHeader('X-Total-Count', count);
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', beerPostCount);
|
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Beer posts retrieved successfully',
|
message: 'Beer posts retrieved successfully',
|
||||||
@@ -124,14 +124,14 @@ export const createBeerPost = async (
|
|||||||
req: CreateBeerPostRequest,
|
req: CreateBeerPostRequest,
|
||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
) => {
|
) => {
|
||||||
const { name, description, styleId: typeId, abv, ibu, breweryId } = req.body;
|
const { name, description, styleId, abv, ibu, breweryId } = req.body;
|
||||||
|
|
||||||
const newBeerPost = await createNewBeerPost({
|
const newBeerPost = await createNewBeerPost({
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
abv,
|
abv,
|
||||||
ibu,
|
ibu,
|
||||||
styleId: typeId,
|
styleId,
|
||||||
breweryId,
|
breweryId,
|
||||||
userId: req.user!.id,
|
userId: req.user!.id,
|
||||||
});
|
});
|
||||||
@@ -153,17 +153,13 @@ export const getBeerPostsByUserId = async (
|
|||||||
|
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
|
|
||||||
const beerPosts = await getBeerPostsByPostedById({
|
const { beerPosts, count } = await getBeerPostsByPostedByIdService({
|
||||||
pageNum,
|
pageNum,
|
||||||
pageSize,
|
pageSize,
|
||||||
postedById: id,
|
postedById: id,
|
||||||
});
|
});
|
||||||
|
|
||||||
const beerPostCount = await DBClient.instance.beerPost.count({
|
res.setHeader('X-Total-Count', count);
|
||||||
where: { postedBy: { id } },
|
|
||||||
});
|
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', beerPostCount);
|
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: `Beer posts by user ${id} fetched successfully`,
|
message: `Beer posts by user ${id} fetched successfully`,
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import DBClient from '@/prisma/DBClient';
|
|||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
|
||||||
import getBeerStyleById from '@/services/posts/beer-style-post/getBeerStyleById';
|
import getBeerStyleById from '@/services/posts/beer-style-post/getBeerStyleById';
|
||||||
import getBeerPostsByBeerStyleId from '@/services/posts/beer-post/getBeerPostsByBeerStyleId';
|
|
||||||
import getAllBeerStyles from '@/services/posts/beer-style-post/getAllBeerStyles';
|
import getAllBeerStyles from '@/services/posts/beer-style-post/getAllBeerStyles';
|
||||||
|
|
||||||
import ServerError from '@/config/util/ServerError';
|
import ServerError from '@/config/util/ServerError';
|
||||||
|
|
||||||
import { CreateBeerStyleRequest, GetBeerStyleByIdRequest } from './types';
|
import { CreateBeerStyleRequest, GetBeerStyleByIdRequest } from './types';
|
||||||
import { GetAllPostsByConnectedPostId, GetAllPostsRequest } from '../types';
|
import { GetAllPostsByConnectedPostId, GetAllPostsRequest } from '../types';
|
||||||
|
import { getBeerPostsByBeerStyleIdService } from '@/services/posts/beer-post';
|
||||||
|
|
||||||
export const getBeerStyle = async (
|
export const getBeerStyle = async (
|
||||||
req: GetBeerStyleByIdRequest,
|
req: GetBeerStyleByIdRequest,
|
||||||
@@ -36,7 +37,7 @@ export const getAllBeersByBeerStyle = async (
|
|||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const { page_size, page_num, id } = req.query;
|
const { page_size, page_num, id } = req.query;
|
||||||
|
|
||||||
const beers = await getBeerPostsByBeerStyleId({
|
const beers = await getBeerPostsByBeerStyleIdService({
|
||||||
pageNum: parseInt(page_num, 10),
|
pageNum: parseInt(page_num, 10),
|
||||||
pageSize: parseInt(page_size, 10),
|
pageSize: parseInt(page_size, 10),
|
||||||
styleId: id,
|
styleId: id,
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ import Head from 'next/head';
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import withPageAuthRequired from '@/util/withPageAuthRequired';
|
import withPageAuthRequired from '@/util/withPageAuthRequired';
|
||||||
import getBeerPostById from '@/services/posts/beer-post/getBeerPostById';
|
|
||||||
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
||||||
import EditBeerPostForm from '@/components/EditBeerPostForm';
|
import EditBeerPostForm from '@/components/EditBeerPostForm';
|
||||||
import FormPageLayout from '@/components/ui/forms/FormPageLayout';
|
import FormPageLayout from '@/components/ui/forms/FormPageLayout';
|
||||||
import { BiBeer } from 'react-icons/bi';
|
import { BiBeer } from 'react-icons/bi';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
import { getBeerPostById } from '@/services/posts/beer-post';
|
||||||
|
|
||||||
interface EditPageProps {
|
interface EditPageProps {
|
||||||
beerPost: z.infer<typeof BeerPostQueryResult>;
|
beerPost: z.infer<typeof BeerPostQueryResult>;
|
||||||
@@ -37,7 +37,6 @@ const EditBeerPostPage: NextPage<EditPageProps> = ({ beerPost }) => {
|
|||||||
ibu: beerPost.ibu,
|
ibu: beerPost.ibu,
|
||||||
description: beerPost.description,
|
description: beerPost.description,
|
||||||
id: beerPost.id,
|
id: beerPost.id,
|
||||||
styleId: beerPost.style.id,
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</FormPageLayout>
|
</FormPageLayout>
|
||||||
@@ -50,7 +49,7 @@ export default EditBeerPostPage;
|
|||||||
export const getServerSideProps = withPageAuthRequired<EditPageProps>(
|
export const getServerSideProps = withPageAuthRequired<EditPageProps>(
|
||||||
async (context, session) => {
|
async (context, session) => {
|
||||||
const beerPostId = context.params?.id as string;
|
const beerPostId = context.params?.id as string;
|
||||||
const beerPost = await getBeerPostById(beerPostId);
|
const beerPost = await getBeerPostById({ beerPostId });
|
||||||
const { id: userId } = session;
|
const { id: userId } = session;
|
||||||
|
|
||||||
if (!beerPost) {
|
if (!beerPost) {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { NextPage, GetServerSideProps } from 'next';
|
import { NextPage, GetServerSideProps } from 'next';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
import getBeerPostById from '@/services/posts/beer-post/getBeerPostById';
|
|
||||||
|
|
||||||
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
||||||
|
|
||||||
@@ -12,6 +11,7 @@ import useMediaQuery from '@/hooks/utilities/useMediaQuery';
|
|||||||
import { Tab } from '@headlessui/react';
|
import { Tab } from '@headlessui/react';
|
||||||
import dynamic from 'next/dynamic';
|
import dynamic from 'next/dynamic';
|
||||||
import { CldImage } from 'next-cloudinary';
|
import { CldImage } from 'next-cloudinary';
|
||||||
|
import { getBeerPostById } from '@/services/posts/beer-post';
|
||||||
|
|
||||||
const [BeerInfoHeader, BeerPostCommentsSection, BeerRecommendations] = [
|
const [BeerInfoHeader, BeerPostCommentsSection, BeerRecommendations] = [
|
||||||
dynamic(() => import('@/components/BeerById/BeerInfoHeader')),
|
dynamic(() => import('@/components/BeerById/BeerInfoHeader')),
|
||||||
@@ -100,7 +100,9 @@ const BeerByIdPage: NextPage<BeerPageProps> = ({ beerPost }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps<BeerPageProps> = async (context) => {
|
export const getServerSideProps: GetServerSideProps<BeerPageProps> = async (context) => {
|
||||||
const beerPost = await getBeerPostById(context.params!.id! as string);
|
const beerPost = await getBeerPostById({
|
||||||
|
beerPostId: context.params?.id as string,
|
||||||
|
});
|
||||||
|
|
||||||
if (!beerPost) {
|
if (!beerPost) {
|
||||||
return { notFound: true };
|
return { notFound: true };
|
||||||
|
|||||||
@@ -20,12 +20,11 @@ const sendEditBeerPostRequest = async ({
|
|||||||
ibu,
|
ibu,
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
styleId,
|
|
||||||
}: z.infer<typeof EditBeerPostValidationSchema>) => {
|
}: z.infer<typeof EditBeerPostValidationSchema>) => {
|
||||||
const response = await fetch(`/api/beers/${id}`, {
|
const response = await fetch(`/api/beers/${id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ abv, description, ibu, name, styleId, id }),
|
body: JSON.stringify({ abv, description, ibu, name, id }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {
|
|||||||
EditBeerPostCommentById,
|
EditBeerPostCommentById,
|
||||||
FindOrDeleteBeerPostCommentById,
|
FindOrDeleteBeerPostCommentById,
|
||||||
GetAllBeerPostComments,
|
GetAllBeerPostComments,
|
||||||
GetBeerPostCommentCount,
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
const beerPostCommentSelect = {
|
const beerPostCommentSelect = {
|
||||||
@@ -66,22 +65,20 @@ export const deleteBeerCommentByIdService: FindOrDeleteBeerPostCommentById = ({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAllBeerCommentsService: GetAllBeerPostComments = ({
|
export const getAllBeerCommentsService: GetAllBeerPostComments = async ({
|
||||||
beerPostId,
|
beerPostId,
|
||||||
pageNum,
|
pageNum,
|
||||||
pageSize,
|
pageSize,
|
||||||
}) => {
|
}) => {
|
||||||
return DBClient.instance.beerComment.findMany({
|
const comments = await DBClient.instance.beerComment.findMany({
|
||||||
skip: (pageNum - 1) * pageSize,
|
skip: (pageNum - 1) * pageSize,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
where: { beerPostId },
|
where: { beerPostId },
|
||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
select: beerPostCommentSelect,
|
select: beerPostCommentSelect,
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
export const getBeerPostCommentCountService: GetBeerPostCommentCount = ({
|
const count = await DBClient.instance.beerComment.count({ where: { beerPostId } });
|
||||||
beerPostId,
|
|
||||||
}) => {
|
return { comments, count };
|
||||||
return DBClient.instance.beerComment.count({ where: { beerPostId } });
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,10 +19,11 @@ export type FindOrDeleteBeerPostCommentById = (args: {
|
|||||||
beerPostCommentId: string;
|
beerPostCommentId: string;
|
||||||
}) => Promise<BeerPostComment | null>;
|
}) => Promise<BeerPostComment | null>;
|
||||||
|
|
||||||
export type GetBeerPostCommentCount = (args: { beerPostId: string }) => Promise<number>;
|
|
||||||
|
|
||||||
export type GetAllBeerPostComments = (args: {
|
export type GetAllBeerPostComments = (args: {
|
||||||
beerPostId: string;
|
beerPostId: string;
|
||||||
pageNum: number;
|
pageNum: number;
|
||||||
pageSize: number;
|
pageSize: number;
|
||||||
}) => Promise<BeerPostComment[]>;
|
}) => Promise<{
|
||||||
|
comments: BeerPostComment[];
|
||||||
|
count: number;
|
||||||
|
}>;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import DBClient from '@/prisma/DBClient';
|
|||||||
import {
|
import {
|
||||||
CreateNewBeerStyleComment,
|
CreateNewBeerStyleComment,
|
||||||
GetAllBeerStyleComments,
|
GetAllBeerStyleComments,
|
||||||
GetBeerStyleCommentCount,
|
|
||||||
UpdateBeerStyleCommentById,
|
UpdateBeerStyleCommentById,
|
||||||
FindOrDeleteBeerStyleCommentById,
|
FindOrDeleteBeerStyleCommentById,
|
||||||
} from './types';
|
} from './types';
|
||||||
@@ -35,22 +34,24 @@ export const createNewBeerStyleComment: CreateNewBeerStyleComment = ({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAllBeerStyleComments: GetAllBeerStyleComments = ({
|
export const getAllBeerStyleComments: GetAllBeerStyleComments = async ({
|
||||||
beerStyleId,
|
beerStyleId,
|
||||||
pageNum,
|
pageNum,
|
||||||
pageSize,
|
pageSize,
|
||||||
}) => {
|
}) => {
|
||||||
return DBClient.instance.beerStyleComment.findMany({
|
const comments = await DBClient.instance.beerStyleComment.findMany({
|
||||||
skip: (pageNum - 1) * pageSize,
|
skip: (pageNum - 1) * pageSize,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
where: { beerStyleId },
|
where: { beerStyleId },
|
||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
select: beerStyleCommentSelect,
|
select: beerStyleCommentSelect,
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
export const getBeerStyleCommentCount: GetBeerStyleCommentCount = ({ beerStyleId }) => {
|
const count = await DBClient.instance.beerStyleComment.count({
|
||||||
return DBClient.instance.beerStyleComment.count({ where: { beerStyleId } });
|
where: { beerStyleId },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { comments, count };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateBeerStyleCommentById: UpdateBeerStyleCommentById = ({
|
export const updateBeerStyleCommentById: UpdateBeerStyleCommentById = ({
|
||||||
|
|||||||
@@ -13,13 +13,14 @@ export type UpdateBeerStyleCommentById = (args: {
|
|||||||
beerStyleCommentId: string;
|
beerStyleCommentId: string;
|
||||||
}) => Promise<BeerStyleComment>;
|
}) => Promise<BeerStyleComment>;
|
||||||
|
|
||||||
export type GetBeerStyleCommentCount = (args: { beerStyleId: string }) => Promise<number>;
|
|
||||||
|
|
||||||
export type GetAllBeerStyleComments = (args: {
|
export type GetAllBeerStyleComments = (args: {
|
||||||
beerStyleId: string;
|
beerStyleId: string;
|
||||||
pageNum: number;
|
pageNum: number;
|
||||||
pageSize: number;
|
pageSize: number;
|
||||||
}) => Promise<BeerStyleComment[]>;
|
}) => Promise<{
|
||||||
|
comments: BeerStyleComment[];
|
||||||
|
count: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
export type CreateNewBeerStyleComment = (args: {
|
export type CreateNewBeerStyleComment = (args: {
|
||||||
body: z.infer<typeof CreateCommentValidationSchema>;
|
body: z.infer<typeof CreateCommentValidationSchema>;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import {
|
|||||||
CreateNewBreweryComment,
|
CreateNewBreweryComment,
|
||||||
FindDeleteBreweryCommentById,
|
FindDeleteBreweryCommentById,
|
||||||
GetAllBreweryComments,
|
GetAllBreweryComments,
|
||||||
GetBreweryCommentCount,
|
|
||||||
UpdateBreweryCommentById,
|
UpdateBreweryCommentById,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
@@ -48,18 +47,24 @@ export const createNewBreweryComment: CreateNewBreweryComment = ({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAllBreweryComments: GetAllBreweryComments = ({
|
export const getAllBreweryComments: GetAllBreweryComments = async ({
|
||||||
id,
|
id,
|
||||||
pageNum,
|
pageNum,
|
||||||
pageSize,
|
pageSize,
|
||||||
}) => {
|
}) => {
|
||||||
return DBClient.instance.breweryComment.findMany({
|
const comments = await DBClient.instance.breweryComment.findMany({
|
||||||
skip: (pageNum - 1) * pageSize,
|
skip: (pageNum - 1) * pageSize,
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
where: { breweryPostId: id },
|
where: { breweryPostId: id },
|
||||||
select: breweryCommentSelect,
|
select: breweryCommentSelect,
|
||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.breweryComment.count({
|
||||||
|
where: { breweryPostId: id },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { comments, count };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBreweryCommentById: FindDeleteBreweryCommentById = ({
|
export const getBreweryCommentById: FindDeleteBreweryCommentById = ({
|
||||||
@@ -79,7 +84,3 @@ export const deleteBreweryCommentByIdService: FindDeleteBreweryCommentById = ({
|
|||||||
select: breweryCommentSelect,
|
select: breweryCommentSelect,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBreweryCommentCount: GetBreweryCommentCount = ({ breweryPostId }) => {
|
|
||||||
return DBClient.instance.breweryComment.count({ where: { breweryPostId } });
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -20,10 +20,8 @@ export type GetAllBreweryComments = (args: {
|
|||||||
id: string;
|
id: string;
|
||||||
pageNum: number;
|
pageNum: number;
|
||||||
pageSize: number;
|
pageSize: number;
|
||||||
}) => Promise<BreweryComment[]>;
|
}) => Promise<{ comments: BreweryComment[]; count: number }>;
|
||||||
|
|
||||||
export type FindDeleteBreweryCommentById = (args: {
|
export type FindDeleteBreweryCommentById = (args: {
|
||||||
breweryCommentId: string;
|
breweryCommentId: string;
|
||||||
}) => Promise<BreweryComment | null>;
|
}) => Promise<BreweryComment | null>;
|
||||||
|
|
||||||
export type GetBreweryCommentCount = (args: { breweryPostId: string }) => Promise<number>;
|
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
|
||||||
import CreateBeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
|
|
||||||
|
|
||||||
const CreateBeerPostWithUserSchema = CreateBeerPostValidationSchema.extend({
|
|
||||||
userId: z.string().cuid(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const createNewBeerPost = ({
|
|
||||||
name,
|
|
||||||
description,
|
|
||||||
abv,
|
|
||||||
ibu,
|
|
||||||
styleId,
|
|
||||||
breweryId,
|
|
||||||
userId,
|
|
||||||
}: 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: {
|
|
||||||
alt: true,
|
|
||||||
path: true,
|
|
||||||
caption: true,
|
|
||||||
id: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
brewery: { select: { id: true, name: true } },
|
|
||||||
style: { select: { id: true, name: true, description: true } },
|
|
||||||
postedBy: { select: { id: true, username: true } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default createNewBeerPost;
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
|
||||||
|
|
||||||
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,
|
|
||||||
updatedAt: true,
|
|
||||||
beerImages: {
|
|
||||||
select: {
|
|
||||||
alt: true,
|
|
||||||
path: true,
|
|
||||||
caption: true,
|
|
||||||
id: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
style: { select: { id: true, name: true, description: true } },
|
|
||||||
postedBy: { select: { id: true, username: true } },
|
|
||||||
brewery: { select: { id: true, name: true } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default deleteBeerPostById;
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
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, styleId: true });
|
|
||||||
|
|
||||||
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: {
|
|
||||||
alt: true,
|
|
||||||
path: true,
|
|
||||||
caption: true,
|
|
||||||
id: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
brewery: { select: { id: true, name: true } },
|
|
||||||
style: { select: { id: true, name: true, description: true } },
|
|
||||||
postedBy: { select: { id: true, username: true } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default editBeerPostById;
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
|
||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
const prisma = DBClient.instance;
|
|
||||||
|
|
||||||
interface GetAllBeerPostsArgs {
|
|
||||||
pageNum: number;
|
|
||||||
pageSize: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
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: {
|
|
||||||
alt: true,
|
|
||||||
path: true,
|
|
||||||
caption: true,
|
|
||||||
id: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
take: pageSize,
|
|
||||||
skip: (pageNum - 1) * pageSize,
|
|
||||||
orderBy: { createdAt: 'desc' },
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getAllBeerPosts;
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
|
||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
const prisma = DBClient.instance;
|
|
||||||
|
|
||||||
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,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
where: { id },
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getBeerPostById;
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
|
||||||
|
|
||||||
interface GetBeerPostsByBeerStyleIdArgs {
|
|
||||||
styleId: string;
|
|
||||||
pageSize: number;
|
|
||||||
pageNum: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getBeerPostsByBeerStyleId = async ({
|
|
||||||
pageNum,
|
|
||||||
pageSize,
|
|
||||||
styleId,
|
|
||||||
}: GetBeerPostsByBeerStyleIdArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
|
|
||||||
const beers = await DBClient.instance.beerPost.findMany({
|
|
||||||
where: { styleId },
|
|
||||||
take: pageSize,
|
|
||||||
skip: (pageNum - 1) * pageSize,
|
|
||||||
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,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return beers;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getBeerPostsByBeerStyleId;
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
|
||||||
|
|
||||||
interface GetBeerPostsByBeerStyleIdArgs {
|
|
||||||
breweryId: string;
|
|
||||||
pageSize: number;
|
|
||||||
pageNum: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getAllBeerPostsByBreweryId = async ({
|
|
||||||
pageNum,
|
|
||||||
pageSize,
|
|
||||||
breweryId,
|
|
||||||
}: GetBeerPostsByBeerStyleIdArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
|
|
||||||
const beers = await DBClient.instance.beerPost.findMany({
|
|
||||||
where: { breweryId },
|
|
||||||
take: pageSize,
|
|
||||||
skip: (pageNum - 1) * pageSize,
|
|
||||||
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,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return beers;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getAllBeerPostsByBreweryId;
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import BeerPostQueryResult from './schema/BeerPostQueryResult';
|
|
||||||
|
|
||||||
interface GetBeerPostsByBeerStyleIdArgs {
|
|
||||||
postedById: string;
|
|
||||||
pageSize: number;
|
|
||||||
pageNum: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getBeerPostsByPostedById = async ({
|
|
||||||
pageNum,
|
|
||||||
pageSize,
|
|
||||||
postedById,
|
|
||||||
}: GetBeerPostsByBeerStyleIdArgs): Promise<z.infer<typeof BeerPostQueryResult>[]> => {
|
|
||||||
const beers = await DBClient.instance.beerPost.findMany({
|
|
||||||
where: { postedBy: { id: postedById } },
|
|
||||||
take: pageSize,
|
|
||||||
skip: (pageNum - 1) * pageSize,
|
|
||||||
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,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return beers;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getBeerPostsByPostedById;
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
|
||||||
import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult';
|
|
||||||
|
|
||||||
import { z } from 'zod';
|
|
||||||
|
|
||||||
interface GetBeerRecommendationsArgs {
|
|
||||||
beerPost: z.infer<typeof BeerPostQueryResult>;
|
|
||||||
pageNum: number;
|
|
||||||
pageSize: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const getBeerRecommendations = async ({
|
|
||||||
beerPost,
|
|
||||||
pageNum,
|
|
||||||
pageSize,
|
|
||||||
}: GetBeerRecommendationsArgs): Promise<{
|
|
||||||
beerRecommendations: z.infer<typeof BeerPostQueryResult>[];
|
|
||||||
count: number;
|
|
||||||
}> => {
|
|
||||||
const skip = (pageNum - 1) * pageSize;
|
|
||||||
const take = pageSize;
|
|
||||||
|
|
||||||
const beerRecommendations: z.infer<typeof BeerPostQueryResult>[] =
|
|
||||||
await DBClient.instance.beerPost.findMany({
|
|
||||||
where: {
|
|
||||||
OR: [{ styleId: beerPost.style.id }, { breweryId: beerPost.brewery.id }],
|
|
||||||
NOT: { id: beerPost.id },
|
|
||||||
},
|
|
||||||
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: {
|
|
||||||
alt: true,
|
|
||||||
path: true,
|
|
||||||
caption: true,
|
|
||||||
id: true,
|
|
||||||
createdAt: true,
|
|
||||||
updatedAt: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
take,
|
|
||||||
skip,
|
|
||||||
});
|
|
||||||
|
|
||||||
const count = await DBClient.instance.beerPost.count({
|
|
||||||
where: {
|
|
||||||
OR: [{ styleId: beerPost.style.id }, { breweryId: beerPost.brewery.id }],
|
|
||||||
NOT: { id: beerPost.id },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return { beerRecommendations, count };
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getBeerRecommendations;
|
|
||||||
276
src/services/posts/beer-post/index.ts
Normal file
276
src/services/posts/beer-post/index.ts
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import {
|
||||||
|
CreateNewBeerPost,
|
||||||
|
EditBeerPostById,
|
||||||
|
FindOrDeleteBeerPostById,
|
||||||
|
GetAllBeerPosts,
|
||||||
|
GetAllBeerPostsByBreweryId,
|
||||||
|
GetAllBeerPostsByPostedById,
|
||||||
|
GetAllBeerPostsByStyleId,
|
||||||
|
GetBeerRecommendations,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The select object for retrieving beer posts.
|
||||||
|
*
|
||||||
|
* Satisfies the BeerPostQueryResult zod schema.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||||
|
* select: beerPostSelect,
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
const beerPostSelect = {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
description: true,
|
||||||
|
abv: true,
|
||||||
|
ibu: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
beerImages: {
|
||||||
|
select: {
|
||||||
|
alt: true,
|
||||||
|
path: true,
|
||||||
|
caption: true,
|
||||||
|
id: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
brewery: { select: { id: true, name: true } },
|
||||||
|
style: { select: { id: true, name: true, description: true } },
|
||||||
|
postedBy: { select: { id: true, username: true } },
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new beer post.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for creating the beer post.
|
||||||
|
* @param params.name - The name of the beer.
|
||||||
|
* @param params.description - The description of the beer.
|
||||||
|
* @param params.abv - The alcohol by volume of the beer.
|
||||||
|
* @param params.ibu - The International Bitterness Units of the beer.
|
||||||
|
* @param params.styleId - The ID of the beer style.
|
||||||
|
* @param params.breweryId - The ID of the brewery.
|
||||||
|
* @param params.userId - The ID of the user who posted the beer.
|
||||||
|
* @returns A promise that resolves to the newly created beer post.
|
||||||
|
*/
|
||||||
|
export const createNewBeerPost: CreateNewBeerPost = ({
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
abv,
|
||||||
|
ibu,
|
||||||
|
styleId,
|
||||||
|
breweryId,
|
||||||
|
userId,
|
||||||
|
}) => {
|
||||||
|
return DBClient.instance.beerPost.create({
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
abv,
|
||||||
|
ibu,
|
||||||
|
style: { connect: { id: styleId } },
|
||||||
|
postedBy: { connect: { id: userId } },
|
||||||
|
brewery: { connect: { id: breweryId } },
|
||||||
|
},
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a beer post by ID.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for retrieving the beer post.
|
||||||
|
* @param params.beerPostId - The ID of the beer post to retrieve.
|
||||||
|
* @returns A promise that resolves to the beer post.
|
||||||
|
*/
|
||||||
|
export const getBeerPostById: FindOrDeleteBeerPostById = async ({ beerPostId }) => {
|
||||||
|
return DBClient.instance.beerPost.findFirst({
|
||||||
|
where: { id: beerPostId },
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves all beer posts with pagination.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for retrieving beer posts.
|
||||||
|
* @param params.pageNum The page number to retrieve.
|
||||||
|
* @param params.pageSize The number of beer posts per page.
|
||||||
|
* @returns An object containing the beer posts and the total count.
|
||||||
|
*/
|
||||||
|
export const getAllBeerPostsService: GetAllBeerPosts = async ({ pageNum, pageSize }) => {
|
||||||
|
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||||
|
select: beerPostSelect,
|
||||||
|
take: pageSize,
|
||||||
|
skip: (pageNum - 1) * pageSize,
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerPost.count();
|
||||||
|
|
||||||
|
return { beerPosts, count };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves beer posts by beer style ID.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for retrieving beer posts.
|
||||||
|
* @param params.pageNum - The page number of the results.
|
||||||
|
* @param params.pageSize - The number of results per page.
|
||||||
|
* @param params.styleId - The ID of the beer style.
|
||||||
|
* @returns A promise that resolves to an object containing the beer posts and the total
|
||||||
|
* count.
|
||||||
|
*/
|
||||||
|
export const getBeerPostsByBeerStyleIdService: GetAllBeerPostsByStyleId = async ({
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
styleId,
|
||||||
|
}) => {
|
||||||
|
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||||
|
where: { styleId },
|
||||||
|
take: pageSize,
|
||||||
|
skip: (pageNum - 1) * pageSize,
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerPost.count({
|
||||||
|
where: { styleId },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { beerPosts, count };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves beer posts by brewery ID.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for retrieving beer posts.
|
||||||
|
* @param params.pageNum - The page number of the results.
|
||||||
|
* @param params.pageSize - The number of beer posts per page.
|
||||||
|
* @param params.breweryId - The ID of the brewery.
|
||||||
|
* @returns A promise that resolves to an object containing the beer posts and the total
|
||||||
|
* count.
|
||||||
|
*/
|
||||||
|
export const getBeerPostsByBreweryIdService: GetAllBeerPostsByBreweryId = async ({
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
breweryId,
|
||||||
|
}) => {
|
||||||
|
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||||
|
where: { breweryId },
|
||||||
|
take: pageSize,
|
||||||
|
skip: (pageNum - 1) * pageSize,
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerPost.count({
|
||||||
|
where: { breweryId },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { beerPosts, count };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves beer posts by the ID of the user who posted them.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for retrieving beer posts.
|
||||||
|
* @param params.pageNum The page number of the results.
|
||||||
|
* @param params.pageSize The number of results per page.
|
||||||
|
* @param params.postedById The ID of the user who posted the beer posts.
|
||||||
|
* @returns A promise that resolves to an object containing the beer posts and the total
|
||||||
|
* count.
|
||||||
|
*/
|
||||||
|
export const getBeerPostsByPostedByIdService: GetAllBeerPostsByPostedById = async ({
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
postedById,
|
||||||
|
}) => {
|
||||||
|
const beerPosts = await DBClient.instance.beerPost.findMany({
|
||||||
|
where: { postedById },
|
||||||
|
take: pageSize,
|
||||||
|
skip: (pageNum - 1) * pageSize,
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerPost.count({
|
||||||
|
where: { postedById },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { beerPosts, count };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves beer recommendations based on the given parameters.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for retrieving beer recommendations.
|
||||||
|
* @param params.beerPost - The beer post for which recommendations are requested.
|
||||||
|
* @param params.pageNum - The page number of the recommendations.
|
||||||
|
* @param params.pageSize - The number of recommendations per page.
|
||||||
|
* @returns A promise that resolves to an object containing the beer recommendations and
|
||||||
|
* the total count.
|
||||||
|
*/
|
||||||
|
export const getBeerRecommendationsService: GetBeerRecommendations = async ({
|
||||||
|
beerPost,
|
||||||
|
pageNum,
|
||||||
|
pageSize,
|
||||||
|
}) => {
|
||||||
|
const beerRecommendations = await DBClient.instance.beerPost.findMany({
|
||||||
|
where: {
|
||||||
|
OR: [{ styleId: beerPost.style.id }, { breweryId: beerPost.brewery.id }],
|
||||||
|
NOT: { id: beerPost.id },
|
||||||
|
},
|
||||||
|
take: pageSize,
|
||||||
|
skip: (pageNum - 1) * pageSize,
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
|
||||||
|
const count = await DBClient.instance.beerPost.count({
|
||||||
|
where: {
|
||||||
|
OR: [{ styleId: beerPost.style.id }, { breweryId: beerPost.brewery.id }],
|
||||||
|
NOT: { id: beerPost.id },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return { beerRecommendations, count };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service for editing a beer post by ID.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for editing the beer post.
|
||||||
|
* @param params.beerPostId - The ID of the beer post to edit.
|
||||||
|
* @param params.body - The updated data for the beer post.
|
||||||
|
* @param params.body.abv - The updated ABV (Alcohol By Volume) of the beer.
|
||||||
|
* @param params.body.description - The updated description of the beer.
|
||||||
|
* @param params.body.ibu - The updated IBU (International Bitterness Units) of the beer.
|
||||||
|
* @param params.body.name - The updated name of the beer.
|
||||||
|
* @returns - A promise that resolves to the updated beer post.
|
||||||
|
*/
|
||||||
|
export const editBeerPostByIdService: EditBeerPostById = async ({
|
||||||
|
body: { abv, description, ibu, name },
|
||||||
|
beerPostId,
|
||||||
|
}) => {
|
||||||
|
return DBClient.instance.beerPost.update({
|
||||||
|
where: { id: beerPostId },
|
||||||
|
data: { abv, description, ibu, name },
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service for deleting a beer post by ID.
|
||||||
|
*
|
||||||
|
* @param params - The parameters object for deleting the beer post.
|
||||||
|
* @param params.beerPostId - The ID of the beer post to delete.
|
||||||
|
* @returns - A promise that resolves to the deleted beer post.
|
||||||
|
*/
|
||||||
|
export const deleteBeerPostByIdService: FindOrDeleteBeerPostById = async ({
|
||||||
|
beerPostId,
|
||||||
|
}) => {
|
||||||
|
return DBClient.instance.beerPost.delete({
|
||||||
|
where: { id: beerPostId },
|
||||||
|
select: beerPostSelect,
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -3,7 +3,7 @@ import CreateBeerPostValidationSchema from './CreateBeerPostValidationSchema';
|
|||||||
|
|
||||||
const EditBeerPostValidationSchema = CreateBeerPostValidationSchema.omit({
|
const EditBeerPostValidationSchema = CreateBeerPostValidationSchema.omit({
|
||||||
breweryId: true,
|
breweryId: true,
|
||||||
typeId: true,
|
styleId: true,
|
||||||
}).extend({ id: z.string().cuid() });
|
}).extend({ id: z.string().cuid() });
|
||||||
|
|
||||||
export default EditBeerPostValidationSchema;
|
export default EditBeerPostValidationSchema;
|
||||||
|
|||||||
60
src/services/posts/beer-post/types/index.ts
Normal file
60
src/services/posts/beer-post/types/index.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import BeerPostQueryResult from '../schema/BeerPostQueryResult';
|
||||||
|
import EditBeerPostValidationSchema from '../schema/EditBeerPostValidationSchema';
|
||||||
|
import CreateBeerPostValidationSchema from '../schema/CreateBeerPostValidationSchema';
|
||||||
|
|
||||||
|
const CreateSchema = CreateBeerPostValidationSchema.extend({
|
||||||
|
userId: z.string().cuid(),
|
||||||
|
});
|
||||||
|
const EditSchema = EditBeerPostValidationSchema.omit({ id: true });
|
||||||
|
|
||||||
|
type BeerPost = z.infer<typeof BeerPostQueryResult>;
|
||||||
|
|
||||||
|
export type CreateNewBeerPost = (args: z.infer<typeof CreateSchema>) => Promise<BeerPost>;
|
||||||
|
|
||||||
|
export type EditBeerPostById = (args: {
|
||||||
|
beerPostId: string;
|
||||||
|
body: z.infer<typeof EditSchema>;
|
||||||
|
}) => Promise<BeerPost>;
|
||||||
|
|
||||||
|
export type FindOrDeleteBeerPostById = (args: {
|
||||||
|
beerPostId: string;
|
||||||
|
}) => Promise<BeerPost | null>;
|
||||||
|
|
||||||
|
export type GetAllBeerPosts = (args: { pageNum: number; pageSize: number }) => Promise<{
|
||||||
|
beerPosts: BeerPost[];
|
||||||
|
count: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
export type GetAllBeerPostsByPostedById = (args: {
|
||||||
|
postedById: string;
|
||||||
|
pageSize: number;
|
||||||
|
pageNum: number;
|
||||||
|
}) => Promise<{
|
||||||
|
beerPosts: BeerPost[];
|
||||||
|
count: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
export type GetAllBeerPostsByStyleId = (args: {
|
||||||
|
styleId: string;
|
||||||
|
pageSize: number;
|
||||||
|
pageNum: number;
|
||||||
|
}) => Promise<{
|
||||||
|
beerPosts: BeerPost[];
|
||||||
|
count: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
export type GetAllBeerPostsByBreweryId = (args: {
|
||||||
|
breweryId: string;
|
||||||
|
pageSize: number;
|
||||||
|
pageNum: number;
|
||||||
|
}) => Promise<{
|
||||||
|
beerPosts: BeerPost[];
|
||||||
|
count: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
export type GetBeerRecommendations = (args: {
|
||||||
|
beerPost: BeerPost;
|
||||||
|
pageNum: number;
|
||||||
|
pageSize: number;
|
||||||
|
}) => Promise<{ beerRecommendations: BeerPost[]; count: number }>;
|
||||||
Reference in New Issue
Block a user