From 0e9978255768d1d3ce6cac35a7b86f316f7808a2 Mon Sep 17 00:00:00 2001 From: Aaron William Po Date: Sun, 24 Dec 2023 12:34:51 -0500 Subject: [PATCH] Refactor: rename [:id] to [:postId] for api routes --- .../BeerById/BeerPostCommentsSection.tsx | 8 +++++-- .../BeerStyleById/BeerStyleCommentSection.tsx | 4 ++-- .../BreweryById/BreweryCommentsSection.tsx | 21 +++++++++++------- .../comments/beer-comments/index.ts | 16 +++++++------- .../comments/beer-style-comments/index.ts | 16 ++++++++------ .../comments/brewery-comments/index.ts | 18 +++++++-------- src/controllers/comments/types/index.ts | 4 ++-- .../likes/beer-posts-likes/index.ts | 12 +++++----- .../likes/beer-style-likes/index.ts | 12 +++++----- .../likes/brewery-post-likes/index.ts | 22 +++++++++---------- src/controllers/likes/types/index.ts | 2 +- src/controllers/posts/beer-posts/index.ts | 14 ++++++------ .../posts/beer-posts/types/index.ts | 4 ++-- .../useGetBreweryPostLikeCount.ts | 2 +- .../[postId]/comments/[commentId].ts} | 6 +++-- .../{[id] => [postId]}/comments/index.ts | 4 ++-- .../beers/{[id] => [postId]}/images/index.ts | 0 .../api/beers/{[id] => [postId]}/index.ts | 4 ++-- .../beers/{[id] => [postId]}/like/index.ts | 4 ++-- .../beers/{[id] => [postId]}/like/is-liked.ts | 2 +- .../{[id] => [postId]}/recommendations.ts | 2 +- .../styles/{[id] => [postId]}/beers/index.ts | 2 +- .../styles/[postId]/comments/[commentId].ts} | 4 ++-- .../{[id] => [postId]}/comments/index.ts | 4 ++-- .../beers/styles/{[id] => [postId]}/index.ts | 2 +- .../styles/{[id] => [postId]}/like/index.ts | 4 ++-- .../{[id] => [postId]}/like/is-liked.ts | 2 +- .../{[id] => [postId]}/beers/index.ts | 2 +- .../[postId]/comments/[commentId].ts} | 4 ++-- .../{[id] => [postId]}/comments/index.ts | 4 ++-- .../{[id] => [postId]}/images/index.ts | 0 .../api/breweries/{[id] => [postId]}/index.ts | 0 .../{[id] => [postId]}/like/index.ts | 4 ++-- .../{[id] => [postId]}/like/is-liked.ts | 6 +---- src/requests/comments/beer-comment/index.ts | 6 +++-- .../comments/beer-comment/types/index.ts | 2 ++ 36 files changed, 118 insertions(+), 105 deletions(-) rename src/pages/api/{beer-comments/[id].ts => beers/[postId]/comments/[commentId].ts} (84%) rename src/pages/api/beers/{[id] => [postId]}/comments/index.ts (88%) rename src/pages/api/beers/{[id] => [postId]}/images/index.ts (100%) rename src/pages/api/beers/{[id] => [postId]}/index.ts (90%) rename src/pages/api/beers/{[id] => [postId]}/like/index.ts (85%) rename src/pages/api/beers/{[id] => [postId]}/like/is-liked.ts (91%) rename src/pages/api/beers/{[id] => [postId]}/recommendations.ts (91%) rename src/pages/api/beers/styles/{[id] => [postId]}/beers/index.ts (91%) rename src/pages/api/{beer-style-comments/[id].ts => beers/styles/[postId]/comments/[commentId].ts} (87%) rename src/pages/api/beers/styles/{[id] => [postId]}/comments/index.ts (88%) rename src/pages/api/beers/styles/{[id] => [postId]}/index.ts (89%) rename src/pages/api/beers/styles/{[id] => [postId]}/like/index.ts (85%) rename src/pages/api/beers/styles/{[id] => [postId]}/like/is-liked.ts (91%) rename src/pages/api/breweries/{[id] => [postId]}/beers/index.ts (90%) rename src/pages/api/{brewery-comments/[id].ts => breweries/[postId]/comments/[commentId].ts} (87%) rename src/pages/api/breweries/{[id] => [postId]}/comments/index.ts (88%) rename src/pages/api/breweries/{[id] => [postId]}/images/index.ts (100%) rename src/pages/api/breweries/{[id] => [postId]}/index.ts (100%) rename src/pages/api/breweries/{[id] => [postId]}/like/index.ts (85%) rename src/pages/api/breweries/{[id] => [postId]}/like/is-liked.ts (90%) diff --git a/src/components/BeerById/BeerPostCommentsSection.tsx b/src/components/BeerById/BeerPostCommentsSection.tsx index 6d574e4..f8f6738 100644 --- a/src/components/BeerById/BeerPostCommentsSection.tsx +++ b/src/components/BeerById/BeerPostCommentsSection.tsx @@ -33,14 +33,18 @@ const BeerPostCommentsSection: FC = ({ beerPost }) const commentSectionRef: MutableRefObject = useRef(null); const handleDeleteRequest = async (id: string) => { - deleteBeerPostCommentRequest({ commentId: id }); + await deleteBeerPostCommentRequest({ commentId: id, beerPostId: beerPost.id }); }; const handleEditRequest = async ( id: string, data: z.infer, ) => { - editBeerPostCommentRequest({ body: data, commentId: id }); + await editBeerPostCommentRequest({ + body: data, + commentId: id, + beerPostId: beerPost.id, + }); }; return ( diff --git a/src/components/BeerStyleById/BeerStyleCommentSection.tsx b/src/components/BeerStyleById/BeerStyleCommentSection.tsx index 5716327..470ee7e 100644 --- a/src/components/BeerStyleById/BeerStyleCommentSection.tsx +++ b/src/components/BeerStyleById/BeerStyleCommentSection.tsx @@ -28,7 +28,7 @@ const BeerStyleCommentsSection: FC = ({ beerStyle const commentSectionRef: MutableRefObject = useRef(null); const handleDeleteRequest = async (id: string) => { - const response = await fetch(`/api/beer-style-comments/${id}`, { + const response = await fetch(`/api/beers/styles/${beerStyle.id}/comments/${id}`, { method: 'DELETE', }); @@ -41,7 +41,7 @@ const BeerStyleCommentsSection: FC = ({ beerStyle id: string, data: z.infer, ) => { - const response = await fetch(`/api/beer-style-comments/${id}`, { + const response = await fetch(`/api/beers/styles/${beerStyle.id}/comments/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: data.content, rating: data.rating }), diff --git a/src/components/BreweryById/BreweryCommentsSection.tsx b/src/components/BreweryById/BreweryCommentsSection.tsx index e7b6c2e..1ecbcfc 100644 --- a/src/components/BreweryById/BreweryCommentsSection.tsx +++ b/src/components/BreweryById/BreweryCommentsSection.tsx @@ -31,9 +31,10 @@ const BreweryCommentsSection: FC = ({ breweryPost }) => const commentSectionRef: MutableRefObject = useRef(null); const handleDeleteRequest = async (commentId: string) => { - const response = await fetch(`/api/brewery-comments/${commentId}`, { - method: 'DELETE', - }); + const response = await fetch( + `/api/breweries/${breweryPost.id}/comments/${commentId}`, + { method: 'DELETE' }, + ); if (!response.ok) { throw new Error(response.statusText); @@ -44,15 +45,19 @@ const BreweryCommentsSection: FC = ({ breweryPost }) => commentId: string, data: z.infer, ) => { - const response = await fetch(`/api/brewery-comments/${commentId}`, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ content: data.content, rating: data.rating }), - }); + const response = await fetch( + `/api/breweries/${breweryPost.id}/comments/${commentId}`, + { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ content: data.content, rating: data.rating }), + }, + ); if (!response.ok) { throw new Error(response.statusText); } + console.log(await response.json()); }; return ( diff --git a/src/controllers/comments/beer-comments/index.ts b/src/controllers/comments/beer-comments/index.ts index 7ca8dbc..b56cd62 100644 --- a/src/controllers/comments/beer-comments/index.ts +++ b/src/controllers/comments/beer-comments/index.ts @@ -24,9 +24,9 @@ export const checkIfBeerCommentOwner = async ( res: NextApiResponse>, next: NextHandler, ) => { - const { id } = req.query; + const { commentId } = req.query; const user = req.user!; - const comment = await getBeerPostCommentByIdService({ beerPostCommentId: id }); + const comment = await getBeerPostCommentByIdService({ beerPostCommentId: commentId }); if (!comment) { throw new ServerError('Comment not found', 404); @@ -43,9 +43,9 @@ export const editBeerPostComment = async ( req: EditAndCreateCommentRequest, res: NextApiResponse>, ) => { - const { id } = req.query; + const { commentId } = req.query; - await editBeerPostCommentByIdService({ body: req.body, beerPostCommentId: id }); + await editBeerPostCommentByIdService({ body: req.body, beerPostCommentId: commentId }); res.status(200).json({ success: true, @@ -58,9 +58,9 @@ export const deleteBeerPostComment = async ( req: CommentRequest, res: NextApiResponse>, ) => { - const { id } = req.query; + const { commentId } = req.query; - await deleteBeerCommentByIdService({ beerPostCommentId: id }); + await deleteBeerCommentByIdService({ beerPostCommentId: commentId }); res.status(200).json({ success: true, @@ -73,7 +73,7 @@ export const createBeerPostComment = async ( req: EditAndCreateCommentRequest, res: NextApiResponse>, ) => { - const beerPostId = req.query.id; + const beerPostId = req.query.postId; const newBeerComment = await createBeerPostCommentService({ body: req.body, @@ -93,7 +93,7 @@ export const getAllBeerPostComments = async ( req: GetAllCommentsRequest, res: NextApiResponse>, ) => { - const beerPostId = req.query.id; + const beerPostId = req.query.postId; // eslint-disable-next-line @typescript-eslint/naming-convention const { page_size, page_num } = req.query; diff --git a/src/controllers/comments/beer-style-comments/index.ts b/src/controllers/comments/beer-style-comments/index.ts index 9365c0c..7179fc3 100644 --- a/src/controllers/comments/beer-style-comments/index.ts +++ b/src/controllers/comments/beer-style-comments/index.ts @@ -25,10 +25,12 @@ export const checkIfBeerStyleCommentOwner = async < res: NextApiResponse>, next: NextHandler, ) => { - const { id } = req.query; + const { commentId } = req.query; const user = req.user!; - const beerStyleComment = await findBeerStyleCommentById({ beerStyleCommentId: id }); + const beerStyleComment = await findBeerStyleCommentById({ + beerStyleCommentId: commentId, + }); if (!beerStyleComment) { throw new ServerError('Beer style comment not found.', 404); @@ -49,7 +51,7 @@ export const editBeerStyleComment = async ( res: NextApiResponse>, ) => { await updateBeerStyleCommentById({ - beerStyleCommentId: req.query.id, + beerStyleCommentId: req.query.commentId, body: req.body, }); @@ -64,9 +66,9 @@ export const deleteBeerStyleComment = async ( req: CommentRequest, res: NextApiResponse>, ) => { - const { id } = req.query; + const { commentId } = req.query; - await deleteBeerStyleCommentById({ beerStyleCommentId: id }); + await deleteBeerStyleCommentById({ beerStyleCommentId: commentId }); res.status(200).json({ success: true, @@ -81,7 +83,7 @@ export const createComment = async ( ) => { const newBeerStyleComment = await createNewBeerStyleComment({ body: req.body, - beerStyleId: req.query.id, + beerStyleId: req.query.postId, userId: req.user!.id, }); @@ -97,7 +99,7 @@ export const getAll = async ( req: GetAllCommentsRequest, res: NextApiResponse>, ) => { - const beerStyleId = req.query.id; + const beerStyleId = req.query.postId; // eslint-disable-next-line @typescript-eslint/naming-convention const { page_size, page_num } = req.query; diff --git a/src/controllers/comments/brewery-comments/index.ts b/src/controllers/comments/brewery-comments/index.ts index 103dca9..cfe0b27 100644 --- a/src/controllers/comments/brewery-comments/index.ts +++ b/src/controllers/comments/brewery-comments/index.ts @@ -25,9 +25,9 @@ export const checkIfBreweryCommentOwner = async < res: NextApiResponse>, next: NextHandler, ) => { - const { id } = req.query; + const { commentId } = req.query; const user = req.user!; - const comment = await getBreweryCommentById({ breweryCommentId: id }); + const comment = await getBreweryCommentById({ breweryCommentId: commentId }); if (!comment) { throw new ServerError('Comment not found', 404); @@ -44,10 +44,10 @@ export const editBreweryPostComment = async ( req: EditAndCreateCommentRequest, res: NextApiResponse>, ) => { - const { id } = req.query; + const { commentId } = req.query; - const updated = updateBreweryCommentById({ - breweryCommentId: id, + const updated = await updateBreweryCommentById({ + breweryCommentId: commentId, body: req.body, }); @@ -63,9 +63,9 @@ export const deleteBreweryPostComment = async ( req: CommentRequest, res: NextApiResponse>, ) => { - const { id } = req.query; + const { commentId } = req.query; - await deleteBreweryCommentByIdService({ breweryCommentId: id }); + await deleteBreweryCommentByIdService({ breweryCommentId: commentId }); res.status(200).json({ success: true, @@ -78,7 +78,7 @@ export const createComment = async ( req: EditAndCreateCommentRequest, res: NextApiResponse>, ) => { - const breweryPostId = req.query.id; + const breweryPostId = req.query.postId; const user = req.user!; @@ -100,7 +100,7 @@ export const getAll = async ( req: GetAllCommentsRequest, res: NextApiResponse>, ) => { - const breweryPostId = req.query.id; + const breweryPostId = req.query.postId; // eslint-disable-next-line @typescript-eslint/naming-convention const { page_size, page_num } = req.query; diff --git a/src/controllers/comments/types/index.ts b/src/controllers/comments/types/index.ts index f56f153..9968ba2 100644 --- a/src/controllers/comments/types/index.ts +++ b/src/controllers/comments/types/index.ts @@ -3,7 +3,7 @@ import CreateCommentValidationSchema from '@/services/schema/CommentSchema/Creat import { z } from 'zod'; export interface CommentRequest extends UserExtendedNextApiRequest { - query: { id: string }; + query: { postId: string; commentId: string }; } export interface EditAndCreateCommentRequest extends CommentRequest { @@ -11,5 +11,5 @@ export interface EditAndCreateCommentRequest extends CommentRequest { } export interface GetAllCommentsRequest extends UserExtendedNextApiRequest { - query: { id: string; page_size: string; page_num: string }; + query: { postId: string; page_size: string; page_num: string }; } diff --git a/src/controllers/likes/beer-posts-likes/index.ts b/src/controllers/likes/beer-posts-likes/index.ts index 0211164..2215978 100644 --- a/src/controllers/likes/beer-posts-likes/index.ts +++ b/src/controllers/likes/beer-posts-likes/index.ts @@ -2,7 +2,7 @@ import { UserExtendedNextApiRequest } from '@/config/auth/types'; import ServerError from '@/config/util/ServerError'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; -import { NextApiResponse, NextApiRequest } from 'next'; +import { NextApiResponse } from 'next'; import { z } from 'zod'; import { getBeerPostById } from '@/services/posts/beer-post'; @@ -19,9 +19,9 @@ export const sendBeerPostLikeRequest = async ( res: NextApiResponse>, ) => { const user = req.user!; - const id = req.query.id as string; + const { postId } = req.query; - const beer = await getBeerPostById({ beerPostId: id }); + const beer = await getBeerPostById({ beerPostId: postId }); if (!beer) { throw new ServerError('Could not find a beer post with that id.', 404); } @@ -53,12 +53,12 @@ export const sendBeerPostLikeRequest = async ( }; export const getBeerPostLikeCount = async ( - req: NextApiRequest, + req: LikeRequest, res: NextApiResponse>, ) => { - const id = req.query.id as string; + const { postId } = req.query; - const likeCount = await getBeerPostLikeCountService({ beerPostId: id }); + const likeCount = await getBeerPostLikeCountService({ beerPostId: postId }); res.status(200).json({ success: true, diff --git a/src/controllers/likes/beer-style-likes/index.ts b/src/controllers/likes/beer-style-likes/index.ts index fd2499d..85a8403 100644 --- a/src/controllers/likes/beer-style-likes/index.ts +++ b/src/controllers/likes/beer-style-likes/index.ts @@ -1,7 +1,7 @@ import ServerError from '@/config/util/ServerError'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; -import { NextApiResponse, NextApiRequest } from 'next'; +import { NextApiResponse } from 'next'; import { z } from 'zod'; import { UserExtendedNextApiRequest } from '@/config/auth/types'; import { @@ -18,9 +18,9 @@ export const sendBeerStyleLikeRequest = async ( res: NextApiResponse>, ) => { const user = req.user!; - const { id } = req.query; + const { postId } = req.query; - const beerStyle = await getBeerStyleByIdService({ beerStyleId: id }); + const beerStyle = await getBeerStyleByIdService({ beerStyleId: postId }); if (!beerStyle) { throw new ServerError('Could not find a beer style with that id.', 404); } @@ -48,11 +48,11 @@ export const sendBeerStyleLikeRequest = async ( }; export const getBeerStyleLikeCountRequest = async ( - req: NextApiRequest, + req: LikeRequest, res: NextApiResponse>, ) => { - const id = req.query.id as string; - const likeCount = await getBeerStyleLikeCountService({ beerStyleId: id }); + const { postId } = req.query; + const likeCount = await getBeerStyleLikeCountService({ beerStyleId: postId }); res.status(200).json({ success: true, diff --git a/src/controllers/likes/brewery-post-likes/index.ts b/src/controllers/likes/brewery-post-likes/index.ts index 6eeb790..af4374f 100644 --- a/src/controllers/likes/brewery-post-likes/index.ts +++ b/src/controllers/likes/brewery-post-likes/index.ts @@ -1,4 +1,3 @@ -import { UserExtendedNextApiRequest } from '@/config/auth/types'; import ServerError from '@/config/util/ServerError'; import { @@ -9,17 +8,18 @@ import { } from '@/services/likes/brewery-post-like'; import { getBreweryPostByIdService } from '@/services/posts/brewery-post'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; -import { NextApiResponse, NextApiRequest } from 'next'; +import { NextApiResponse } from 'next'; import { z } from 'zod'; +import { LikeRequest } from '../types'; export const sendBreweryPostLikeRequest = async ( - req: UserExtendedNextApiRequest, + req: LikeRequest, res: NextApiResponse>, ) => { - const id = req.query.id! as string; + const { postId } = req.query; const user = req.user!; - const breweryPost = await getBreweryPostByIdService({ breweryPostId: id }); + const breweryPost = await getBreweryPostByIdService({ breweryPostId: postId }); if (!breweryPost) { throw new ServerError('Could not find a brewery post with that id', 404); @@ -53,12 +53,12 @@ export const sendBreweryPostLikeRequest = async ( }; export const getBreweryPostLikeCount = async ( - req: NextApiRequest, + req: LikeRequest, res: NextApiResponse>, ) => { - const id = req.query.id! as string; + const { postId } = req.query; - const breweryPost = await getBreweryPostByIdService({ breweryPostId: id }); + const breweryPost = await getBreweryPostByIdService({ breweryPostId: postId }); if (!breweryPost) { throw new ServerError('Could not find a brewery post with that id', 404); } @@ -76,14 +76,14 @@ export const getBreweryPostLikeCount = async ( }; export const getBreweryPostLikeStatus = async ( - req: UserExtendedNextApiRequest, + req: LikeRequest, res: NextApiResponse>, ) => { const user = req.user!; - const id = req.query.id as string; + const { postId } = req.query; const liked = await findBreweryPostLikeService({ - breweryPostId: id, + breweryPostId: postId, likedById: user.id, }); diff --git a/src/controllers/likes/types/index.ts b/src/controllers/likes/types/index.ts index 424a4e4..8efc87b 100644 --- a/src/controllers/likes/types/index.ts +++ b/src/controllers/likes/types/index.ts @@ -1,5 +1,5 @@ import { UserExtendedNextApiRequest } from '@/config/auth/types'; export interface LikeRequest extends UserExtendedNextApiRequest { - query: { id: string }; + query: { postId: string }; } diff --git a/src/controllers/posts/beer-posts/index.ts b/src/controllers/posts/beer-posts/index.ts index 76cacf7..0e8bda5 100644 --- a/src/controllers/posts/beer-posts/index.ts +++ b/src/controllers/posts/beer-posts/index.ts @@ -28,9 +28,9 @@ export const checkIfBeerPostOwner = async { const { user, query } = req; - const { id } = query; + const { postId } = query; - const beerPost = await getBeerPostById({ beerPostId: id }); + const beerPost = await getBeerPostById({ beerPostId: postId }); if (!beerPost) { throw new ServerError('Beer post not found', 404); @@ -47,7 +47,7 @@ export const editBeerPost = async ( req: EditBeerPostRequest, res: NextApiResponse>, ) => { - await editBeerPostByIdService({ beerPostId: req.query.id, body: req.body }); + await editBeerPostByIdService({ beerPostId: req.query.postId, body: req.body }); res.status(200).json({ message: 'Beer post updated successfully', @@ -57,9 +57,9 @@ export const editBeerPost = async ( }; export const deleteBeerPost = async (req: BeerPostRequest, res: NextApiResponse) => { - const { id } = req.query; + const { postId } = req.query; - const deleted = await deleteBeerPostByIdService({ beerPostId: id }); + const deleted = await deleteBeerPostByIdService({ beerPostId: postId }); if (!deleted) { throw new ServerError('Beer post not found', 404); } @@ -75,9 +75,9 @@ export const getBeerPostRecommendations = async ( req: GetBeerRecommendationsRequest, res: NextApiResponse>, ) => { - const { id } = req.query; + const { postId } = req.query; - const beerPost = await getBeerPostById({ beerPostId: id }); + const beerPost = await getBeerPostById({ beerPostId: postId }); if (!beerPost) { throw new ServerError('Beer post not found', 404); diff --git a/src/controllers/posts/beer-posts/types/index.ts b/src/controllers/posts/beer-posts/types/index.ts index a1b5977..d4bed49 100644 --- a/src/controllers/posts/beer-posts/types/index.ts +++ b/src/controllers/posts/beer-posts/types/index.ts @@ -5,7 +5,7 @@ import { NextApiRequest } from 'next'; import { z } from 'zod'; export interface BeerPostRequest extends UserExtendedNextApiRequest { - query: { id: string }; + query: { postId: string }; } export interface EditBeerPostRequest extends BeerPostRequest { @@ -17,7 +17,7 @@ export interface GetAllBeerPostsRequest extends NextApiRequest { } export interface GetBeerRecommendationsRequest extends BeerPostRequest { - query: { id: string; page_num: string; page_size: string }; + query: { postId: string; page_num: string; page_size: string }; } export interface CreateBeerPostRequest extends UserExtendedNextApiRequest { diff --git a/src/hooks/data-fetching/brewery-likes/useGetBreweryPostLikeCount.ts b/src/hooks/data-fetching/brewery-likes/useGetBreweryPostLikeCount.ts index c2ba167..7391410 100644 --- a/src/hooks/data-fetching/brewery-likes/useGetBreweryPostLikeCount.ts +++ b/src/hooks/data-fetching/brewery-likes/useGetBreweryPostLikeCount.ts @@ -41,7 +41,7 @@ const useGetBreweryPostLikeCount = (breweryPostId: string) => { error: error as unknown, isLoading, mutate, - likeCount: data as number | undefined, + likeCount: data, }; }; diff --git a/src/pages/api/beer-comments/[id].ts b/src/pages/api/beers/[postId]/comments/[commentId].ts similarity index 84% rename from src/pages/api/beer-comments/[id].ts rename to src/pages/api/beers/[postId]/comments/[commentId].ts index 8acf718..aa30b1d 100644 --- a/src/pages/api/beer-comments/[id].ts +++ b/src/pages/api/beers/[postId]/comments/[commentId].ts @@ -22,14 +22,16 @@ const router = createRouter< router .delete( - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ + querySchema: z.object({ postId: z.string().cuid(), commentId: z.string().cuid() }), + }), getCurrentUser, checkIfBeerCommentOwner, deleteBeerPostComment, ) .put( validateRequest({ - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ postId: z.string().cuid(), commentId: z.string().cuid() }), bodySchema: CreateCommentValidationSchema, }), getCurrentUser, diff --git a/src/pages/api/beers/[id]/comments/index.ts b/src/pages/api/beers/[postId]/comments/index.ts similarity index 88% rename from src/pages/api/beers/[id]/comments/index.ts rename to src/pages/api/beers/[postId]/comments/index.ts index 1efb734..17b0608 100644 --- a/src/pages/api/beers/[id]/comments/index.ts +++ b/src/pages/api/beers/[postId]/comments/index.ts @@ -22,7 +22,7 @@ const router = createRouter< router.post( validateRequest({ bodySchema: CreateCommentValidationSchema, - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ postId: z.string().cuid() }), }), getCurrentUser, createBeerPostComment, @@ -30,7 +30,7 @@ router.post( router.get( validateRequest({ - querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }), + querySchema: PaginatedQueryResponseSchema.extend({ postId: z.string().cuid() }), }), getAllBeerPostComments, ); diff --git a/src/pages/api/beers/[id]/images/index.ts b/src/pages/api/beers/[postId]/images/index.ts similarity index 100% rename from src/pages/api/beers/[id]/images/index.ts rename to src/pages/api/beers/[postId]/images/index.ts diff --git a/src/pages/api/beers/[id]/index.ts b/src/pages/api/beers/[postId]/index.ts similarity index 90% rename from src/pages/api/beers/[id]/index.ts rename to src/pages/api/beers/[postId]/index.ts index 8a67ec7..f513eaf 100644 --- a/src/pages/api/beers/[id]/index.ts +++ b/src/pages/api/beers/[postId]/index.ts @@ -26,14 +26,14 @@ router .put( validateRequest({ bodySchema: EditBeerPostValidationSchema, - querySchema: z.object({ id: z.string() }), + querySchema: z.object({ postId: z.string() }), }), getCurrentUser, checkIfBeerPostOwner, editBeerPost, ) .delete( - validateRequest({ querySchema: z.object({ id: z.string() }) }), + validateRequest({ querySchema: z.object({ postId: z.string() }) }), getCurrentUser, checkIfBeerPostOwner, deleteBeerPost, diff --git a/src/pages/api/beers/[id]/like/index.ts b/src/pages/api/beers/[postId]/like/index.ts similarity index 85% rename from src/pages/api/beers/[id]/like/index.ts rename to src/pages/api/beers/[postId]/like/index.ts index a6e7eb8..ffbc659 100644 --- a/src/pages/api/beers/[id]/like/index.ts +++ b/src/pages/api/beers/[postId]/like/index.ts @@ -19,12 +19,12 @@ const router = createRouter< router.post( getCurrentUser, - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), sendBeerPostLikeRequest, ); router.get( - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), getBeerPostLikeCount, ); diff --git a/src/pages/api/beers/[id]/like/is-liked.ts b/src/pages/api/beers/[postId]/like/is-liked.ts similarity index 91% rename from src/pages/api/beers/[id]/like/is-liked.ts rename to src/pages/api/beers/[postId]/like/is-liked.ts index aa0f9a3..3146f48 100644 --- a/src/pages/api/beers/[id]/like/is-liked.ts +++ b/src/pages/api/beers/[postId]/like/is-liked.ts @@ -16,7 +16,7 @@ const router = createRouter< router.get( getCurrentUser, - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), checkIfBeerPostIsLiked, ); diff --git a/src/pages/api/beers/[id]/recommendations.ts b/src/pages/api/beers/[postId]/recommendations.ts similarity index 91% rename from src/pages/api/beers/[id]/recommendations.ts rename to src/pages/api/beers/[postId]/recommendations.ts index a3f6f92..65cf82a 100644 --- a/src/pages/api/beers/[id]/recommendations.ts +++ b/src/pages/api/beers/[postId]/recommendations.ts @@ -15,7 +15,7 @@ const router = createRouter< router.get( validateRequest({ - querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }), + querySchema: PaginatedQueryResponseSchema.extend({ postId: z.string().cuid() }), }), getBeerPostRecommendations, ); diff --git a/src/pages/api/beers/styles/[id]/beers/index.ts b/src/pages/api/beers/styles/[postId]/beers/index.ts similarity index 91% rename from src/pages/api/beers/styles/[id]/beers/index.ts rename to src/pages/api/beers/styles/[postId]/beers/index.ts index 7da569f..46440b2 100644 --- a/src/pages/api/beers/styles/[id]/beers/index.ts +++ b/src/pages/api/beers/styles/[postId]/beers/index.ts @@ -19,7 +19,7 @@ const router = createRouter< router.get( validateRequest({ - querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }), + querySchema: PaginatedQueryResponseSchema.extend({ postId: z.string().cuid() }), }), getAllBeersByBeerStyle, ); diff --git a/src/pages/api/beer-style-comments/[id].ts b/src/pages/api/beers/styles/[postId]/comments/[commentId].ts similarity index 87% rename from src/pages/api/beer-style-comments/[id].ts rename to src/pages/api/beers/styles/[postId]/comments/[commentId].ts index ec6dc8f..4d7f0b3 100644 --- a/src/pages/api/beer-style-comments/[id].ts +++ b/src/pages/api/beers/styles/[postId]/comments/[commentId].ts @@ -22,7 +22,7 @@ const router = createRouter< router .delete( validateRequest({ - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ postId: z.string().cuid(), commentId: z.string().cuid() }), }), getCurrentUser, checkIfBeerStyleCommentOwner, @@ -30,7 +30,7 @@ router ) .put( validateRequest({ - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ postId: z.string().cuid(), commentId: z.string().cuid() }), bodySchema: CreateCommentValidationSchema, }), getCurrentUser, diff --git a/src/pages/api/beers/styles/[id]/comments/index.ts b/src/pages/api/beers/styles/[postId]/comments/index.ts similarity index 88% rename from src/pages/api/beers/styles/[id]/comments/index.ts rename to src/pages/api/beers/styles/[postId]/comments/index.ts index b8e9b8c..6345d20 100644 --- a/src/pages/api/beers/styles/[id]/comments/index.ts +++ b/src/pages/api/beers/styles/[postId]/comments/index.ts @@ -19,7 +19,7 @@ const router = createRouter< router.post( validateRequest({ bodySchema: CreateCommentValidationSchema, - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ postId: z.string().cuid() }), }), getCurrentUser, createComment, @@ -27,7 +27,7 @@ router.post( router.get( validateRequest({ - querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }), + querySchema: PaginatedQueryResponseSchema.extend({ postId: z.string().cuid() }), }), getAll, ); diff --git a/src/pages/api/beers/styles/[id]/index.ts b/src/pages/api/beers/styles/[postId]/index.ts similarity index 89% rename from src/pages/api/beers/styles/[id]/index.ts rename to src/pages/api/beers/styles/[postId]/index.ts index c392ab7..1d8cfcc 100644 --- a/src/pages/api/beers/styles/[id]/index.ts +++ b/src/pages/api/beers/styles/[postId]/index.ts @@ -13,7 +13,7 @@ const router = createRouter< >(); router.get( - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), getBeerStyle, ); diff --git a/src/pages/api/beers/styles/[id]/like/index.ts b/src/pages/api/beers/styles/[postId]/like/index.ts similarity index 85% rename from src/pages/api/beers/styles/[id]/like/index.ts rename to src/pages/api/beers/styles/[postId]/like/index.ts index 87ff024..688c83c 100644 --- a/src/pages/api/beers/styles/[id]/like/index.ts +++ b/src/pages/api/beers/styles/[postId]/like/index.ts @@ -20,12 +20,12 @@ const router = createRouter< router.post( getCurrentUser, - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), sendBeerStyleLikeRequest, ); router.get( - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), getBeerStyleLikeCountRequest, ); diff --git a/src/pages/api/beers/styles/[id]/like/is-liked.ts b/src/pages/api/beers/styles/[postId]/like/is-liked.ts similarity index 91% rename from src/pages/api/beers/styles/[id]/like/is-liked.ts rename to src/pages/api/beers/styles/[postId]/like/is-liked.ts index 3289cb6..d4709a7 100644 --- a/src/pages/api/beers/styles/[id]/like/is-liked.ts +++ b/src/pages/api/beers/styles/[postId]/like/is-liked.ts @@ -15,7 +15,7 @@ const router = createRouter< router.get( getCurrentUser, - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), checkIfBeerStyleIsLiked, ); diff --git a/src/pages/api/breweries/[id]/beers/index.ts b/src/pages/api/breweries/[postId]/beers/index.ts similarity index 90% rename from src/pages/api/breweries/[id]/beers/index.ts rename to src/pages/api/breweries/[postId]/beers/index.ts index b3a51f0..62c0564 100644 --- a/src/pages/api/breweries/[id]/beers/index.ts +++ b/src/pages/api/breweries/[postId]/beers/index.ts @@ -15,7 +15,7 @@ const router = createRouter< router.get( validateRequest({ - querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }), + querySchema: PaginatedQueryResponseSchema.extend({ postId: z.string().cuid() }), }), getAllBeersByBrewery, ); diff --git a/src/pages/api/brewery-comments/[id].ts b/src/pages/api/breweries/[postId]/comments/[commentId].ts similarity index 87% rename from src/pages/api/brewery-comments/[id].ts rename to src/pages/api/breweries/[postId]/comments/[commentId].ts index 57e13c4..9865356 100644 --- a/src/pages/api/brewery-comments/[id].ts +++ b/src/pages/api/breweries/[postId]/comments/[commentId].ts @@ -24,7 +24,7 @@ const router = createRouter< router .delete( validateRequest({ - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ commentId: z.string().cuid(), postId: z.string().cuid() }), }), getCurrentUser, checkIfBreweryCommentOwner, @@ -32,7 +32,7 @@ router ) .put( validateRequest({ - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ commentId: z.string().cuid(), postId: z.string().cuid() }), bodySchema: CreateCommentValidationSchema, }), getCurrentUser, diff --git a/src/pages/api/breweries/[id]/comments/index.ts b/src/pages/api/breweries/[postId]/comments/index.ts similarity index 88% rename from src/pages/api/breweries/[id]/comments/index.ts rename to src/pages/api/breweries/[postId]/comments/index.ts index 338ff16..c97b67b 100644 --- a/src/pages/api/breweries/[id]/comments/index.ts +++ b/src/pages/api/breweries/[postId]/comments/index.ts @@ -20,7 +20,7 @@ const router = createRouter< router.post( validateRequest({ bodySchema: CreateCommentValidationSchema, - querySchema: z.object({ id: z.string().cuid() }), + querySchema: z.object({ postId: z.string().cuid() }), }), getCurrentUser, createComment, @@ -28,7 +28,7 @@ router.post( router.get( validateRequest({ - querySchema: PaginatedQueryResponseSchema.extend({ id: z.string().cuid() }), + querySchema: PaginatedQueryResponseSchema.extend({ postId: z.string().cuid() }), }), getAll, ); diff --git a/src/pages/api/breweries/[id]/images/index.ts b/src/pages/api/breweries/[postId]/images/index.ts similarity index 100% rename from src/pages/api/breweries/[id]/images/index.ts rename to src/pages/api/breweries/[postId]/images/index.ts diff --git a/src/pages/api/breweries/[id]/index.ts b/src/pages/api/breweries/[postId]/index.ts similarity index 100% rename from src/pages/api/breweries/[id]/index.ts rename to src/pages/api/breweries/[postId]/index.ts diff --git a/src/pages/api/breweries/[id]/like/index.ts b/src/pages/api/breweries/[postId]/like/index.ts similarity index 85% rename from src/pages/api/breweries/[id]/like/index.ts rename to src/pages/api/breweries/[postId]/like/index.ts index 40b6c14..1bdc931 100644 --- a/src/pages/api/breweries/[id]/like/index.ts +++ b/src/pages/api/breweries/[postId]/like/index.ts @@ -19,12 +19,12 @@ const router = createRouter< router.post( getCurrentUser, - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), sendBreweryPostLikeRequest, ); router.get( - validateRequest({ querySchema: z.object({ id: z.string().cuid() }) }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), getBreweryPostLikeCount, ); diff --git a/src/pages/api/breweries/[id]/like/is-liked.ts b/src/pages/api/breweries/[postId]/like/is-liked.ts similarity index 90% rename from src/pages/api/breweries/[id]/like/is-liked.ts rename to src/pages/api/breweries/[postId]/like/is-liked.ts index 4a41897..16e97bf 100644 --- a/src/pages/api/breweries/[id]/like/is-liked.ts +++ b/src/pages/api/breweries/[postId]/like/is-liked.ts @@ -16,11 +16,7 @@ const router = createRouter< router.get( getCurrentUser, - validateRequest({ - querySchema: z.object({ - id: z.string().cuid(), - }), - }), + validateRequest({ querySchema: z.object({ postId: z.string().cuid() }) }), getBreweryPostLikeStatus, ); diff --git a/src/requests/comments/beer-comment/index.ts b/src/requests/comments/beer-comment/index.ts index 7c2d015..d922500 100644 --- a/src/requests/comments/beer-comment/index.ts +++ b/src/requests/comments/beer-comment/index.ts @@ -9,8 +9,9 @@ import { export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async ({ body, commentId, + beerPostId, }) => { - const response = await fetch(`/api/beer-post-comments/${commentId}`, { + const response = await fetch(`/api/beers/${beerPostId}/comments/${commentId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), @@ -33,8 +34,9 @@ export const editBeerPostCommentRequest: SendEditBeerPostCommentRequest = async export const deleteBeerPostCommentRequest: SendDeleteBeerPostCommentRequest = async ({ commentId, + beerPostId, }) => { - const response = await fetch(`/api/beer-post-comments/${commentId}`, { + const response = await fetch(`/api/beers/${beerPostId}/comments/${commentId}`, { method: 'DELETE', }); diff --git a/src/requests/comments/beer-comment/types/index.ts b/src/requests/comments/beer-comment/types/index.ts index b790ed6..45f8d96 100644 --- a/src/requests/comments/beer-comment/types/index.ts +++ b/src/requests/comments/beer-comment/types/index.ts @@ -5,10 +5,12 @@ import { z } from 'zod'; export type SendEditBeerPostCommentRequest = (args: { body: { content: string; rating: number }; commentId: string; + beerPostId: string; }) => Promise>; export type SendDeleteBeerPostCommentRequest = (args: { commentId: string; + beerPostId: string; }) => Promise>; export type SendCreateBeerCommentRequest = (args: {