Skip to content

Commit

Permalink
Integrated Prisma ORM with initial database schema and seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
kaali001 committed Jul 22, 2024
1 parent e26a431 commit 92c2e1d
Show file tree
Hide file tree
Showing 13 changed files with 782 additions and 127 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,53 @@ Ensure that `Node.js` and `MySQL` are installed on your machine.

4. **Create a configuration file:**

Create a file named `config.env` in the server folder and simply copy the content of `.env.example` into it.
1. Create a file named `config.env` in the server folder and simply copy the content of `.env.example` into it.


2. Replace `user` and `password` from `DATABASE_URL="mysql://user:password@localhost:3306/drawn2shoe" ` with your credential of mysql in the `.env` file.

5. **Setting up the Database:**

<!-- 5. **Setting up the Database:**

Open XAMPP (or any other SQL Db service) and start the Apache and MySQL services. Find and import the database from [here](https://drive.google.com/file/d/1qShqZpEGcdhVmZ7zzAar-tBwhPFomNWW/view?usp=sharing). Make sure to name it as `drawn2shoe`.

<i>NOTE: if you have custom configuration for XAMPP, don't forget to change Db config in `server/data/database.js`</i>
<i>NOTE: if you have custom configuration for XAMPP, don't forget to change Db config in `server/data/database.js`</i> -->

5. **Setting up Prisma:**

- Install Prisma CLI globally:

```bash
npm install -g prisma
```

- Follow the interactive prompts to set up Prisma with your database.

- To generate Prisma client:

```bash
cd ../server
npx prisma generate
```

- To migrate the database:

```bash
cd ../server
npx prisma migrate dev --name init
```

- To seed the database :

To populate initial datasets in the database:

```bash
cd ../server
node prisma/seed.js
```


- Follow the interactive prompts to set up Prisma with your database.

6. **Running the Frontend:**

Expand Down
9 changes: 9 additions & 0 deletions server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings



DATABASE_URL="mysql://user:password@localhost:3306/drawn2shoe"
4 changes: 2 additions & 2 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express";
import { connectDB } from "./data/database.js";
import { PrismaClient } from "@prisma/client"; // Imported PrismaClient
import { config } from "dotenv";
import userrouter from "./routes/user.js";
import productrouter from "./routes/products.js";
Expand All @@ -14,7 +14,7 @@ config({
path: "config.env",
});

export const con = connectDB();
export const prisma = new PrismaClient(); // Initialized PrismaClient

app.use(cors({
origin: true,
Expand Down
50 changes: 28 additions & 22 deletions server/controllers/cart.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import { con } from "../app.js";
import { prisma } from "../app.js"; // Imported prisma

const getcart = (req, res) => {
con.query(`select * FROM cart NATURAL JOIN product NATURAL JOIN shoe WHERE email='${req.user["email"]}'`, (err, result) => {
if (err) {
throw err;
}
return res.status(200).json({
success: true,
data: result
})
const getcart = async (req, res) => {
const cartItems = await prisma.cart.findMany({ // Changed to use Prisma
where: { email: req.user.email },
include: {
product: true,
},
});
}

const addcart = (req, res) => {
return res.status(200).json({
success: true,
data: cartItems,
});
};

const addcart = async (req, res) => {
const { pId, price, size } = req.body;
con.query(`INSERT INTO cart VALUES (${pId}, '${req.user["email"]}', ${size}, 1)`, (err, result) => {
if (err) {
throw err;
}
return res.status(200).json({
success: true,
message: "Added to cart"
})

await prisma.cart.create({ // Changed to use Prisma
data: {
productId: pId,
email: req.user.email,
shoeSize:size,
quantity: 1,
},
});
}

return res.status(200).json({
success: true,
message: "Added to cart",
});
};

export { getcart, addcart };
export { getcart, addcart };
159 changes: 126 additions & 33 deletions server/controllers/products.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,146 @@
import { con } from "../app.js";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const getproducts = async (req, res) => {
try {
const products = await prisma.product.findMany({
select: {
shoeId: true,
productId: true,
shoe: {
select: {
shoename: true,
supplier: {
select: {
supplierName: true
}
},
price: true,
shoeImage: true
}
},
}
});

const result = products.map(product => ({
categoryId: product.shoeId,
pId: product.productId,
shoeName: product.shoe.shoename,
brand: product.shoe.supplier.supplierName,
price: product.shoe.price,
shoeImage: product.shoe.shoeImage
}));

const getproducts = (req, res) => {
con.query(`select P.shoeId as categoryId, productId as pId, S.shoename as shoeName, S1.supplierName as brand, price, P.productImage as shoeImage from product P natural join shoe S natural join supplier S1`, (err, result) => {
if (err) {
throw err;
}
return res.status(200).json({
success: true,
data: result
})
});
}
});
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
};

const getproductbyid = (req, res) => {
const getproductbyid = async (req, res) => {
const { productId } = req.body;
con.query(`select P.shoeId as categoryId, productId as pId, S.shoename as shoeName, S1.supplierName as brand, price, P.productImage as shoeImage from product P natural join shoe S natural join supplier S1 WHERE productId ='${productId}' `, (err, result) => {
if (err) {
throw err;
}

try {
const product = await prisma.product.findUnique({
where: { productId },
select: {
shoeId: true,
productId: true,
shoe: {
select: {
shoename: true,
supplier: {
select: {
supplierName: true
}
},
price: true,
shoeImage: true
}
},
}
});

const result = {
categoryId: product.shoeId,
pId: product.productId,
shoeName: product.shoe.shoename,
brand: product.shoe.supplier.supplierName,
price: product.shoe.price,
shoeImage: product.shoe.shoeImage
};

return res.status(200).json({
success: true,
data: result
})
});
}

const getcategories = (req, res) => {
con.query(`select S1.shoeId as categoryId, S1.shoename, S2.supplierName as brand, S1.shoeImage from shoe S1 NATURAL JOIN supplier S2`, (err, result) => {
if (err) {
throw err;
}
});
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
};

const getcategories = async (req, res) => {
try {
const categories = await prisma.shoe.findMany({
include: {
supplier: true
}
});

const result = categories.map(category => ({
categoryId: category.shoeId,
shoename: category.shoename,
brand: category.supplier.supplierName,
shoeImage: category.shoeImage
}));

return res.status(200).json({
success: true,
data: result
});
})
}
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
};

const createcustomproduct = (req, res) => {
const createcustomproduct = async (req, res) => {
const { description, image } = req.body;
con.query(`INSERT INTO design (description, image) VALUES ('${description}', '${image}') `, (err, result) => {
if (err) {
throw err;
}

try {
await prisma.design.create({
data: {
description,
image,
},
});

return res.status(200).json({
success: true,
message: "Custom design created"
});
})
}
} catch (err) {
console.error(err);
res.status(500).json({
success: false,
message: "Internal Server Error"
});
}
};

export { getproducts, getcategories, getproductbyid, createcustomproduct };
export { getproducts, getcategories, getproductbyid, createcustomproduct };
Loading

0 comments on commit 92c2e1d

Please sign in to comment.