mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor codebase, format
This commit is contained in:
@@ -2,6 +2,6 @@
|
||||
"semi": true,
|
||||
"trailingComma": "all",
|
||||
"singleQuote": true,
|
||||
"printWidth": 80,
|
||||
"printWidth": 90,
|
||||
"plugins": ["prettier-plugin-jsdoc"]
|
||||
}
|
||||
|
||||
@@ -9,9 +9,7 @@ import UserContext from '@/contexts/userContext';
|
||||
import sendCheckIfUserLikesBeerPostRequest from '@/requests/sendCheckIfUserLikesBeerPostRequest';
|
||||
import sendLikeRequest from '../../requests/sendLikeRequest';
|
||||
|
||||
const BeerInfoHeader: FC<{ beerPost: BeerPostQueryResult }> = ({
|
||||
beerPost,
|
||||
}) => {
|
||||
const BeerInfoHeader: FC<{ beerPost: BeerPostQueryResult }> = ({ beerPost }) => {
|
||||
const createdAtDate = new Date(beerPost.createdAt);
|
||||
const [timeDistance, setTimeDistance] = useState('');
|
||||
const { user } = useContext(UserContext);
|
||||
@@ -36,9 +34,7 @@ const BeerInfoHeader: FC<{ beerPost: BeerPostQueryResult }> = ({
|
||||
}, [user, beerPost.id]);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeDistance(
|
||||
formatDistanceStrict(new Date(beerPost.createdAt), new Date()),
|
||||
);
|
||||
setTimeDistance(formatDistanceStrict(new Date(beerPost.createdAt), new Date()));
|
||||
}, [beerPost.createdAt]);
|
||||
|
||||
const handleLike = async () => {
|
||||
@@ -66,10 +62,7 @@ const BeerInfoHeader: FC<{ beerPost: BeerPostQueryResult }> = ({
|
||||
|
||||
<h3 className="italic">
|
||||
posted by{' '}
|
||||
<Link
|
||||
href={`/users/${beerPost.postedBy.id}`}
|
||||
className="link-hover link"
|
||||
>
|
||||
<Link href={`/users/${beerPost.postedBy.id}`} className="link-hover link">
|
||||
{beerPost.postedBy.username}{' '}
|
||||
</Link>
|
||||
<span
|
||||
@@ -92,9 +85,7 @@ const BeerInfoHeader: FC<{ beerPost: BeerPostQueryResult }> = ({
|
||||
</Link>
|
||||
</div>
|
||||
<div>
|
||||
<span className="mr-4 text-lg font-medium">
|
||||
{beerPost.abv}% ABV
|
||||
</span>
|
||||
<span className="mr-4 text-lg font-medium">{beerPost.abv}% ABV</span>
|
||||
<span className="text-lg font-medium">{beerPost.ibu} IBU</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -14,17 +14,10 @@ const BeerRecommendations: FunctionComponent<BeerRecommendationsProps> = ({
|
||||
{beerRecommendations.map((beerPost) => (
|
||||
<div key={beerPost.id} className="w-full">
|
||||
<div>
|
||||
<Link
|
||||
className="link-hover"
|
||||
href={`/beers/${beerPost.id}`}
|
||||
scroll={false}
|
||||
>
|
||||
<Link className="link-hover" href={`/beers/${beerPost.id}`} scroll={false}>
|
||||
<h2 className="text-2xl font-bold">{beerPost.name}</h2>
|
||||
</Link>
|
||||
<Link
|
||||
href={`/breweries/${beerPost.brewery.id}`}
|
||||
className="link-hover"
|
||||
>
|
||||
<Link href={`/breweries/${beerPost.brewery.id}`} className="link-hover">
|
||||
<p className="text-lg font-semibold">{beerPost.brewery.name}</p>
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sendLoginUserRequest from '@/requests/sendLoginUserRequest';
|
||||
import LoginValidationSchema from '@/services/user/schema/LoginValidationSchema';
|
||||
import LoginValidationSchema from '@/services/User/schema/LoginValidationSchema';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useForm, SubmitHandler } from 'react-hook-form';
|
||||
@@ -66,7 +66,7 @@ const LoginForm = () => {
|
||||
</div>
|
||||
|
||||
<div className="w-full">
|
||||
<button type="submit" className="btn-primary btn w-full">
|
||||
<button type="submit" className="btn btn-primary w-full">
|
||||
Login
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -26,7 +26,7 @@ const Navbar = () => {
|
||||
return (
|
||||
<nav className="navbar bg-primary">
|
||||
<div className="flex-1">
|
||||
<Link className="btn-ghost btn text-3xl normal-case" href="/">
|
||||
<Link className="btn btn-ghost text-3xl normal-case" href="/">
|
||||
<span className="cursor-pointer text-xl font-bold">The Biergarten App</span>
|
||||
</Link>
|
||||
</div>
|
||||
@@ -51,7 +51,7 @@ const Navbar = () => {
|
||||
</div>
|
||||
<div className="flex-none lg:hidden">
|
||||
<div className="dropdown-end dropdown">
|
||||
<label tabIndex={0} className="btn-ghost btn-circle btn">
|
||||
<label tabIndex={0} className="btn btn-ghost btn-circle">
|
||||
<span className="w-10 rounded-full">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
|
||||
@@ -7,7 +7,7 @@ interface FormButtonProps {
|
||||
|
||||
const Button: FunctionComponent<FormButtonProps> = ({ children, type }) => (
|
||||
// eslint-disable-next-line react/button-has-type
|
||||
<button type={type} className="btn-primary btn mt-4 w-full rounded-xl">
|
||||
<button type={type} className="btn btn-primary mt-4 w-full rounded-xl">
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import findUserByUsername from '@/services/user/findUserByUsername';
|
||||
import findUserByUsername from '@/services/User/findUserByUsername';
|
||||
import Local from 'passport-local';
|
||||
import ServerError from '../util/ServerError';
|
||||
import { validatePassword } from './passwordFns';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import findUserById from '@/services/user/findUserById';
|
||||
import findUserById from '@/services/User/findUserById';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import { getLoginSession } from '../session';
|
||||
import { UserExtendedNextApiRequest } from '../types';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import GetUserSchema from '@/services/user/schema/GetUserSchema';
|
||||
import GetUserSchema from '@/services/User/schema/GetUserSchema';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { NextApiRequest } from 'next';
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -37,7 +37,7 @@ const validateRequest =
|
||||
if (querySchema) {
|
||||
const parsed = querySchema.safeParse(req.query);
|
||||
if (!parsed.success) {
|
||||
throw new ServerError(parsed.error.message, 400);
|
||||
throw new ServerError('Invalid request query.', 400);
|
||||
}
|
||||
req.query = parsed.data;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import GetUserSchema from '@/services/user/schema/GetUserSchema';
|
||||
import GetUserSchema from '@/services/User/schema/GetUserSchema';
|
||||
import { createContext } from 'react';
|
||||
import { z } from 'zod';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import GetUserSchema from '@/services/user/schema/GetUserSchema';
|
||||
import GetUserSchema from '@/services/User/schema/GetUserSchema';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import useSWR from 'swr';
|
||||
|
||||
|
||||
1104
package-lock.json
generated
1104
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -37,7 +37,7 @@
|
||||
"devDependencies": {
|
||||
"@faker-js/faker": "^7.6.0",
|
||||
"@types/cookie": "^0.5.1",
|
||||
"@types/node": "18.11.19",
|
||||
"@types/node": "^18.13.0",
|
||||
"@types/passport-local": "^1.0.35",
|
||||
"@types/react": "18.0.27",
|
||||
"@types/react-dom": "18.0.10",
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
@@ -9,8 +8,11 @@ import nextConnect from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import { NextApiResponse } from 'next';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import createBeerPostLike from '@/services/BeerPostLike/createBeerPostLike';
|
||||
import removeBeerPostLikeById from '@/services/BeerPostLike/removeBeerPostLikeById';
|
||||
import findBeerPostLikeById from '@/services/BeerPostLike/findBeerPostLikeById';
|
||||
|
||||
const likeBeerPost = async (
|
||||
const sendLikeRequest = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
@@ -22,41 +24,23 @@ const likeBeerPost = async (
|
||||
throw new ServerError('Could not find a beer post with that id', 404);
|
||||
}
|
||||
|
||||
const alreadyLiked = await DBClient.instance.beerPostLikes.findFirst({
|
||||
where: {
|
||||
beerPostId: id,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
const alreadyLiked = await findBeerPostLikeById(id);
|
||||
|
||||
const jsonResponse = {
|
||||
success: true as const,
|
||||
message: '',
|
||||
statusCode: 200 as const,
|
||||
};
|
||||
|
||||
if (alreadyLiked) {
|
||||
await DBClient.instance.beerPostLikes.delete({
|
||||
where: {
|
||||
id: alreadyLiked.id,
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Successfully unliked beer post',
|
||||
statusCode: 200,
|
||||
});
|
||||
|
||||
return;
|
||||
await removeBeerPostLikeById(alreadyLiked.id);
|
||||
jsonResponse.message = 'Successfully unliked beer post';
|
||||
} else {
|
||||
await createBeerPostLike({ id, user });
|
||||
jsonResponse.message = 'Successfully liked beer post';
|
||||
}
|
||||
|
||||
await DBClient.instance.beerPostLikes.create({
|
||||
data: {
|
||||
beerPost: { connect: { id } },
|
||||
user: { connect: { id: user.id } },
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'Successfully liked beer post',
|
||||
statusCode: 200,
|
||||
});
|
||||
res.status(200).json(jsonResponse);
|
||||
};
|
||||
|
||||
const handler = nextConnect(NextConnectConfig).post(
|
||||
@@ -66,7 +50,7 @@ const handler = nextConnect(NextConnectConfig).post(
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
}),
|
||||
likeBeerPost,
|
||||
sendLikeRequest,
|
||||
);
|
||||
|
||||
export default handler;
|
||||
|
||||
@@ -15,7 +15,7 @@ const checkIfLiked = async (
|
||||
const user = req.user!;
|
||||
const id = req.query.id as string;
|
||||
|
||||
const alreadyLiked = await DBClient.instance.beerPostLikes.findFirst({
|
||||
const alreadyLiked = await DBClient.instance.beerPostLike.findFirst({
|
||||
where: {
|
||||
beerPostId: id,
|
||||
userId: user.id,
|
||||
@@ -26,19 +26,13 @@ const checkIfLiked = async (
|
||||
success: true,
|
||||
message: alreadyLiked ? 'Beer post is liked.' : 'Beer post is not liked.',
|
||||
statusCode: 200,
|
||||
payload: {
|
||||
isLiked: !!alreadyLiked,
|
||||
},
|
||||
payload: { isLiked: !!alreadyLiked },
|
||||
});
|
||||
};
|
||||
|
||||
const handler = nextConnect(NextConnectConfig).get(
|
||||
getCurrentUser,
|
||||
validateRequest({
|
||||
querySchema: z.object({
|
||||
id: z.string().uuid(),
|
||||
}),
|
||||
}),
|
||||
validateRequest({ querySchema: z.object({ id: z.string().uuid() }) }),
|
||||
checkIfLiked,
|
||||
);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import { setLoginSession } from '@/config/auth/session';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import LoginValidationSchema from '@/services/user/schema/LoginValidationSchema';
|
||||
import LoginValidationSchema from '@/services/User/schema/LoginValidationSchema';
|
||||
import { UserExtendedNextApiRequest } from '../../../config/auth/types';
|
||||
|
||||
export default nextConnect<
|
||||
|
||||
@@ -2,11 +2,11 @@ import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import nc from 'next-connect';
|
||||
import createNewUser from '@/services/user/createNewUser';
|
||||
import CreateUserValidationSchema from '@/services/user/schema/CreateUserValidationSchema';
|
||||
import createNewUser from '@/services/User/createNewUser';
|
||||
import CreateUserValidationSchema from '@/services/User/schema/CreateUserValidationSchema';
|
||||
import NextConnectConfig from '@/config/nextConnect/NextConnectConfig';
|
||||
import findUserByUsername from '@/services/user/findUserByUsername';
|
||||
import findUserByEmail from '@/services/user/findUserByEmail';
|
||||
import findUserByUsername from '@/services/User/findUserByUsername';
|
||||
import findUserByEmail from '@/services/User/findUserByEmail';
|
||||
import validateRequest from '@/config/zod/middleware/validateRequest';
|
||||
|
||||
interface RegisterUserRequest extends NextApiRequest {
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "BeerPostLikes" (
|
||||
"id" TEXT NOT NULL,
|
||||
"beerPostId" TEXT NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMPTZ(3),
|
||||
|
||||
CONSTRAINT "BeerPostLikes_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "BeerPostLikes" ADD CONSTRAINT "BeerPostLikes_beerPostId_fkey" FOREIGN KEY ("beerPostId") REFERENCES "BeerPost"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "BeerPostLikes" ADD CONSTRAINT "BeerPostLikes_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
16
prisma/migrations/20230209085224_/migration.sql
Normal file
16
prisma/migrations/20230209085224_/migration.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "BeerPostLike" (
|
||||
"id" TEXT NOT NULL,
|
||||
"beerPostId" TEXT NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMPTZ(3),
|
||||
|
||||
CONSTRAINT "BeerPostLike_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "BeerPostLike" ADD CONSTRAINT "BeerPostLike_beerPostId_fkey" FOREIGN KEY ("beerPostId") REFERENCES "BeerPost"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "BeerPostLike" ADD CONSTRAINT "BeerPostLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -25,29 +25,29 @@ model User {
|
||||
breweryPosts BreweryPost[]
|
||||
beerComments BeerComment[]
|
||||
breweryComments BreweryComment[]
|
||||
BeerPostLikes BeerPostLikes[]
|
||||
BeerPostLikes BeerPostLike[]
|
||||
}
|
||||
|
||||
model BeerPost {
|
||||
id String @id @default(uuid())
|
||||
id String @id @default(uuid())
|
||||
name String
|
||||
ibu Float
|
||||
abv Float
|
||||
description String
|
||||
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
|
||||
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
|
||||
postedById String
|
||||
brewery BreweryPost @relation(fields: [breweryId], references: [id], onDelete: Cascade)
|
||||
brewery BreweryPost @relation(fields: [breweryId], references: [id], onDelete: Cascade)
|
||||
breweryId String
|
||||
type BeerType @relation(fields: [typeId], references: [id], onDelete: Cascade)
|
||||
type BeerType @relation(fields: [typeId], references: [id], onDelete: Cascade)
|
||||
typeId String
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(3)
|
||||
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(3)
|
||||
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
|
||||
beerComments BeerComment[]
|
||||
beerImages BeerImage[]
|
||||
BeerPostLikes BeerPostLikes[]
|
||||
BeerPostLikes BeerPostLike[]
|
||||
}
|
||||
|
||||
model BeerPostLikes {
|
||||
model BeerPostLike {
|
||||
id String @id @default(uuid())
|
||||
beerPost BeerPost @relation(fields: [beerPostId], references: [id], onDelete: Cascade)
|
||||
beerPostId String
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { BeerPost, BeerPostLikes, User } from '@prisma/client';
|
||||
import type { BeerPost, BeerPostLike, User } from '@prisma/client';
|
||||
import DBClient from '../../DBClient';
|
||||
|
||||
const createNewBeerPostLikes = async ({
|
||||
@@ -11,7 +11,7 @@ const createNewBeerPostLikes = async ({
|
||||
};
|
||||
numberOfLikes: number;
|
||||
}) => {
|
||||
const beerPostLikePromises: Promise<BeerPostLikes>[] = [];
|
||||
const beerPostLikePromises: Promise<BeerPostLike>[] = [];
|
||||
|
||||
// eslint-disable-next-line no-plusplus
|
||||
for (let i = 0; i < numberOfLikes; i++) {
|
||||
@@ -19,7 +19,7 @@ const createNewBeerPostLikes = async ({
|
||||
const user = users[Math.floor(Math.random() * users.length)];
|
||||
|
||||
beerPostLikePromises.push(
|
||||
DBClient.instance.beerPostLikes.create({
|
||||
DBClient.instance.beerPostLike.create({
|
||||
data: {
|
||||
beerPost: { connect: { id: beerPost.id } },
|
||||
user: { connect: { id: user.id } },
|
||||
|
||||
@@ -5,6 +5,7 @@ import BeerCommentValidationSchema from './schema/CreateBeerCommentValidationSch
|
||||
const CreateBeerCommentWithUserSchema = BeerCommentValidationSchema.extend({
|
||||
userId: z.string().uuid(),
|
||||
});
|
||||
|
||||
const createNewBeerComment = async ({
|
||||
content,
|
||||
rating,
|
||||
|
||||
20
services/BeerPostLike/createBeerPostLike.ts
Normal file
20
services/BeerPostLike/createBeerPostLike.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
import { z } from 'zod';
|
||||
import GetUserSchema from '../User/schema/GetUserSchema';
|
||||
|
||||
const createBeerPostLike = async ({
|
||||
id,
|
||||
user,
|
||||
}: {
|
||||
id: string;
|
||||
user: z.infer<typeof GetUserSchema>;
|
||||
}) => {
|
||||
return DBClient.instance.beerPostLike.create({
|
||||
data: {
|
||||
beerPost: { connect: { id } },
|
||||
user: { connect: { id: user.id } },
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default createBeerPostLike;
|
||||
6
services/BeerPostLike/findBeerPostLikeById.ts
Normal file
6
services/BeerPostLike/findBeerPostLikeById.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const findBeerPostLikeById = async (id: string) =>
|
||||
DBClient.instance.beerPostLike.findUnique({ where: { id } });
|
||||
|
||||
export default findBeerPostLikeById;
|
||||
11
services/BeerPostLike/removeBeerPostLikeById.ts
Normal file
11
services/BeerPostLike/removeBeerPostLikeById.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
const removeBeerPostLikeById = async (id: string) => {
|
||||
return DBClient.instance.beerPostLike.delete({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default removeBeerPostLikeById;
|
||||
Reference in New Issue
Block a user