Skip to content

Commit 1619300

Browse files
committed
tried to resolve some of the errors
1 parent 1570327 commit 1619300

File tree

5 files changed

+98
-80
lines changed

5 files changed

+98
-80
lines changed

server/config/data/data.json

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"price": 150.0,
88
"size": 10.5,
99
"color": "White",
10-
"stockquantity": 100,
10+
"stock_quantity": 100,
1111
"category": "Basketball",
12-
"targetaudience": "Men",
13-
"img_url": "https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco/bf80c9bb-3c07-4e5a-ba3f-677cc1889e4d/air-force-1-low-retro-mens-shoes-H7r1WF.png"
12+
"target_audience": "Men",
13+
"image_url": [
14+
"https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco/bf80c9bb-3c07-4e5a-ba3f-677cc1889e4d/air-force-1-low-retro-mens-shoes-H7r1WF.png"
15+
]
1416
},
1517
{
1618
"id": 2,
@@ -20,10 +22,12 @@
2022
"price": 130.0,
2123
"size": 10.5,
2224
"color": "Black",
23-
"stockquantity": 80,
25+
"stock_quantity": 80,
2426
"category": "Basketball",
25-
"targetaudience": "Men",
26-
"img_url": "https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco,u_126ab356-44d8-4a06-89b4-fcdcc8df0245,c_scale,fl_relative,w_1.0,h_1.0,fl_layer_apply/58928cb8-74c0-4ad3-882a-4bfa65bab908/luka-2-basketball-shoes-vcXFrk.png"
27+
"target_audience": "Men",
28+
"image_url": [
29+
"https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco,u_126ab356-44d8-4a06-89b4-fcdcc8df0245,c_scale,fl_relative,w_1.0,h_1.0,fl_layer_apply/58928cb8-74c0-4ad3-882a-4bfa65bab908/luka-2-basketball-shoes-vcXFrk.png"
30+
]
2731
},
2832
{
2933
"id": 3,
@@ -33,9 +37,11 @@
3337
"price": 200.0,
3438
"size": 10.5,
3539
"color": "Silver",
36-
"stockquantity": 120,
40+
"stock_quantity": 120,
3741
"category": "Basketball",
38-
"targetaudience": "Men",
39-
"img_url": "https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco/02fbc834-6752-4778-9a95-1973de02b0b9/lebron-xxi-akoya-basketball-shoes-lnQSsH.png"
42+
"target_audience": "Men",
43+
"image_url": [
44+
"https://static.nike.com/a/images/t_PDP_1728_v1/f_auto,q_auto:eco/02fbc834-6752-4778-9a95-1973de02b0b9/lebron-xxi-akoya-basketball-shoes-lnQSsH.png"
45+
]
4046
}
4147
]

server/config/reset.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ const sneakersFile = fs.readFileSync(
1010
path.join(dirname(currentPath), "../config/data/data.json")
1111
);
1212
const sneakersData = JSON.parse(sneakersFile);
13-
1413
const createSneakersTable = async () => {
1514
const createSneakersTableQuery = `
16-
CREATE TABLE IF NOT EXISTS sneakers (
17-
id serial PRIMARY KEY,
18-
name varchar(100) NOT NULL,
19-
brand varchar(100) NOT NULL,
20-
description text NOT NULL,
21-
price money NOT NULL,
22-
size numeric(7, 2) NOT NULL,
23-
color varchar(50) NOT NULL,
24-
stockquantity integer NOT NULL,
25-
category varchar(50) NOT NULL,
26-
targetaudience varchar(50) NOT NULL
27-
);
15+
CREATE TABLE IF NOT EXISTS sneakers (
16+
id serial PRIMARY KEY,
17+
name varchar(100) NOT NULL,
18+
brand varchar(100) NOT NULL,
19+
description text NOT NULL,
20+
price money NOT NULL,
21+
size numeric(7, 2) NOT NULL,
22+
color varchar(50) NOT NULL,
23+
stock_quantity integer NOT NULL,
24+
category varchar(50) NOT NULL,
25+
target_audience varchar(50) NOT NULL
26+
);
2827
`;
28+
// console.log(sneakersData);
2929

3030
try {
3131
const res = await pool.query(createSneakersTableQuery);
@@ -38,10 +38,10 @@ const createSneakersTable = async () => {
3838
const createImagesTable = async () => {
3939
const createImagesTableQuery = `
4040
CREATE TABLE IF NOT EXISTS images (
41-
imageid serial PRIMARY KEY,
42-
productid integer NOT NULL,
43-
imageurl text NOT NULL,
44-
FOREIGN KEY(productid) REFERENCES sneakers(id)
41+
id serial PRIMARY KEY,
42+
product_id integer NOT NULL,
43+
image_url text NOT NULL,
44+
FOREIGN KEY(product_id) REFERENCES sneakers(id)
4545
);
4646
`;
4747

@@ -56,9 +56,9 @@ const createImagesTable = async () => {
5656
const seedSneakersTable = async () => {
5757
await createSneakersTable();
5858

59-
sneakersData.forEach((sneaker) => {
59+
await sneakersData.forEach((sneaker) => {
6060
const insertQuery = {
61-
text: "INSERT INTO sneakers (name, brand, description, price, size, color, stockquantity, category, targetaudience) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
61+
text: "INSERT INTO sneakers (name, brand, description, price, size, color, stock_quantity, category, target_audience) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
6262
};
6363

6464
const values = [
@@ -68,9 +68,9 @@ const seedSneakersTable = async () => {
6868
sneaker.price,
6969
sneaker.size,
7070
sneaker.color,
71-
sneaker.stockquantity,
71+
sneaker.stock_quantity,
7272
sneaker.category,
73-
sneaker.targetaudience,
73+
sneaker.target_audience,
7474
];
7575

7676
try {
@@ -88,10 +88,10 @@ const seedImagesTable = async () => {
8888

8989
sneakersData.forEach((sneaker) => {
9090
const insertImageQuery = {
91-
text: "INSERT INTO images (productid, imageurl) VALUES ($1, $2)",
91+
text: "INSERT INTO images (product_id, image_url) VALUES ($1, $2)",
9292
};
9393

94-
const imageValues = [sneaker.id, sneaker.img_url];
94+
const imageValues = [sneaker.id, sneaker.image_url[0]];
9595

9696
try {
9797
pool.query(insertImageQuery, imageValues);
@@ -101,15 +101,16 @@ const seedImagesTable = async () => {
101101
}
102102
});
103103
};
104+
104105
const createReviewsTable = async () => {
105106
const createReviewsTableQuery = `
106107
CREATE TABLE IF NOT EXISTS reviews (
107-
reviewid serial PRIMARY KEY,
108-
productid integer NOT NULL,
109-
userid integer NOT NULL,
108+
review_id serial PRIMARY KEY,
109+
product_id integer NOT NULL,
110+
user_id integer NOT NULL,
110111
rating integer NOT NULL,
111-
reviewtext text NOT NULL,
112-
reviewdate date NOT NULL
112+
review_text text NOT NULL,
113+
review_date date NOT NULL
113114
);
114115
`;
115116

@@ -122,12 +123,13 @@ const createReviewsTable = async () => {
122123
};
123124
const createUsersTable = async () => {
124125
const createUsersTableQuery = `
125-
CREATE TABLE IF NOT EXISTS user (
126+
CREATE TABLE IF NOT EXISTS users (
126127
id serial PRIMARY KEY,
127-
googleid integer NOT NULL,
128+
google_id integer UNIQUE NOT NULL,
128129
username varchar(100) NOT NULL,
129130
avatarurl varchar(500) NOT NULL,
130-
accesstoken varchar(500) NOT NULL
131+
access_token varchar(500) NOT NULL,
132+
is_admin boolean NOT NULL
131133
);
132134
`;
133135

@@ -141,15 +143,15 @@ const createUsersTable = async () => {
141143

142144
const createUserAddressTable = async () => {
143145
const createUserAddressTableQuery = `
144-
CREATE TABLE IF NOT EXISTS useraddress (
146+
CREATE TABLE IF NOT EXISTS user_address (
145147
id serial PRIMARY KEY,
146-
userid integer NOT NULL,
148+
user_id integer NOT NULL,
147149
address varchar(255) NOT NULL,
148150
city varchar(100) NOT NULL,
149-
postalcode varchar(10) NOT NULL,
151+
postal_code varchar(10) NOT NULL,
150152
country varchar(100) NOT NULL,
151153
phone varchar(20) NOT NULL,
152-
FOREIGN KEY(userid) REFERENCES user(id)
154+
FOREIGN KEY(userid) REFERENCES users(id)
153155
);
154156
`;
155157

server/controllers/reviews.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const createReview = async (req, res) => {
66
const { userid, rating, reviewtext, reviewdate } = req.body;
77

88
const results = await pool.query(
9-
`INSERT INTO reviews (productid, userid, rating, reviewtext, reviewdate)
9+
`INSERT INTO reviews (product_id, user_id, rating, review_text, review_date)
1010
VALUES($1, $2, $3, $4, $5)
1111
RETURNING *`,
1212
[productid, userid, rating, reviewtext, reviewdate]
@@ -32,7 +32,7 @@ const getProductReviews = async (req, res) => {
3232
try {
3333
const productid = parseInt(req.params.productid);
3434
const results = await pool.query(
35-
"SELECT * FROM reviews WHERE productid = $1",
35+
"SELECT * FROM reviews WHERE product_id = $1",
3636
[productid]
3737
);
3838
res.status(200).json(results.rows);
@@ -47,8 +47,8 @@ const updateReview = async (req, res) => {
4747
const { rating, reviewtext, reviewdate } = req.body;
4848
const results = await pool.query(
4949
`UPDATE reviews
50-
SET rating = $1, reviewtext = $2, reviewdate = $3
51-
WHERE reviewid = $4`,
50+
SET rating = $1, review_text = $2, review_date = $3
51+
WHERE review_id = $4`,
5252
[rating, reviewtext, reviewdate, reviewid]
5353
);
5454
res.status(200).json(results.rows[0]);
@@ -61,7 +61,7 @@ const deleteReview = async (req, res) => {
6161
try {
6262
const reviewid = parseInt(req.params.reviewid);
6363
const results = await pool.query(
64-
"DELETE FROM reviews WHERE reviewid = $1",
64+
"DELETE FROM reviews WHERE review_id = $1",
6565
[reviewid]
6666
);
6767
res.status(200).json(results.rows[0]);

server/controllers/sneakers.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { pool } from "../config/database.js";
2-
32
const createSneaker = async (req, res) => {
43
try {
54
const {
@@ -16,7 +15,7 @@ const createSneaker = async (req, res) => {
1615
} = req.body;
1716

1817
const sneakerResult = await pool.query(
19-
`INSERT INTO sneakers (name, brand, description, price, size, color, stockquantity, category, targetaudience)
18+
`INSERT INTO sneakers (name, brand, description, price, size, color, stock_quantity, category, target_audience)
2019
VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)
2120
RETURNING id`,
2221
[
@@ -35,12 +34,12 @@ const createSneaker = async (req, res) => {
3534
const productId = sneakerResult.rows[0].id;
3635

3736
// Assume imageUrls is an array of image URLs associated with the sneakers
38-
const imageUrls = req.body.img_url || [];
37+
const imageUrls = img_url || [];
3938

4039
// Insert image URLs into the images table
4140
const imageInsertPromises = imageUrls.map(async (imageUrl) => {
4241
await pool.query(
43-
`INSERT INTO images (productid, imageurl)
42+
`INSERT INTO images (product_id, image_url)
4443
VALUES($1, $2)`,
4544
[productId, imageUrl]
4645
);
@@ -59,17 +58,25 @@ const createSneaker = async (req, res) => {
5958

6059
const getSneakers = async (req, res) => {
6160
try {
62-
const results = await pool.query("SELECT * FROM sneakers ORDER BY id ASC");
63-
const sneakers = results.rows;
64-
65-
// Fetch and attach images for each sneaker
66-
for (const sneaker of sneakers) {
67-
const imagesResult = await pool.query(
68-
"SELECT imageurl FROM images WHERE productid = $1",
69-
[sneaker.id]
70-
);
71-
sneaker.images = imagesResult.rows.map((row) => row.imageurl);
72-
}
61+
const results = await pool.query(
62+
"SELECT s.*, i.image_url FROM sneakers s LEFT JOIN images i ON s.id = i.product_id ORDER BY s.id ASC"
63+
);
64+
65+
const sneakers = [];
66+
67+
// Group results by sneaker id
68+
const groupedResults = results.rows.reduce((acc, row) => {
69+
if (!acc[row.id]) {
70+
acc[row.id] = { ...row, images: [] };
71+
sneakers.push(acc[row.id]);
72+
}
73+
74+
if (row.image_url) {
75+
acc[row.id].images.push(row.image_url);
76+
}
77+
78+
return acc;
79+
}, {});
7380

7481
res.status(200).json(sneakers);
7582
} catch (error) {
@@ -81,25 +88,26 @@ const getSneakers = async (req, res) => {
8188
const getSneaker = async (req, res) => {
8289
try {
8390
const id = parseInt(req.params.id);
84-
const sneakerResult = await pool.query(
85-
"SELECT * FROM sneakers WHERE id = $1",
91+
const results = await pool.query(
92+
"SELECT s.*, i.image_url FROM sneakers s LEFT JOIN images i ON s.id = i.productid WHERE s.id = $1",
8693
[id]
8794
);
8895

89-
if (sneakerResult.rows.length === 0) {
96+
if (results.rows.length === 0) {
9097
res.status(404).json({ error: "Sneaker not found" });
91-
} else {
92-
const sneaker = sneakerResult.rows[0];
98+
return;
99+
}
93100

94-
// Fetch and attach images for the sneaker
95-
const imagesResult = await pool.query(
96-
"SELECT imageurl FROM images WHERE productid = $1",
97-
[sneaker.id]
98-
);
99-
sneaker.images = imagesResult.rows.map((row) => row.imageurl);
101+
const sneaker = { ...results.rows[0], images: [] };
100102

101-
res.status(200).json(sneaker);
103+
// Add images to the sneaker object
104+
for (const row of results.rows) {
105+
if (row.image_url) {
106+
sneaker.images.push(row.image_url);
107+
}
102108
}
109+
110+
res.status(200).json(sneaker);
103111
} catch (error) {
104112
console.error(error);
105113
res.status(500).json({ error: "Internal Server Error" });
@@ -124,7 +132,7 @@ const updateSneaker = async (req, res) => {
124132
// Update sneaker details
125133
const updatedSneakerResult = await pool.query(
126134
`UPDATE sneakers
127-
SET name = $1, brand = $2, description = $3, price = $4, size = $5, color = $6, stockquantity = $7, category = $8, targetaudience = $9
135+
SET name = $1, brand = $2, description = $3, price = $4, size = $5, color = $6, stock_quantity = $7, category = $8, target_audience = $9
128136
WHERE id = $10
129137
RETURNING *`,
130138
[
@@ -149,13 +157,13 @@ const updateSneaker = async (req, res) => {
149157
const updatedSneaker = updatedSneakerResult.rows[0];
150158

151159
// Delete existing images for the sneaker
152-
await pool.query("DELETE FROM images WHERE productid = $1", [id]);
160+
await pool.query("DELETE FROM images WHERE product_id = $1", [id]);
153161

154162
// Insert new images for the sneaker
155163
if (img_url && img_url.length > 0) {
156164
const imageValues = img_url.map((img_url) => [id, img_url]);
157165
await pool.query(
158-
"INSERT INTO images (productid, imageurl) VALUES $1:raw",
166+
"INSERT INTO images (product_id, image_url) VALUES $1:raw",
159167
[imageValues]
160168
);
161169
}
@@ -185,7 +193,7 @@ const deleteSneaker = async (req, res) => {
185193
const deletedSneaker = deletedSneakerResult.rows[0];
186194

187195
// Delete associated images
188-
await pool.query("DELETE FROM images WHERE productid = $1", [id]);
196+
await pool.query("DELETE FROM images WHERE product_id = $1", [id]);
189197

190198
res.status(200).json({ message: "Sneaker deleted successfully" });
191199
} catch (error) {

server/routes/order.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ import express from "express";
22
import { createOrderFromCart } from "../controllers/order";
33
const router = express.Router();
44
router.post("/createOrder", createOrderFromCart);
5+
6+
export default router;

0 commit comments

Comments
 (0)