mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +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 { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||||
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
|
||||||
|
import ServerError from '@/config/util/ServerError';
|
||||||
import DBClient from '@/prisma/DBClient';
|
import DBClient from '@/prisma/DBClient';
|
||||||
|
|
||||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||||
@@ -15,6 +16,11 @@ const BeerStyleValidationSchema = z.object({
|
|||||||
id: z.string().cuid(),
|
id: z.string().cuid(),
|
||||||
username: z.string(),
|
username: z.string(),
|
||||||
}),
|
}),
|
||||||
|
glassware: z.object({
|
||||||
|
id: z.string().cuid(),
|
||||||
|
name: z.string(),
|
||||||
|
description: z.string(),
|
||||||
|
}),
|
||||||
description: z.string(),
|
description: z.string(),
|
||||||
createdAt: z.date(),
|
createdAt: z.date(),
|
||||||
updatedAt: z.date().nullable(),
|
updatedAt: z.date().nullable(),
|
||||||
@@ -25,6 +31,9 @@ const CreateBeerStyleValidationSchema = BeerStyleValidationSchema.omit({
|
|||||||
postedBy: true,
|
postedBy: true,
|
||||||
createdAt: true,
|
createdAt: true,
|
||||||
updatedAt: true,
|
updatedAt: true,
|
||||||
|
glassware: true,
|
||||||
|
}).extend({
|
||||||
|
glasswareId: z.string().cuid(),
|
||||||
});
|
});
|
||||||
|
|
||||||
interface CreateBeerStyleRequest extends UserExtendedNextApiRequest {
|
interface CreateBeerStyleRequest extends UserExtendedNextApiRequest {
|
||||||
@@ -43,13 +52,22 @@ const createBeerStyle = async (
|
|||||||
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
res: NextApiResponse<z.infer<typeof APIResponseValidationSchema>>,
|
||||||
) => {
|
) => {
|
||||||
const user = req.user!;
|
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({
|
const newBeerStyle = await DBClient.instance.beerStyle.create({
|
||||||
data: {
|
data: {
|
||||||
description,
|
description,
|
||||||
name,
|
name,
|
||||||
postedBy: { connect: { id: user.id } },
|
postedBy: { connect: { id: user.id } },
|
||||||
|
glassware: { connect: { id: glassware.id } },
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
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[]
|
BreweryImage BreweryImage[]
|
||||||
BreweryPostLike BreweryPostLike[]
|
BreweryPostLike BreweryPostLike[]
|
||||||
Location Location[]
|
Location Location[]
|
||||||
|
Glassware Glassware[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model BeerPost {
|
model BeerPost {
|
||||||
@@ -93,10 +94,25 @@ model BeerStyle {
|
|||||||
createdAt DateTime @default(now()) @db.Timestamptz(3)
|
createdAt DateTime @default(now()) @db.Timestamptz(3)
|
||||||
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
|
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
|
||||||
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
|
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
|
||||||
|
glassware Glassware @relation(fields: [glasswareId], references: [id], onDelete: Cascade)
|
||||||
|
glasswareId String
|
||||||
postedById String
|
postedById String
|
||||||
|
abvRange Float[]
|
||||||
|
ibuRange Float[]
|
||||||
beerPosts BeerPost[]
|
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 {
|
model Location {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
city String
|
city String
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||||
import { faker } from '@faker-js/faker';
|
import { faker } from '@faker-js/faker';
|
||||||
import { User } from '@prisma/client';
|
import { User } from '@prisma/client';
|
||||||
|
import uniq from 'lodash/uniq';
|
||||||
import DBClient from '../../DBClient';
|
import DBClient from '../../DBClient';
|
||||||
import beerStyles from '../util/beerStyles';
|
import beerStyles from '../util/beerStyles';
|
||||||
|
|
||||||
@@ -13,28 +14,61 @@ interface BeerStyleData {
|
|||||||
postedById: string;
|
postedById: string;
|
||||||
description: string;
|
description: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
glasswareId: string;
|
||||||
|
abvRange: [number, number];
|
||||||
|
ibuRange: [number, number];
|
||||||
}
|
}
|
||||||
|
|
||||||
const createNewBeerStyles = async ({ joinData }: CreateNewBeerStylesArgs) => {
|
const createNewBeerStyles = async ({ joinData }: CreateNewBeerStylesArgs) => {
|
||||||
const { users } = joinData;
|
const { users } = joinData;
|
||||||
const prisma = DBClient.instance;
|
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 createdAt = faker.date.past({ years: 1 });
|
||||||
const { description, style: name } = beerStyle;
|
|
||||||
const user = users[Math.floor(Math.random() * users.length)];
|
const user = users[Math.floor(Math.random() * users.length)];
|
||||||
|
|
||||||
beerTypeData.push({
|
beerStyleData.push({
|
||||||
createdAt,
|
createdAt,
|
||||||
description,
|
description,
|
||||||
name,
|
name,
|
||||||
postedById: user.id,
|
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();
|
return prisma.beerStyle.findMany();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user