Skip to content

Abstracting Work Into Specific Modules #1

Description

@kennyctran

Reviews-API/src/app.js

Lines 34 to 45 in b7769c4

//get photo data..
const photos = await db.getPhotos(Number(params.product_id));
//parse through it and associate it by key value pairs
const photosData = {};
photos[0].forEach((photo) => {
if (photosData[photo.id] === undefined) {
photosData[photo.id] = [];
}
if (photo.url) {
photosData[photo.id].push(photo.url);
}
})

Reviews-API/src/app.js

Lines 46 to 51 in b7769c4

//initial data
const data = {
product: params.product_id,
page: 0,
count: params.count,
};

Reviews-API/src/app.js

Lines 52 to 71 in b7769c4

//get reviews!!
const reviews = await db.getNReviews(Number(params.product_id), Number(params.count), sort);
const resultsArray = reviews[0].map((row) => {
return {
review_id: row.id,
rating: row.rating,
summary: row.summary,
recommend: row.recommend,
response: row.response,
body: row.body,
date: row.date,
reviewer_name: row.reviewer_name,
helpfulness: row.helpfullness,
photos: photosData[row.id]
}
});
data.results = resultsArray;
res.statusCode = 200;
res.send(data);
});

Reviews-API/src/app.js

Lines 73 to 127 in b7769c4

//get meta reviews
app.get('/reviews/meta', async (req, res) => {
try {
const params = req.query;
client.get(params.product_id, async (err, metaData) => {
if (err) {
console.log(err);
res.statusCode = 500;
res.send(err);
}
if (metaData !== null) {
res.statusCode = 200;
return res.json(JSON.parse(metaData));
} else {
data = {
product_id: params.product_id,
ratings: {},
recommended: {0:0, 1:0}
};
const characteristics = await db.getCharacteristics(params.product_id);
characteristics[0].forEach((row) => {
if (data[row.name] === undefined) {
data[row.name] = {
id: row.id,
value: row.value
};
} else {
let current = data[row.name].value;
data[row.name].value = (current + row.value) / 2;
}
})
const reviews = await db.getAllReviews(Number(params.product_id));
reviews[0].forEach((row) => {
if (row.recommend === 1) {
data.recommended['1'] += 1;
} else {
data.recommended['0'] += 1;
}
if (!(data.ratings[row.rating])) {
data.ratings[row.rating] = 1;
} else {
data.ratings[row.rating] += 1;
}
})
client.setex(params.product_id, 1000, JSON.stringify(data));
res.statusCode = 200;
res.send(data);
}
});
} catch(err) {
res.statusCode = 404;
res.send(err);
}
});

Reviews-API/src/app.js

Lines 129 to 166 in b7769c4

//write to database
app.post('/reviews', async (req, res) => {
try {
const body = req.body;
const array = [
Number(body.product_id),
Number(body.rating),
helper.generateDate(),
body.summary,
body.body,
((body.recommend === 'true') ? 1 : 0),
1,
body.name,
body.email,
null,
0
]
await db.addAReview(array);
const lastInsertIdRow = await db.lastInsertId();
const lastInsertId = lastInsertIdRow[0][0].s;
//add photos
body.photos.forEach(async (photo) => {
await db.addAPhoto([lastInsertId, photo]);
})
//add characteristics
const characteristicIds = Object.keys(body.characteristics);
const characteristicValues = Object.values(body.characteristics);
for (let i = 0; i < characteristicIds.length; i++) {
await db.addACharacteristicsReviews([Number(characteristicIds[i]), Number(lastInsertId), Number(characteristicValues[i])]);
}
res.statusCode = 200;
res.send('review added!!');
} catch(err) {
res.statusCode = 404;
res.send(err);
}
});

There is some work here that can be broken up into smaller modules/functions. When you're doing work like parsing data or getting reviews, that can be its own module.

I'd also ask, are there tools we can use to separate routes into their own modules? We should be thinking of the MVC style at least and thinking about how to create predictable structures. When we go into app.js do we intend to find ALL the work that the server is doing? Or maybe we only find the initialization of the server while the routes get offloaded to other modules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions