-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated Prisma ORM with initial database schema and seeding
- Loading branch information
Showing
13 changed files
with
782 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.