Skip to content

Commit

Permalink
feat: added random product generator
Browse files Browse the repository at this point in the history
  • Loading branch information
JusJira committed Oct 29, 2023
1 parent ab0c40b commit 8ee587f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
36 changes: 36 additions & 0 deletions app/api/random/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { faker } from "@faker-js/faker";
import { db } from "@/lib/db";
import { getDbUser } from "@/lib/getUser";
import { NextRequest, NextResponse } from "next/server";
// or, if desiring a different locale
// import { fakerDE as faker } from '@faker-js/faker';

export const dynamic = "force-dynamic";

export async function GET() {
const dbUser = await getDbUser();
if (!dbUser) return new Response("User isn't authorize", { status: 401 });

const id = dbUser.id;
const name = faker.commerce.product();
const description = faker.commerce.productDescription();
const price = faker.commerce.price({ dec: 0 });
const stock = faker.number.int({ min: 1, max: 99999 });

try {
const image = await fetch("https://source.unsplash.com/random");
const res = await db.product.create({
data: {
ownerId: id,
name: name,
description: description,
price: parseInt(price),
quantity: stock,
image: image.url,
},
});
return NextResponse.json({ status: 200, message: res });
} catch (error) {
return NextResponse.json({ status: 204, message: error });
}
}
3 changes: 1 addition & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
domains: ["lh3.googleusercontent.com","res.cloudinary.com","utfs.io"],
domains: ["lh3.googleusercontent.com","res.cloudinary.com","utfs.io","images.unsplash.com","plus.unsplash.com"],
},
output: "standalone"
}

module.exports = nextConfig
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@faker-js/faker": "^8.2.0",
"@types/node": "^20",
"@types/pluralize": "^0.0.32",
"@types/react": "^18",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ee587f

Please sign in to comment.