diff --git a/src/components/Account/UserAvatar.tsx b/src/components/Account/UserAvatar.tsx new file mode 100644 index 0000000..42e837c --- /dev/null +++ b/src/components/Account/UserAvatar.tsx @@ -0,0 +1,27 @@ +import { FC } from 'react'; +import Image from 'next/image'; +import { z } from 'zod'; +import GetUserSchema from '@/services/User/schema/GetUserSchema'; + +interface UserAvatarProps { + user: { + username: z.infer['username']; + userAvatar: z.infer['userAvatar']; + id: z.infer['id']; + }; +} + +const UserAvatar: FC = ({ user }) => { + const { userAvatar } = user; + return !userAvatar ? null : ( + user avatar + ); +}; + +export default UserAvatar; diff --git a/src/components/BeerBreweryComments/CommentCardBody.tsx b/src/components/BeerBreweryComments/CommentCardBody.tsx index 2191983..c874a7f 100644 --- a/src/components/BeerBreweryComments/CommentCardBody.tsx +++ b/src/components/BeerBreweryComments/CommentCardBody.tsx @@ -4,9 +4,9 @@ import { FC, useState } from 'react'; import { useInView } from 'react-intersection-observer'; import { z } from 'zod'; import CreateCommentValidationSchema from '@/services/schema/CommentSchema/CreateCommentValidationSchema'; -import Image from 'next/image'; import CommentContentBody from './CommentContentBody'; import EditCommentBody from './EditCommentBody'; +import UserAvatar from '../Account/UserAvatar'; interface CommentCardProps { comment: z.infer; @@ -28,20 +28,11 @@ const CommentCardBody: FC = ({ }) => { const [inEditMode, setInEditMode] = useState(false); - const { userAvatar } = comment.postedBy; return (
- {!userAvatar ? null : ( - user avatar - )} +
diff --git a/src/pages/api/beers/search.ts b/src/pages/api/beers/search.ts index 80ea329..e088143 100644 --- a/src/pages/api/beers/search.ts +++ b/src/pages/api/beers/search.ts @@ -31,7 +31,16 @@ const search = async (req: SearchAPIRequest, res: NextApiResponse) => { 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 } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, where: { OR: [ diff --git a/src/pages/api/breweries/[id]/beers/index.ts b/src/pages/api/breweries/[id]/beers/index.ts index 7b213c5..a7fe24c 100644 --- a/src/pages/api/breweries/[id]/beers/index.ts +++ b/src/pages/api/breweries/[id]/beers/index.ts @@ -34,7 +34,16 @@ const getAllBeersByBrewery = async ( 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 } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, }); diff --git a/src/services/BeerPost/createNewBeerPost.ts b/src/services/BeerPost/createNewBeerPost.ts index 97475e2..87d19ba 100644 --- a/src/services/BeerPost/createNewBeerPost.ts +++ b/src/services/BeerPost/createNewBeerPost.ts @@ -36,7 +36,16 @@ const createNewBeerPost = ({ ibu: true, createdAt: true, updatedAt: true, - beerImages: { select: { id: true, path: true, caption: true, alt: 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 } }, diff --git a/src/services/BeerPost/deleteBeerPostById.ts b/src/services/BeerPost/deleteBeerPostById.ts index 2344f9b..634712a 100644 --- a/src/services/BeerPost/deleteBeerPostById.ts +++ b/src/services/BeerPost/deleteBeerPostById.ts @@ -19,7 +19,16 @@ const deleteBeerPostById = ({ id: true, name: true, updatedAt: true, - beerImages: { select: { id: true, path: true, caption: true, alt: 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 } }, diff --git a/src/services/BeerPost/editBeerPostById.ts b/src/services/BeerPost/editBeerPostById.ts index bed2629..7f4ae0a 100644 --- a/src/services/BeerPost/editBeerPostById.ts +++ b/src/services/BeerPost/editBeerPostById.ts @@ -25,7 +25,16 @@ const editBeerPostById = ({ ibu: true, createdAt: true, updatedAt: true, - beerImages: { select: { id: true, path: true, caption: true, alt: 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 } }, diff --git a/src/services/BeerPost/getAllBeerPosts.ts b/src/services/BeerPost/getAllBeerPosts.ts index c04ee9e..483f02c 100644 --- a/src/services/BeerPost/getAllBeerPosts.ts +++ b/src/services/BeerPost/getAllBeerPosts.ts @@ -25,7 +25,16 @@ const getAllBeerPosts = ({ style: { select: { name: true, id: true, description: true } }, brewery: { select: { name: true, id: true } }, postedBy: { select: { id: true, username: true } }, - beerImages: { select: { path: true, caption: true, id: true, alt: true } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, take: pageSize, skip: (pageNum - 1) * pageSize, diff --git a/src/services/BeerPost/getBeerPostById.ts b/src/services/BeerPost/getBeerPostById.ts index 57b3759..407c143 100644 --- a/src/services/BeerPost/getBeerPostById.ts +++ b/src/services/BeerPost/getBeerPostById.ts @@ -19,7 +19,16 @@ const getBeerPostById = async ( 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 } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, where: { id }, }); diff --git a/src/services/BeerPost/getBeerPostsByBeerStyleId.ts b/src/services/BeerPost/getBeerPostsByBeerStyleId.ts index adc1fc7..6fabe91 100644 --- a/src/services/BeerPost/getBeerPostsByBeerStyleId.ts +++ b/src/services/BeerPost/getBeerPostsByBeerStyleId.ts @@ -28,7 +28,16 @@ const getBeerPostsByBeerStyleId = async ({ 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 } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, }); diff --git a/src/services/BeerPost/getBeerPostsByBreweryId.ts b/src/services/BeerPost/getBeerPostsByBreweryId.ts index 910391b..2d69ecb 100644 --- a/src/services/BeerPost/getBeerPostsByBreweryId.ts +++ b/src/services/BeerPost/getBeerPostsByBreweryId.ts @@ -28,7 +28,16 @@ const getBeerPostsByBeerStyleId = async ({ 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 } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, }); diff --git a/src/services/BeerPost/getBeerRecommendations.ts b/src/services/BeerPost/getBeerRecommendations.ts index e855548..2fce906 100644 --- a/src/services/BeerPost/getBeerRecommendations.ts +++ b/src/services/BeerPost/getBeerRecommendations.ts @@ -37,7 +37,16 @@ const getBeerRecommendations = async ({ style: { select: { name: true, id: true, description: true } }, brewery: { select: { name: true, id: true } }, postedBy: { select: { id: true, username: true } }, - beerImages: { select: { path: true, caption: true, id: true, alt: true } }, + beerImages: { + select: { + alt: true, + path: true, + caption: true, + id: true, + createdAt: true, + updatedAt: true, + }, + }, }, take, skip, diff --git a/src/services/BeerPost/schema/BeerPostQueryResult.ts b/src/services/BeerPost/schema/BeerPostQueryResult.ts index 9fca4cd..b19441a 100644 --- a/src/services/BeerPost/schema/BeerPostQueryResult.ts +++ b/src/services/BeerPost/schema/BeerPostQueryResult.ts @@ -1,3 +1,4 @@ +import ImageQueryValidationSchema from '@/services/schema/ImageSchema/ImageQueryValidationSchema'; import { z } from 'zod'; const BeerPostQueryResult = z.object({ @@ -5,9 +6,7 @@ const BeerPostQueryResult = z.object({ name: z.string(), brewery: z.object({ id: z.string(), name: z.string() }), description: z.string(), - beerImages: z.array( - z.object({ path: z.string(), caption: z.string(), id: z.string(), alt: z.string() }), - ), + beerImages: z.array(ImageQueryValidationSchema), ibu: z.number(), abv: z.number(), style: z.object({ id: z.string(), name: z.string(), description: z.string() }), diff --git a/src/services/BreweryPost/createNewBreweryPost.ts b/src/services/BreweryPost/createNewBreweryPost.ts index 2d60a3b..cdcc66a 100644 --- a/src/services/BreweryPost/createNewBreweryPost.ts +++ b/src/services/BreweryPost/createNewBreweryPost.ts @@ -37,7 +37,16 @@ const createNewBreweryPost = async ({ createdAt: true, dateEstablished: true, postedBy: { select: { id: true, username: true } }, - breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, + breweryImages: { + select: { + path: true, + caption: true, + id: true, + alt: true, + createdAt: true, + updatedAt: true, + }, + }, location: { select: { city: true, diff --git a/src/services/BreweryPost/getAllBreweryPosts.ts b/src/services/BreweryPost/getAllBreweryPosts.ts index d913aa9..c83883c 100644 --- a/src/services/BreweryPost/getAllBreweryPosts.ts +++ b/src/services/BreweryPost/getAllBreweryPosts.ts @@ -29,7 +29,16 @@ const getAllBreweryPosts = async ({ description: true, name: true, postedBy: { select: { username: true, id: true } }, - breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, + breweryImages: { + select: { + path: true, + caption: true, + id: true, + alt: true, + createdAt: true, + updatedAt: true, + }, + }, createdAt: true, dateEstablished: true, }, diff --git a/src/services/BreweryPost/getBreweryPostById.ts b/src/services/BreweryPost/getBreweryPostById.ts index 55509a4..51b5db9 100644 --- a/src/services/BreweryPost/getBreweryPostById.ts +++ b/src/services/BreweryPost/getBreweryPostById.ts @@ -19,7 +19,16 @@ const getBreweryPostById = async (id: string) => { }, description: true, name: true, - breweryImages: { select: { path: true, caption: true, id: true, alt: true } }, + breweryImages: { + select: { + path: true, + caption: true, + id: true, + alt: true, + createdAt: true, + updatedAt: true, + }, + }, postedBy: { select: { username: true, id: true } }, createdAt: true, dateEstablished: true, diff --git a/src/services/BreweryPost/schema/BreweryPostQueryResult.ts b/src/services/BreweryPost/schema/BreweryPostQueryResult.ts index 21b1874..98ecac0 100644 --- a/src/services/BreweryPost/schema/BreweryPostQueryResult.ts +++ b/src/services/BreweryPost/schema/BreweryPostQueryResult.ts @@ -1,3 +1,4 @@ +import ImageQueryValidationSchema from '@/services/schema/ImageSchema/ImageQueryValidationSchema'; import { z } from 'zod'; const BreweryPostQueryResult = z.object({ @@ -12,9 +13,7 @@ const BreweryPostQueryResult = z.object({ stateOrProvince: z.string().nullable(), }), postedBy: z.object({ id: z.string(), username: z.string() }), - breweryImages: z.array( - z.object({ path: z.string(), caption: z.string(), id: z.string(), alt: z.string() }), - ), + breweryImages: z.array(ImageQueryValidationSchema), createdAt: z.coerce.date(), dateEstablished: z.coerce.date(), }); diff --git a/src/services/User/schema/GetUserSchema.ts b/src/services/User/schema/GetUserSchema.ts index eaf21b7..eca414d 100644 --- a/src/services/User/schema/GetUserSchema.ts +++ b/src/services/User/schema/GetUserSchema.ts @@ -1,3 +1,4 @@ +import ImageQueryValidationSchema from '@/services/schema/ImageSchema/ImageQueryValidationSchema'; import { z } from 'zod'; const GetUserSchema = z.object({ @@ -12,16 +13,7 @@ const GetUserSchema = z.object({ accountIsVerified: z.boolean(), role: z.enum(['USER', 'ADMIN']), bio: z.string().nullable(), - userAvatar: z - .object({ - id: z.string().cuid(), - path: z.string().url(), - alt: z.string(), - caption: z.string(), - createdAt: z.coerce.date(), - updatedAt: z.coerce.date().nullable(), - }) - .nullable(), + userAvatar: ImageQueryValidationSchema.nullable(), }); export default GetUserSchema; diff --git a/src/services/User/schema/PublicUserSchema.tsx b/src/services/User/schema/PublicUserSchema.ts similarity index 100% rename from src/services/User/schema/PublicUserSchema.tsx rename to src/services/User/schema/PublicUserSchema.ts diff --git a/src/services/schema/CommentSchema/CommentQueryResult.ts b/src/services/schema/CommentSchema/CommentQueryResult.ts index bf94cd9..f784076 100644 --- a/src/services/schema/CommentSchema/CommentQueryResult.ts +++ b/src/services/schema/CommentSchema/CommentQueryResult.ts @@ -1,23 +1,17 @@ import { z } from 'zod'; +import ImageQueryValidationSchema from '../ImageSchema/ImageQueryValidationSchema'; const CommentQueryResult = z.object({ id: z.string().cuid(), content: z.string().min(1).max(500), rating: z.number().int().min(1).max(5), createdAt: z.coerce.date(), + updatedAt: z.coerce.date().nullable(), postedBy: z.object({ id: z.string().cuid(), username: z.string().min(1).max(50), - userAvatar: z - .object({ - id: z.string().cuid(), - path: z.string().url(), - alt: z.string().min(1).max(50), - caption: z.string().min(1).max(50), - }) - .nullable(), + userAvatar: ImageQueryValidationSchema.nullable(), }), - updatedAt: z.coerce.date().nullable(), }); export default CommentQueryResult; diff --git a/src/services/schema/ImageSchema/ImageQueryValidationSchema.ts b/src/services/schema/ImageSchema/ImageQueryValidationSchema.ts new file mode 100644 index 0000000..fa6fcb7 --- /dev/null +++ b/src/services/schema/ImageSchema/ImageQueryValidationSchema.ts @@ -0,0 +1,12 @@ +import { z } from 'zod'; + +const ImageQueryValidationSchema = z.object({ + id: z.string().cuid(), + path: z.string().url(), + alt: z.string(), + caption: z.string(), + createdAt: z.coerce.date(), + updatedAt: z.coerce.date().nullable(), +}); + +export default ImageQueryValidationSchema;