Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review branch #7

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ dist/
lib-cov
coverage

package-lock.json
package-lock.json

server/db/characteristics.csv
1 change: 0 additions & 1 deletion example.env

This file was deleted.

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server-dev": "nodemon server/app.js"
"server-dev": "nodemon server/app.js",
"reviews-pg": "node server/db/postgres.sql"
},
"repository": {
"type": "git",
Expand All @@ -20,10 +21,19 @@
"dependencies": {
"axios": "^1.4.0",
"cors": "^2.8.5",
"csv-parser": "^3.0.0",
"csv-stringify": "^6.4.0",
"csv-writer": "^1.6.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"fs": "^0.0.1-security",
"k6": "^0.0.0",
"mongoose": "^7.2.0",
"morgan": "^1.10.0",
"nodemon": "^2.0.22"
"nodemon": "^2.0.22",
"pg": "^8.11.0",
"pg-protocol": "^1.6.0",
"readline": "^1.3.0"
},
"devDependencies": {
"eslint": "^8.41.0",
Expand Down
12 changes: 0 additions & 12 deletions server/controllers/product.js

This file was deleted.

Empty file removed server/controllers/qa.js
Empty file.
161 changes: 161 additions & 0 deletions server/controllers/reviews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* eslint-disable camelcase */
/* eslint-disable radix */
const reviewPool = require('../db/postgres.js');

module.exports.review = {
getReviews: (req, res) => {
const page = Number(req.query.page) || 1;
const count = Number(req.query.count) || 5;
const productNum = Number(req.query.product_id);
const offset = (page - 1) * count;

reviewPool.query(
`SELECT *
FROM revs
WHERE product_id = $3
ORDER BY date
LIMIT $1 OFFSET $2`,
[count, offset, productNum],
)
.then((results) => {
const resultArr = [];
const promises = results.rows.map((rev) => {
const revObj = {
review_id: rev.id,
rating: rev.rating,
summary: rev.summary,
recommend: rev.recommend,
response: rev.response === 'null' ? null : rev.recommend,
body: rev.body,
date: new Date(rev.date * 1).toISOString(),
reviewer_name: rev.reviewer_name,
helpfulness: rev.helpfulness,
photos: [],
};

return reviewPool.query(
`SELECT *
FROM revphotos
WHERE review_id = $1
LIMIT $2 OFFSET $3`,
[rev.id, count, offset],
).then((photosResult) => {
const photos = photosResult.rows;
revObj.photos = photos;
resultArr.push(revObj);
});
});

Promise.all(promises).then(() => {
const data = {
product: `${productNum}`,
page: `${page}`,
count: `${count}`,
results: resultArr,
};

res.status(201).send(data);
}).catch((err) => {
console.error(err);
res.status(500).send(err);
});
})
.catch((err) => {
console.error(err);
res.status(500).send(err);
});
},
getMeta: (req, res) => {
// eslint-disable-next-line camelcase
const { product_id } = req.query;
const ratingCounts = {};
const recommended = {};
const charholder = {};

const fetchData = async () => {
try {
const ratingQuery = reviewPool.query(
`SELECT rating, COUNT(*) AS count
FROM revs
WHERE product_id = $1
GROUP BY rating`,
[product_id],
);

const recommendedQuery = reviewPool.query(
`SELECT recommend, COUNT(*) AS count
FROM revs
WHERE product_id = $1
GROUP BY recommend`,
[product_id],
);

const characteristicsQuery = reviewPool.query(
`SELECT
c.characteristic_id AS id,
ci.name AS characteristic_name,
AVG(c.value) AS average_value
FROM
charsinfo ci
JOIN
chars c ON ci.id = c.characteristic_id
WHERE
ci.product_id = $1
GROUP BY
c.characteristic_id, ci.name`,
[product_id],
);

const [ratingResult, recommendedResult, characteristicsResult] = await Promise.all([
ratingQuery,
recommendedQuery,
characteristicsQuery,
]);

ratingResult.rows.forEach((row) => {
const { rating } = row;
const count = parseInt(row.count);
ratingCounts[rating] = count;
});

// console.log(ratingCounts);

recommendedResult.rows.forEach((row, index) => {
const count = parseInt(row.count);
recommended[`${index}`] = count;
});

// console.log(recommended);

characteristicsResult.rows.forEach((char) => {
charholder[char.characteristic_name] = {
id: char.id,
value: Number(char.average_value).toFixed(4).toString(),
};
});

// console.log('characteristics', charholder);

// Continue with the rest of your code that depends on the fetched data
} catch (error) {
// console.error('Error fetching data:', error);
}
};
// Call the async function
fetchData()
.then(() => {
res.status(201).send({
product_id,
ratings: ratingCounts,
recommended,
characteristics: charholder,
});
})
.catch((err) => {
res.status(500).send(err);
});
},
// postRev: (req, res) => {
// console.log(req);
// },
};
48 changes: 48 additions & 0 deletions server/db/mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mongoose = require('mongoose');
require('dotenv').config();

// const PORT = process.env.PORT || 3500;
// console.log(process.env.DB_NAME);

mongoose.connect(`mongodb://127.0.0.1:27017/${process.env.DB_NAME}`);

const reviewSchema = new mongoose.Schema(
{
ratings: {
1: Number,
2: Number,
3: Number,
4: Number,
5: Number,
},
recommended: {
0: Number,
},
characteristics: {
Size: {
id: Number,
value: Number,
},
}
results: [{
rating: Number,
summary: String,
response: String,
body: String,
date: Date,
reviewer_name: String,
helpfulness: Number,
photos: [{
url: String,
},
// ...
],
},
// ...
],
},
);

const Review = new mongoose.model('Review', reviewSchema);

module.exports = Review;
80 changes: 80 additions & 0 deletions server/db/postgres.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const { Pool, Client } = require('pg');
const fs = require('fs');
// eslint-disable-next-line import/no-extraneous-dependencies
const csv = require('csv-parser');
// eslint-disable-next-line import/no-extraneous-dependencies
const createCsvWriter = require('csv-writer').createObjectCsvWriter;

// eslint-disable-next-line import/prefer-default-export
const reviewPool = new Pool({
user: process.env.USER,
host: process.env.HOST,
database: process.env.DB_NAME,
port: process.env.DBPORT,
});

async function connectAndCreateSchema() {
const client = new Client({
database: 'reviews',
});

try {
await client.connect();
console.log('Database connection successful');
// Rest of your code...
} catch (error) {
console.error('Error connecting to database:', error);
}

await client.query(
`
CREATE INDEX idx_chars_characteristic_id
ON chars(characteristic_id);

CREATE INDEX idx_revs_product_id
ON revs(product_id);

CREATE INDEX idx_revphotos_review_id
ON revphotos(review_id);

CREATE TABLE charsinfo (
id SERIAL primary key,
product_id int,
name varchar(50)
);

CREATE TABLE revs (
id SERIAL primary key,
product_id int,
rating int,
date bigint,
summary varchar(500),
body varchar(500),
recommend boolean,
reported boolean,
reviewer_name varchar(500),
reviewer_email varchar(500),
response varchar(500) null,
helpfulness int
);

CREATE TABLE chars (
id SERIAL primary key,
characteristic_id SERIAL,
review_id int REFERENCES revs(id),
value int
);

CREATE TABLE revPhotos (
id SERIAL PRIMARY KEY,
review_id INT REFERENCES revs(id),
url VARCHAR(500)
);
`,

);
client.end();
}
// connectAndCreateSchema();

module.exports = reviewPool;
23 changes: 23 additions & 0 deletions server/db/postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DROP TABLE IF EXISTS revs;
DROP TABLE IF EXISTS meta;

CREATE TABLE revs (
review_id SERIAL primary key,
product int,
rating int,
summary varchar(100),
recommend boolean,
response varchar(100),
body varchar(100),
review_name varchar(30),
helpfulness int,
photos JSONB
);

CREATE TABLE meta (
product_id int primary key,
ratings JSONB,
recommended int,
characteristics JSONB
)

3 changes: 1 addition & 2 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require('dotenv').config();
// const path = require('path');

const express = require('express');
const morgan = require('morgan');
Expand All @@ -18,6 +17,6 @@ app.use(express.urlencoded({ extended: true }));
app.use(express.json());

// Set up routes
app.use(routes);
routes(app);

module.exports = app;
Loading