Update api routes to use authenticated user

This commit is contained in:
Aaron William Po
2023-02-06 19:01:01 -05:00
parent 9a9d8bcb94
commit 3626e3de44
12 changed files with 190 additions and 143 deletions

View File

@@ -2,18 +2,21 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerCommentValidationSchema from './schema/CreateBeerCommentValidationSchema';
const CreateBeerCommentWithUserSchema = BeerCommentValidationSchema.extend({
userId: z.string().uuid(),
});
const createNewBeerComment = async ({
content,
rating,
beerPostId,
}: z.infer<typeof BeerCommentValidationSchema>) => {
const user = await DBClient.instance.user.findFirstOrThrow();
userId,
}: z.infer<typeof CreateBeerCommentWithUserSchema>) => {
return DBClient.instance.beerComment.create({
data: {
content,
rating,
beerPost: { connect: { id: beerPostId } },
postedBy: { connect: { id: user.id } },
postedBy: { connect: { id: userId } },
},
select: {
id: true,

View File

@@ -2,6 +2,10 @@ import DBClient from '@/prisma/DBClient';
import { z } from 'zod';
import BeerPostValidationSchema from './schema/CreateBeerPostValidationSchema';
const CreateBeerPostWithUserSchema = BeerPostValidationSchema.extend({
userId: z.string().uuid(),
});
const createNewBeerPost = async ({
name,
description,
@@ -9,9 +13,8 @@ const createNewBeerPost = async ({
ibu,
typeId,
breweryId,
}: z.infer<typeof BeerPostValidationSchema>) => {
const user = await DBClient.instance.user.findFirstOrThrow();
userId,
}: z.infer<typeof CreateBeerPostWithUserSchema>) => {
const newBeerPost = await DBClient.instance.beerPost.create({
data: {
name,
@@ -19,7 +22,7 @@ const createNewBeerPost = async ({
abv,
ibu,
type: { connect: { id: typeId } },
postedBy: { connect: { id: user.id } },
postedBy: { connect: { id: userId } },
brewery: { connect: { id: breweryId } },
},
});