mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Merge pull request #51 from aaronpo97/code-cleanup-add-docs
Code cleanup add docs
This commit is contained in:
@@ -3,7 +3,7 @@ import useCheckIfUserLikesBeerPost from '@/hooks/data-fetching/beer-likes/useChe
|
|||||||
import { FC, useEffect, useState } from 'react';
|
import { FC, useEffect, useState } from 'react';
|
||||||
|
|
||||||
import useGetBeerPostLikeCount from '@/hooks/data-fetching/beer-likes/useBeerPostLikeCount';
|
import useGetBeerPostLikeCount from '@/hooks/data-fetching/beer-likes/useBeerPostLikeCount';
|
||||||
import sendBeerPostLikeRequest from '@/requests/BeerPost/sendBeerPostLikeRequest';
|
import sendBeerPostLikeRequest from '@/requests/BeerPostLike/sendBeerPostLikeRequest';
|
||||||
import LikeButton from '../ui/LikeButton';
|
import LikeButton from '../ui/LikeButton';
|
||||||
|
|
||||||
const BeerPostLikeButton: FC<{
|
const BeerPostLikeButton: FC<{
|
||||||
|
|||||||
@@ -28,13 +28,13 @@ const checkIfCommentOwner = async (
|
|||||||
) => {
|
) => {
|
||||||
const { id } = req.query;
|
const { id } = req.query;
|
||||||
const user = req.user!;
|
const user = req.user!;
|
||||||
const comment = await findBeerCommentById(id);
|
const comment = await findBeerCommentById({ beerCommentId: id });
|
||||||
|
|
||||||
if (!comment) {
|
if (!comment) {
|
||||||
throw new ServerError('Comment not found', 404);
|
throw new ServerError('Comment not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (comment.postedById !== user.id) {
|
if (comment.postedBy.id !== user.id) {
|
||||||
throw new ServerError('You are not authorized to modify this comment', 403);
|
throw new ServerError('You are not authorized to modify this comment', 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,10 +53,11 @@ 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 getAllBeerComments(
|
const comments = await getAllBeerComments({
|
||||||
{ id: beerPostId },
|
beerPostId,
|
||||||
{ pageSize: parseInt(page_size, 10), pageNum: parseInt(page_num, 10) },
|
pageNum: parseInt(page_num, 10),
|
||||||
);
|
pageSize: parseInt(page_size, 10),
|
||||||
|
});
|
||||||
|
|
||||||
const pageCount = await DBClient.instance.beerComment.count({ where: { beerPostId } });
|
const pageCount = await DBClient.instance.beerComment.count({ where: { beerPostId } });
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ const sendLikeRequest = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (alreadyLiked) {
|
if (alreadyLiked) {
|
||||||
await removeBeerPostLikeById(alreadyLiked.id);
|
await removeBeerPostLikeById({ beerLikeId: alreadyLiked.id });
|
||||||
jsonResponse.message = 'Successfully unliked beer post';
|
jsonResponse.message = 'Successfully unliked beer post';
|
||||||
} else {
|
} else {
|
||||||
await createBeerPostLike({ id, user });
|
await createBeerPostLike({ id, user });
|
||||||
@@ -53,7 +53,7 @@ const getLikeCount = async (
|
|||||||
) => {
|
) => {
|
||||||
const id = req.query.id as string;
|
const id = req.query.id as string;
|
||||||
|
|
||||||
const likeCount = await getBeerPostLikeCount(id);
|
const likeCount = await getBeerPostLikeCount({ beerPostId: id });
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import getAllBeerPosts from '@/services/BeerPost/getAllBeerPosts';
|
import getAllBeerPosts from '@/services/BeerPost/getAllBeerPosts';
|
||||||
|
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
@@ -8,10 +9,7 @@ import { createRouter } from 'next-connect';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface GetBeerPostsRequest extends NextApiRequest {
|
interface GetBeerPostsRequest extends NextApiRequest {
|
||||||
query: {
|
query: z.infer<typeof PaginatedQueryResponseSchema>;
|
||||||
page_num: string;
|
|
||||||
page_size: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBeerPosts = async (
|
const getBeerPosts = async (
|
||||||
@@ -41,10 +39,7 @@ const router = createRouter<
|
|||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
validateRequest({
|
validateRequest({
|
||||||
querySchema: z.object({
|
querySchema: PaginatedQueryResponseSchema,
|
||||||
page_num: z.string().regex(/^\d+$/),
|
|
||||||
page_size: z.string().regex(/^\d+$/),
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
getBeerPosts,
|
getBeerPosts,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import getAllBeerStyles from '@/services/BeerStyles/getAllBeerStyles';
|
import getAllBeerStyles from '@/services/BeerStyles/getAllBeerStyles';
|
||||||
|
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
@@ -8,7 +9,7 @@ import { createRouter } from 'next-connect';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface GetBeerStylesRequest extends NextApiRequest {
|
interface GetBeerStylesRequest extends NextApiRequest {
|
||||||
query: { page_num: string; page_size: string };
|
query: z.infer<typeof PaginatedQueryResponseSchema>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBeerStyles = async (
|
const getBeerStyles = async (
|
||||||
@@ -18,7 +19,7 @@ const getBeerStyles = 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 beerStyles = await getAllBeerStyles(pageNum, pageSize);
|
const beerStyles = await getAllBeerStyles({ pageNum, pageSize });
|
||||||
const beerStyleCount = await DBClient.instance.beerStyle.count();
|
const beerStyleCount = await DBClient.instance.beerStyle.count();
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', beerStyleCount);
|
res.setHeader('X-Total-Count', beerStyleCount);
|
||||||
@@ -38,10 +39,7 @@ const router = createRouter<
|
|||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
validateRequest({
|
validateRequest({
|
||||||
querySchema: z.object({
|
querySchema: PaginatedQueryResponseSchema,
|
||||||
page_num: z.string().regex(/^\d+$/),
|
|
||||||
page_size: z.string().regex(/^\d+$/),
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
getBeerStyles,
|
getBeerStyles,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import getAllBreweryPosts from '@/services/BreweryPost/getAllBreweryPosts';
|
import getAllBreweryPosts from '@/services/BreweryPost/getAllBreweryPosts';
|
||||||
|
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
@@ -8,10 +9,7 @@ import { createRouter } from 'next-connect';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface GetBreweryPostsRequest extends NextApiRequest {
|
interface GetBreweryPostsRequest extends NextApiRequest {
|
||||||
query: {
|
query: z.infer<typeof PaginatedQueryResponseSchema>;
|
||||||
page_num: string;
|
|
||||||
page_size: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBreweryPosts = async (
|
const getBreweryPosts = async (
|
||||||
@@ -25,7 +23,6 @@ const getBreweryPosts = async (
|
|||||||
const breweryPostCount = await DBClient.instance.breweryPost.count();
|
const breweryPostCount = await DBClient.instance.breweryPost.count();
|
||||||
|
|
||||||
res.setHeader('X-Total-Count', breweryPostCount);
|
res.setHeader('X-Total-Count', breweryPostCount);
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'Brewery posts retrieved successfully',
|
message: 'Brewery posts retrieved successfully',
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
@@ -40,12 +37,7 @@ const router = createRouter<
|
|||||||
>();
|
>();
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
validateRequest({
|
validateRequest({ querySchema: PaginatedQueryResponseSchema }),
|
||||||
querySchema: z.object({
|
|
||||||
page_num: z.string().regex(/^\d+$/),
|
|
||||||
page_size: z.string().regex(/^\d+$/),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
getBreweryPosts,
|
getBreweryPosts,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BreweryPostMapQueryResult from '@/services/BreweryPost/schema/BreweryPostMapQueryResult';
|
import BreweryPostMapQueryResult from '@/services/BreweryPost/schema/BreweryPostMapQueryResult';
|
||||||
|
import PaginatedQueryResponseSchema from '@/services/schema/PaginatedQueryResponseSchema';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
@@ -8,10 +9,7 @@ import { createRouter } from 'next-connect';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface GetBreweryPostsRequest extends NextApiRequest {
|
interface GetBreweryPostsRequest extends NextApiRequest {
|
||||||
query: {
|
query: z.infer<typeof PaginatedQueryResponseSchema>;
|
||||||
page_num: string;
|
|
||||||
page_size: string;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBreweryPosts = async (
|
const getBreweryPosts = async (
|
||||||
@@ -28,12 +26,7 @@ const getBreweryPosts = async (
|
|||||||
await DBClient.instance.breweryPost.findMany({
|
await DBClient.instance.breweryPost.findMany({
|
||||||
select: {
|
select: {
|
||||||
location: {
|
location: {
|
||||||
select: {
|
select: { coordinates: true, city: true, country: true, stateOrProvince: true },
|
||||||
coordinates: true,
|
|
||||||
city: true,
|
|
||||||
country: true,
|
|
||||||
stateOrProvince: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
id: true,
|
id: true,
|
||||||
name: true,
|
name: true,
|
||||||
@@ -59,12 +52,7 @@ const router = createRouter<
|
|||||||
>();
|
>();
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
validateRequest({
|
validateRequest({ querySchema: PaginatedQueryResponseSchema }),
|
||||||
querySchema: z.object({
|
|
||||||
page_num: z.string().regex(/^\d+$/),
|
|
||||||
page_size: z.string().regex(/^\d+$/),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
getBreweryPosts,
|
getBreweryPosts,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,17 @@ const BeerCommentValidationSchemaWithId = CreateCommentValidationSchema.extend({
|
|||||||
beerPostId: z.string().cuid(),
|
beerPostId: z.string().cuid(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a POST request to the server to create a new beer comment.
|
||||||
|
*
|
||||||
|
* @param data The data to be sent to the server.
|
||||||
|
* @param data.beerPostId The ID of the beer post to comment on.
|
||||||
|
* @param data.content The content of the comment.
|
||||||
|
* @param data.rating The rating of the beer.
|
||||||
|
* @returns A promise that resolves to the created comment.
|
||||||
|
* @throws An error if the request fails, the API response is invalid, or the API response
|
||||||
|
* payload is invalid.
|
||||||
|
*/
|
||||||
const sendCreateBeerCommentRequest = async ({
|
const sendCreateBeerCommentRequest = async ({
|
||||||
beerPostId,
|
beerPostId,
|
||||||
content,
|
content,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||||
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
interface SendUploadBeerImagesRequestArgs {
|
interface SendUploadBeerImagesRequestArgs {
|
||||||
@@ -6,6 +7,15 @@ interface SendUploadBeerImagesRequestArgs {
|
|||||||
images: FileList;
|
images: FileList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a POST request to the server to upload images for a beer post.
|
||||||
|
*
|
||||||
|
* @param beerPost The beer post object.
|
||||||
|
* @param images The list of images to upload.
|
||||||
|
* @returns A promise that resolves to the response from the server.
|
||||||
|
* @throws An error if the upload fails or the API response is invalid.
|
||||||
|
*/
|
||||||
|
|
||||||
const sendUploadBeerImagesRequest = async ({
|
const sendUploadBeerImagesRequest = async ({
|
||||||
beerPost,
|
beerPost,
|
||||||
images,
|
images,
|
||||||
@@ -28,7 +38,14 @@ const sendUploadBeerImagesRequest = async ({
|
|||||||
throw new Error('Failed to upload images');
|
throw new Error('Failed to upload images');
|
||||||
}
|
}
|
||||||
|
|
||||||
return uploadResponse.json();
|
const json = await uploadResponse.json();
|
||||||
|
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||||
|
|
||||||
|
if (!parsed.success) {
|
||||||
|
throw new Error('Invalid API response');
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default sendUploadBeerImagesRequest;
|
export default sendUploadBeerImagesRequest;
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a DELETE request to the server to delete a beer post with the given ID.
|
||||||
|
*
|
||||||
|
* @param id The ID of the beer post to delete.
|
||||||
|
* @returns A Promise that resolves to the parsed API response.
|
||||||
|
* @throws An error if the response fails or the API response is invalid.
|
||||||
|
*/
|
||||||
const deleteBeerPostRequest = async (id: string) => {
|
const deleteBeerPostRequest = async (id: string) => {
|
||||||
const response = await fetch(`/api/beers/${id}`, {
|
const response = await fetch(`/api/beers/${id}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
|||||||
@@ -3,14 +3,45 @@ import CreateBeerPostValidationSchema from '@/services/BeerPost/schema/CreateBee
|
|||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
const sendCreateBeerPostRequest = async (
|
/**
|
||||||
data: z.infer<typeof CreateBeerPostValidationSchema>,
|
* Sends a POST request to the server to create a new beer post.
|
||||||
) => {
|
*
|
||||||
|
* @param data Data containing the beer post information to be sent to the server.
|
||||||
|
* @param abv The alcohol by volume of the beer.
|
||||||
|
* @param breweryId The ID of the brewery that produces the beer.
|
||||||
|
* @param description The description of the beer.
|
||||||
|
* @param ibu The International Bitterness Units of the beer.
|
||||||
|
* @param name The name of the beer.
|
||||||
|
* @param styleId The ID of the beer style.
|
||||||
|
* @returns A Promise that resolves to the created beer post.
|
||||||
|
* @throws An error if the request fails, the API response is invalid, or the API response
|
||||||
|
* payload is invalid.
|
||||||
|
*/
|
||||||
|
const sendCreateBeerPostRequest = async ({
|
||||||
|
abv,
|
||||||
|
breweryId,
|
||||||
|
description,
|
||||||
|
ibu,
|
||||||
|
name,
|
||||||
|
styleId,
|
||||||
|
}: z.infer<typeof CreateBeerPostValidationSchema>) => {
|
||||||
const response = await fetch('/api/beers/create', {
|
const response = await fetch('/api/beers/create', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify({
|
||||||
|
abv,
|
||||||
|
breweryId,
|
||||||
|
description,
|
||||||
|
ibu,
|
||||||
|
name,
|
||||||
|
styleId,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
const parsed = APIResponseValidationSchema.safeParse(json);
|
const parsed = APIResponseValidationSchema.safeParse(json);
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,30 @@ import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPos
|
|||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
async function sendEditBeerPostRequest(
|
/**
|
||||||
data: z.infer<typeof EditBeerPostValidationSchema>,
|
* Sends a PUT request to the server to update a beer post.
|
||||||
) {
|
*
|
||||||
const response = await fetch(`/api/beers/${data.id}`, {
|
* @param data Data containing the updated beer post information to be sent to the server.
|
||||||
|
* @param data.abv The updated ABV of the beer.
|
||||||
|
* @param data.description The updated description of the beer.
|
||||||
|
* @param data.ibu The updated IBU of the beer.
|
||||||
|
* @param data.id The ID of the beer post to be updated.
|
||||||
|
* @param data.name The updated name of the beer.
|
||||||
|
* @param data.styleId The updated style ID of the beer.
|
||||||
|
* @throws If the response status is not ok or the API response is not successful.
|
||||||
|
*/
|
||||||
|
const sendEditBeerPostRequest = async ({
|
||||||
|
abv,
|
||||||
|
description,
|
||||||
|
ibu,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
styleId,
|
||||||
|
}: z.infer<typeof EditBeerPostValidationSchema>) => {
|
||||||
|
const response = await fetch(`/api/beers/${id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify({ abv, description, ibu, name, styleId, id }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -21,6 +38,6 @@ async function sendEditBeerPostRequest(
|
|||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
throw new Error(parsed.error.message);
|
throw new Error(parsed.error.message);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export default sendEditBeerPostRequest;
|
export default sendEditBeerPostRequest;
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a POST request to the server to like or unlike a beer post.
|
||||||
|
*
|
||||||
|
* @param beerPostId The ID of the beer post to like or unlike.
|
||||||
|
* @returns An object containing a success boolean and a message string.
|
||||||
|
* @throws An error if the response is not ok or if the API response is invalid.
|
||||||
|
*/
|
||||||
const sendBeerPostLikeRequest = async (beerPostId: string) => {
|
const sendBeerPostLikeRequest = async (beerPostId: string) => {
|
||||||
const response = await fetch(`/api/beers/${beerPostId}/like`, {
|
const response = await fetch(`/api/beers/${beerPostId}/like`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -10,7 +17,6 @@ const sendBeerPostLikeRequest = async (beerPostId: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
const parsed = APIResponseValidationSchema.safeParse(data);
|
const parsed = APIResponseValidationSchema.safeParse(data);
|
||||||
|
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
@@ -1,18 +1,21 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
|
import CreateCommentValidationSchema from '../schema/CommentSchema/CreateCommentValidationSchema';
|
||||||
|
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||||
|
|
||||||
const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({
|
const CreateNewBeerCommentServiceSchema = CreateCommentValidationSchema.extend({
|
||||||
userId: z.string().cuid(),
|
userId: z.string().cuid(),
|
||||||
beerPostId: z.string().cuid(),
|
beerPostId: z.string().cuid(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
type CreateNewBeerCommentArgs = z.infer<typeof CreateNewBeerCommentServiceSchema>;
|
||||||
|
|
||||||
const createNewBeerComment = async ({
|
const createNewBeerComment = async ({
|
||||||
content,
|
content,
|
||||||
rating,
|
rating,
|
||||||
beerPostId,
|
beerPostId,
|
||||||
userId,
|
userId,
|
||||||
}: z.infer<typeof CreateNewBeerCommentServiceSchema>) => {
|
}: CreateNewBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
|
||||||
return DBClient.instance.beerComment.create({
|
return DBClient.instance.beerComment.create({
|
||||||
data: {
|
data: {
|
||||||
content,
|
content,
|
||||||
@@ -26,6 +29,7 @@ const createNewBeerComment = async ({
|
|||||||
rating: true,
|
rating: true,
|
||||||
postedBy: { select: { id: true, username: true } },
|
postedBy: { select: { id: true, username: true } },
|
||||||
createdAt: true,
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||||
|
|
||||||
interface EditBeerCommentByIdArgs {
|
interface EditBeerCommentByIdArgs {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -6,17 +8,23 @@ interface EditBeerCommentByIdArgs {
|
|||||||
rating: number;
|
rating: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const editBeerCommentById = async ({ id, content, rating }: EditBeerCommentByIdArgs) => {
|
const editBeerCommentById = async ({
|
||||||
const updated = await DBClient.instance.beerComment.update({
|
id,
|
||||||
|
content,
|
||||||
|
rating,
|
||||||
|
}: EditBeerCommentByIdArgs): Promise<z.infer<typeof BeerCommentQueryResult>> => {
|
||||||
|
return DBClient.instance.beerComment.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: { content, rating, updatedAt: new Date() },
|
||||||
content,
|
select: {
|
||||||
rating,
|
id: true,
|
||||||
updatedAt: new Date(),
|
content: true,
|
||||||
|
rating: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
postedBy: { select: { id: true, username: true, createdAt: true } },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return updated;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default editBeerCommentById;
|
export default editBeerCommentById;
|
||||||
|
|||||||
@@ -1,11 +1,25 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import BeerCommentQueryResult from './schema/BeerCommentQueryResult';
|
||||||
|
|
||||||
const findBeerCommentById = async (id: string) => {
|
interface FindBeerCommentArgs {
|
||||||
const comment = await DBClient.instance.beerComment.findUnique({
|
beerCommentId: string;
|
||||||
where: { id },
|
}
|
||||||
|
|
||||||
|
const findBeerCommentById = async ({
|
||||||
|
beerCommentId,
|
||||||
|
}: FindBeerCommentArgs): Promise<z.infer<typeof BeerCommentQueryResult> | null> => {
|
||||||
|
return DBClient.instance.beerComment.findUnique({
|
||||||
|
where: { id: beerCommentId },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
content: true,
|
||||||
|
rating: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
postedBy: { select: { id: true, username: true, createdAt: true } },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return comment;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default findBeerCommentById;
|
export default findBeerCommentById;
|
||||||
|
|||||||
@@ -1,28 +1,31 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
import CommentQueryResult from '../schema/CommentSchema/CommentQueryResult';
|
||||||
|
|
||||||
const getAllBeerComments = async (
|
interface GetAllBeerCommentsArgs {
|
||||||
{ id }: Pick<z.infer<typeof BeerPostQueryResult>, 'id'>,
|
beerPostId: string;
|
||||||
{ pageSize, pageNum = 0 }: { pageSize: number; pageNum?: number },
|
pageNum: number;
|
||||||
) => {
|
pageSize: number;
|
||||||
const skip = (pageNum - 1) * pageSize;
|
}
|
||||||
const beerComments: z.infer<typeof CommentQueryResult>[] =
|
|
||||||
await DBClient.instance.beerComment.findMany({
|
const getAllBeerComments = async ({
|
||||||
skip,
|
beerPostId,
|
||||||
take: pageSize,
|
pageNum,
|
||||||
where: { beerPostId: id },
|
pageSize,
|
||||||
select: {
|
}: GetAllBeerCommentsArgs): Promise<z.infer<typeof CommentQueryResult>[]> => {
|
||||||
id: true,
|
return DBClient.instance.beerComment.findMany({
|
||||||
content: true,
|
skip: (pageNum - 1) * pageSize,
|
||||||
rating: true,
|
take: pageSize,
|
||||||
createdAt: true,
|
where: { beerPostId },
|
||||||
postedBy: { select: { id: true, username: true, createdAt: true } },
|
orderBy: { createdAt: 'desc' },
|
||||||
},
|
select: {
|
||||||
orderBy: { createdAt: 'desc' },
|
id: true,
|
||||||
});
|
content: true,
|
||||||
return beerComments;
|
rating: true,
|
||||||
|
createdAt: true,
|
||||||
|
postedBy: { select: { id: true, username: true, createdAt: true } },
|
||||||
|
},
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default getAllBeerComments;
|
export default getAllBeerComments;
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
|
||||||
const getBeerCommentCount = async (beerPostId: string) => {
|
interface GetBeerCommentCountArgs {
|
||||||
const count = await DBClient.instance.beerComment.count({
|
beerPostId: string;
|
||||||
where: { beerPostId },
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return count;
|
const getBeerCommentCount = async ({
|
||||||
|
beerPostId,
|
||||||
|
}: GetBeerCommentCountArgs): Promise<number> => {
|
||||||
|
return DBClient.instance.beerComment.count({ where: { beerPostId } });
|
||||||
};
|
};
|
||||||
|
|
||||||
export default getBeerCommentCount;
|
export default getBeerCommentCount;
|
||||||
|
|||||||
15
src/services/BeerComment/schema/BeerCommentQueryResult.ts
Normal file
15
src/services/BeerComment/schema/BeerCommentQueryResult.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const BeerCommentQueryResult = z.object({
|
||||||
|
id: z.string().cuid(),
|
||||||
|
content: z.string(),
|
||||||
|
rating: z.number(),
|
||||||
|
postedBy: z.object({
|
||||||
|
id: z.string().cuid(),
|
||||||
|
username: z.string(),
|
||||||
|
}),
|
||||||
|
createdAt: z.coerce.date(),
|
||||||
|
updatedAt: z.coerce.date().nullable(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export default BeerCommentQueryResult;
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const CreateCommentValidationSchema = z.object({
|
||||||
|
userId: z.string().uuid(),
|
||||||
|
beerPostId: z.string().uuid(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export default CreateCommentValidationSchema;
|
||||||
@@ -19,6 +19,7 @@ const addBeerImageToDB = ({
|
|||||||
userId,
|
userId,
|
||||||
}: ProcessImageDataArgs) => {
|
}: ProcessImageDataArgs) => {
|
||||||
const beerImagePromises: Promise<BeerImage>[] = [];
|
const beerImagePromises: Promise<BeerImage>[] = [];
|
||||||
|
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
beerImagePromises.push(
|
beerImagePromises.push(
|
||||||
DBClient.instance.beerImage.create({
|
DBClient.instance.beerImage.create({
|
||||||
|
|||||||
@@ -5,10 +5,6 @@ import EditBeerPostValidationSchema from './schema/EditBeerPostValidationSchema'
|
|||||||
const schema = EditBeerPostValidationSchema.omit({ id: true });
|
const schema = EditBeerPostValidationSchema.omit({ id: true });
|
||||||
|
|
||||||
export default async function editBeerPostById(id: string, data: z.infer<typeof schema>) {
|
export default async function editBeerPostById(id: string, data: z.infer<typeof schema>) {
|
||||||
const beerPost = await DBClient.instance.beerPost.update({
|
const beerPost = await DBClient.instance.beerPost.update({ where: { id }, data });
|
||||||
where: { id },
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
|
|
||||||
return beerPost;
|
return beerPost;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ const getBeerRecommendations = async ({
|
|||||||
}: GetBeerRecommendationsArgs) => {
|
}: GetBeerRecommendationsArgs) => {
|
||||||
const skip = (pageNum - 1) * pageSize;
|
const skip = (pageNum - 1) * pageSize;
|
||||||
const take = pageSize;
|
const take = pageSize;
|
||||||
|
|
||||||
const beerRecommendations: z.infer<typeof BeerPostQueryResult>[] =
|
const beerRecommendations: z.infer<typeof BeerPostQueryResult>[] =
|
||||||
await DBClient.instance.beerPost.findMany({
|
await DBClient.instance.beerPost.findMany({
|
||||||
where: {
|
where: {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const BeerPostQueryResult = z.object({
|
|||||||
style: z.object({ id: z.string(), name: z.string(), description: z.string() }),
|
style: z.object({ id: z.string(), name: z.string(), description: z.string() }),
|
||||||
postedBy: z.object({ id: z.string(), username: z.string() }),
|
postedBy: z.object({ id: z.string(), username: z.string() }),
|
||||||
createdAt: z.coerce.date(),
|
createdAt: z.coerce.date(),
|
||||||
|
updatedAt: z.coerce.date().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default BeerPostQueryResult;
|
export default BeerPostQueryResult;
|
||||||
|
|||||||
@@ -2,19 +2,14 @@ import DBClient from '@/prisma/DBClient';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import GetUserSchema from '../User/schema/GetUserSchema';
|
import GetUserSchema from '../User/schema/GetUserSchema';
|
||||||
|
|
||||||
const createBeerPostLike = async ({
|
interface CreateBeerPostLikeArgs {
|
||||||
id,
|
|
||||||
user,
|
|
||||||
}: {
|
|
||||||
id: string;
|
id: string;
|
||||||
user: z.infer<typeof GetUserSchema>;
|
user: z.infer<typeof GetUserSchema>;
|
||||||
}) => {
|
}
|
||||||
return DBClient.instance.beerPostLike.create({
|
|
||||||
data: {
|
const createBeerPostLike = async ({ id, user }: CreateBeerPostLikeArgs) =>
|
||||||
beerPost: { connect: { id } },
|
DBClient.instance.beerPostLike.create({
|
||||||
likedBy: { connect: { id: user.id } },
|
data: { beerPost: { connect: { id } }, likedBy: { connect: { id: user.id } } },
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
export default createBeerPostLike;
|
export default createBeerPostLike;
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
|
||||||
const getBeerPostLikeCount = async (beerPostId: string) => {
|
const getBeerPostLikeCount = async ({ beerPostId }: { beerPostId: string }) =>
|
||||||
const count = await DBClient.instance.beerPostLike.count({
|
DBClient.instance.beerPostLike.count({ where: { beerPostId } });
|
||||||
where: { beerPostId },
|
|
||||||
});
|
|
||||||
|
|
||||||
return count;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getBeerPostLikeCount;
|
export default getBeerPostLikeCount;
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
|
||||||
const removeBeerPostLikeById = async (id: string) => {
|
interface RemoveBeerPostLikeArgs {
|
||||||
return DBClient.instance.beerPostLike.delete({
|
beerLikeId: string;
|
||||||
where: {
|
}
|
||||||
id,
|
|
||||||
},
|
const removeBeerPostLikeById = async ({ beerLikeId }: RemoveBeerPostLikeArgs) =>
|
||||||
});
|
DBClient.instance.beerPostLike.delete({ where: { id: beerLikeId } });
|
||||||
};
|
|
||||||
|
|
||||||
export default removeBeerPostLikeById;
|
export default removeBeerPostLikeById;
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import DBClient from '@/prisma/DBClient';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
|
import BeerStyleQueryResult from './schema/BeerStyleQueryResult';
|
||||||
|
|
||||||
const getAllBeerStyles = async (
|
const getAllBeerStyles = async ({
|
||||||
pageNum: number,
|
pageNum,
|
||||||
pageSize: number,
|
pageSize,
|
||||||
): Promise<z.infer<typeof BeerStyleQueryResult>[]> => {
|
}: {
|
||||||
const styles = (await DBClient.instance.beerStyle.findMany({
|
pageNum: number;
|
||||||
|
pageSize: number;
|
||||||
|
}): Promise<z.infer<typeof BeerStyleQueryResult>[]> =>
|
||||||
|
DBClient.instance.beerStyle.findMany({
|
||||||
take: pageSize,
|
take: pageSize,
|
||||||
skip: (pageNum - 1) * pageSize,
|
skip: (pageNum - 1) * pageSize,
|
||||||
select: {
|
select: {
|
||||||
@@ -20,9 +23,6 @@ const getAllBeerStyles = async (
|
|||||||
description: true,
|
description: true,
|
||||||
glassware: { select: { id: true, name: true } },
|
glassware: { select: { id: true, name: true } },
|
||||||
},
|
},
|
||||||
})) as z.infer<typeof BeerStyleQueryResult>[];
|
}) as ReturnType<typeof getAllBeerStyles>;
|
||||||
|
|
||||||
return styles;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getAllBeerStyles;
|
export default getAllBeerStyles;
|
||||||
|
|||||||
8
src/services/schema/PaginatedQueryResponseSchema.ts
Normal file
8
src/services/schema/PaginatedQueryResponseSchema.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const PaginatedQueryResponseSchema = z.object({
|
||||||
|
page_num: z.string(),
|
||||||
|
page_size: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export default PaginatedQueryResponseSchema;
|
||||||
Reference in New Issue
Block a user