mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Feat: add glassware, abv range, and ibu range to beer style table
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import DBClient from '@/prisma/DBClient';
|
||||
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
@@ -15,6 +16,11 @@ const BeerStyleValidationSchema = z.object({
|
||||
id: z.string().cuid(),
|
||||
username: z.string(),
|
||||
}),
|
||||
glassware: z.object({
|
||||
id: z.string().cuid(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
}),
|
||||
description: z.string(),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date().nullable(),
|
||||
@@ -25,6 +31,9 @@ const CreateBeerStyleValidationSchema = BeerStyleValidationSchema.omit({
|
||||
postedBy: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
glassware: true,
|
||||
}).extend({
|
||||
glasswareId: z.string().cuid(),
|
||||
});
|
||||
|
||||
interface CreateBeerStyleRequest extends UserExtendedNextApiRequest {
|
||||
@@ -43,13 +52,22 @@ const createBeerStyle = async (
|
||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
const { name, description } = req.body;
|
||||
const { name, description, glasswareId } = req.body;
|
||||
|
||||
const glassware = await DBClient.instance.glassware.findUnique({
|
||||
where: { id: glasswareId },
|
||||
});
|
||||
|
||||
if (!glassware) {
|
||||
throw new ServerError('Glassware not found', 404);
|
||||
}
|
||||
|
||||
const newBeerStyle = await DBClient.instance.beerStyle.create({
|
||||
data: {
|
||||
description,
|
||||
name,
|
||||
postedBy: { connect: { id: user.id } },
|
||||
glassware: { connect: { id: glassware.id } },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
28
src/prisma/migrations/20230922054646_/migration.sql
Normal file
28
src/prisma/migrations/20230922054646_/migration.sql
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- Added the required column `glasswareId` to the `BeerStyle` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE "BeerStyle" ADD COLUMN "abvRange" DOUBLE PRECISION[],
|
||||
ADD COLUMN "glasswareId" TEXT NOT NULL,
|
||||
ADD COLUMN "ibuRange" DOUBLE PRECISION[];
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Glassware" (
|
||||
"id" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"description" TEXT NOT NULL,
|
||||
"createdAt" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMPTZ(3),
|
||||
"postedById" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "Glassware_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "BeerStyle" ADD CONSTRAINT "BeerStyle_glasswareId_fkey" FOREIGN KEY ("glasswareId") REFERENCES "Glassware"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Glassware" ADD CONSTRAINT "Glassware_postedById_fkey" FOREIGN KEY ("postedById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -33,6 +33,7 @@ model User {
|
||||
BreweryImage BreweryImage[]
|
||||
BreweryPostLike BreweryPostLike[]
|
||||
Location Location[]
|
||||
Glassware Glassware[]
|
||||
}
|
||||
|
||||
model BeerPost {
|
||||
@@ -93,10 +94,25 @@ model BeerStyle {
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(3)
|
||||
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
|
||||
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
|
||||
glassware Glassware @relation(fields: [glasswareId], references: [id], onDelete: Cascade)
|
||||
glasswareId String
|
||||
postedById String
|
||||
abvRange Float[]
|
||||
ibuRange Float[]
|
||||
beerPosts BeerPost[]
|
||||
}
|
||||
|
||||
model Glassware {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
description String
|
||||
createdAt DateTime @default(now()) @db.Timestamptz(3)
|
||||
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
|
||||
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
|
||||
postedById String
|
||||
BeerStyle BeerStyle[]
|
||||
}
|
||||
|
||||
model Location {
|
||||
id String @id @default(cuid())
|
||||
city String
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { User } from '@prisma/client';
|
||||
import uniq from 'lodash/uniq';
|
||||
import DBClient from '../../DBClient';
|
||||
import beerStyles from '../util/beerStyles';
|
||||
|
||||
@@ -13,28 +14,61 @@ interface BeerStyleData {
|
||||
postedById: string;
|
||||
description: string;
|
||||
createdAt: Date;
|
||||
glasswareId: string;
|
||||
abvRange: [number, number];
|
||||
ibuRange: [number, number];
|
||||
}
|
||||
|
||||
const createNewBeerStyles = async ({ joinData }: CreateNewBeerStylesArgs) => {
|
||||
const { users } = joinData;
|
||||
const prisma = DBClient.instance;
|
||||
|
||||
const beerTypeData: BeerStyleData[] = [];
|
||||
const glasswarePromises: Promise<{
|
||||
id: string;
|
||||
description: string;
|
||||
name: string;
|
||||
}>[] = [];
|
||||
|
||||
const glasswareData = uniq(beerStyles.map((beerStyle) => beerStyle.glassware));
|
||||
|
||||
glasswareData.forEach((glassware) => {
|
||||
const createdAt = faker.date.past({ years: 1 });
|
||||
const user = users[Math.floor(Math.random() * users.length)];
|
||||
const query = prisma.glassware.create({
|
||||
data: {
|
||||
createdAt,
|
||||
description: faker.lorem.paragraph(),
|
||||
name: glassware,
|
||||
postedById: user.id,
|
||||
},
|
||||
select: { id: true, description: true, name: true },
|
||||
});
|
||||
|
||||
glasswarePromises.push(query);
|
||||
});
|
||||
|
||||
const glassware = await Promise.all(glasswarePromises);
|
||||
|
||||
const beerStyleData: BeerStyleData[] = [];
|
||||
beerStyles.forEach((beerStyle) => {
|
||||
const { description, name, ibuRange, abvRange } = beerStyle;
|
||||
const glasswareId = glassware.find((glass) => glass.name === beerStyle.glassware)!.id;
|
||||
|
||||
beerStyles.forEach((beerStyle) => {
|
||||
const createdAt = faker.date.past({ years: 1 });
|
||||
const { description, style: name } = beerStyle;
|
||||
const user = users[Math.floor(Math.random() * users.length)];
|
||||
|
||||
beerTypeData.push({
|
||||
beerStyleData.push({
|
||||
createdAt,
|
||||
description,
|
||||
name,
|
||||
postedById: user.id,
|
||||
glasswareId,
|
||||
ibuRange,
|
||||
abvRange,
|
||||
});
|
||||
});
|
||||
|
||||
await prisma.beerStyle.createMany({ data: beerTypeData, skipDuplicates: true });
|
||||
await prisma.beerStyle.createMany({ data: beerStyleData, skipDuplicates: true });
|
||||
return prisma.beerStyle.findMany();
|
||||
};
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user