user rate product count update

pull/420/head
Fitsumhelina 10 months ago
parent 0b59f7a8f5
commit 4c7d6a657a

@ -28,48 +28,48 @@
// // **** Questions:2, 3 and 4 are based on the following two arrays:users and products () // // **** Questions:2, 3 and 4 are based on the following two arrays:users and products ()
// const users = [ const users = [
// { {
// _id: "ab12ex", _id: "ab12ex",
// username: "Alex", username: "Alex",
// email: "alex@alex.com", email: "alex@alex.com",
// password: "123123", password: "123123",
// createdAt: "08/01/2020 9:00 AM", createdAt: "08/01/2020 9:00 AM",
// isLoggedIn: false, isLoggedIn: false,
// }, },
// { {
// _id: "fg12cy", _id: "fg12cy",
// username: "Asab", username: "Asab",
// email: "asab@asab.com", email: "asab@asab.com",
// password: "123456", password: "123456",
// createdAt: "08/01/2020 9:30 AM", createdAt: "08/01/2020 9:30 AM",
// isLoggedIn: true, isLoggedIn: true,
// }, },
// { {
// _id: "zwf8md", _id: "zwf8md",
// username: "Brook", username: "Brook",
// email: "brook@brook.com", email: "brook@brook.com",
// password: "123111", password: "123111",
// createdAt: "08/01/2020 9:45 AM", createdAt: "08/01/2020 9:45 AM",
// isLoggedIn: true, isLoggedIn: true,
// }, },
// { {
// _id: "eefamr", _id: "eefamr",
// username: "Martha", username: "Martha",
// email: "martha@martha.com", email: "martha@martha.com",
// password: "123222", password: "123222",
// createdAt: "08/01/2020 9:50 AM", createdAt: "08/01/2020 9:50 AM",
// isLoggedIn: false, isLoggedIn: false,
// }, },
// { {
// _id: "ghderc", _id: "ghderc",
// username: "Thomas", username: "Thomas",
// email: "thomas@thomas.com", email: "thomas@thomas.com",
// password: "123333", password: "123333",
// createdAt: "08/01/2020 10:00 AM", createdAt: "08/01/2020 10:00 AM",
// isLoggedIn: false, isLoggedIn: false,
// }, },
// ]; ];
const products = [ const products = [
{ {
@ -79,7 +79,7 @@ const products = [
price: 200, price: 200,
ratings: [ ratings: [
{ userId: "fg12cy", rate: 5 }, { userId: "fg12cy", rate: 5 },
{ userId: "zwf8md", rate: 4.5 }, { userId: "ab12ex", rate: 4.5 },
], ],
likes: [], likes: [],
}, },
@ -150,35 +150,35 @@ const products = [
// }; // };
// The products array has three elements and each of them has six properties. // The products array has three elements and each of them has six properties.
const product = [ // const product = [
{ // {
_id: "eedfcf", // _id: "eedfcf",
name: "Mobile phone", // name: "Mobile phone",
description: "Huawei Honor", // description: "Huawei Honor",
price: 200, // price: 200,
ratings: [ // ratings: [
{ userId: "fg12cy", rate: 5 }, // { userId: "fg12cy", rate: 5 },
{ userId: "zwf8md", rate: 4.5 }, // { userId: "zwf8md", rate: 4.5 },
], // ],
likes: [], // likes: [],
}, // },
{ // {
_id: "aegfal", // _id: "aegfal",
name: "Laptop", // name: "Laptop",
description: "MacPro: System Darwin", // description: "MacPro: System Darwin",
price: 2500, // price: 2500,
ratings: [], // ratings: [],
likes: ["fg12cy"], // likes: ["fg12cy"],
}, // },
{ // {
_id: "hedfcg", // _id: "hedfcg",
name: "TV", // name: "TV",
description: "Smart TV: Procaster", // description: "Smart TV: Procaster",
price: 400, // price: 400,
ratings: [{ userId: "fg12cy", rate: 5 }, { userId: "zwf8md", rate: 3.5 },], // ratings: [{ userId: "aegfal", rate: 5 }, { userId: "zwf8md", rate: 3.5 },],
likes: ["fg12cy"], // likes: ["fg12cy"],
}, // },
]; // ];
// a. Create a function called rateProduct which rates the product // a. Create a function called rateProduct which rates the product
// const rateproduct = (productId, userid, rate) => { // const rateproduct = (productId, userid, rate) => {
@ -195,6 +195,34 @@ const product = [
// } // }
// }; // };
// const rateProduct = (productId, userId, rate) => {
// const product = products.find((product) => product._id === productId );
// if (product) {
// const user = users.find((users) => users._id === userId);
// if (user) {
// const existingRating = product.ratings.find(
// (rating) => rating.userId === userId
// );
// if(!existingRating){
// product.ratings.push({ userId: users._id, rate });
// console.log("Product rating updated successfully");}
// else{
// console.log("User already rated this product");
// }
// }
// else{
// console.log("User not found");
// }
// }
// else{
// console.log("Product not found");
// }
// }
// rateProduct("aegfal", "ab12ex", 2);
// // b. Create a function called averageRating which calculate the average rating of a product // // b. Create a function called averageRating which calculate the average rating of a product
// const averageRating = (productid, products) => { // const averageRating = (productid, products) => {
// const product = products.find((item) => productid === item._id); // const product = products.find((item) => productid === item._id);
@ -244,6 +272,7 @@ const product = [
// } // }
// } // }
const userRatedProducts = (userId, products) => { const userRatedProducts = (userId, products) => {
const ratedProducts = products.filter((item) => const ratedProducts = products.filter((item) =>
@ -252,6 +281,7 @@ const userRatedProducts = (userId, products) => {
return ratedProducts.map((item) => item.name); return ratedProducts.map((item) => item.name);
}; };
const ratedByUser = userRatedProducts("zwf8md", product); const ratedByUser = userRatedProducts("ab12ex", products);
console.log(ratedByUser); console.log(ratedByUser);

Loading…
Cancel
Save