From 96b68305834281a405f2e95f985722e4eab7c364 Mon Sep 17 00:00:00 2001 From: Ikki Date: Sun, 10 Nov 2024 18:48:32 +0530 Subject: [PATCH] analysisc --- .../sub-controllers/analyticcontroller.js | 51 ++++++ .../model/shop/sub-model/productanalysis.jsx | 29 +++ backend/model/shop/sub-model/useranalysis.js | 25 +++ .../shop/sub-model/useranalysiscontroller.js | 172 ++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 backend/controllers/shop/sub-controllers/analyticcontroller.js create mode 100644 backend/model/shop/sub-model/productanalysis.jsx create mode 100644 backend/model/shop/sub-model/useranalysis.js create mode 100644 backend/model/shop/sub-model/useranalysiscontroller.js diff --git a/backend/controllers/shop/sub-controllers/analyticcontroller.js b/backend/controllers/shop/sub-controllers/analyticcontroller.js new file mode 100644 index 00000000..805ee9cd --- /dev/null +++ b/backend/controllers/shop/sub-controllers/analyticcontroller.js @@ -0,0 +1,51 @@ +const ProductAnalysis = require("../models/ProductAnalysis"); + +exports.createProductAnalysis = async (req, res) => { + try { + const { productId, views, sales, ratings, feedback } = req.body; + const productAnalysis = new ProductAnalysis({ + productId, + views, + sales, + ratings, + feedback, + }); + await productAnalysis.save(); + res.status(201).json(productAnalysis); + } catch (error) { + res + .status(400) + .json({ message: "Error creating product analysis data", error }); + } +}; + +exports.getProductAnalysis = async (req, res) => { + try { + const productAnalysis = await ProductAnalysis.findById(req.params.id); + if (!productAnalysis) + return res.status(404).json({ message: "Product analysis not found" }); + res.status(200).json(productAnalysis); + } catch (error) { + res + .status(400) + .json({ message: "Error retrieving product analysis", error }); + } +}; + +exports.updateProductAnalysis = async (req, res) => { + try { + const updatedData = req.body; + const productAnalysis = await ProductAnalysis.findByIdAndUpdate( + req.params.id, + updatedData, + { new: true } + ); + if (!productAnalysis) + return res.status(404).json({ message: "Product analysis not found" }); + res.status(200).json(productAnalysis); + } catch (error) { + res + .status(400) + .json({ message: "Error updating product analysis data", error }); + } +}; diff --git a/backend/model/shop/sub-model/productanalysis.jsx b/backend/model/shop/sub-model/productanalysis.jsx new file mode 100644 index 00000000..a106cec4 --- /dev/null +++ b/backend/model/shop/sub-model/productanalysis.jsx @@ -0,0 +1,29 @@ +const mongoose = require('mongoose'); + +const productAnalysisSchema = new mongoose.Schema({ + productId: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Product', + required: true, + }, + views: { + type: Number, + default: 0, + }, + sales: { + type: Number, + default: 0, + }, + ratings: { + type: Number, + default: 0, + }, + feedback: { + type: [String], + default: [], + }, +}); + +const ProductAnalysis = mongoose.model('ProductAnalysis', productAnalysisSchema); + +module.exports = ProductAnalysis; diff --git a/backend/model/shop/sub-model/useranalysis.js b/backend/model/shop/sub-model/useranalysis.js new file mode 100644 index 00000000..a9779a7e --- /dev/null +++ b/backend/model/shop/sub-model/useranalysis.js @@ -0,0 +1,25 @@ +const mongoose = require('mongoose'); + +const userAnalysisSchema = new mongoose.Schema({ + userId: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', + required: true, + }, + activityLog: { + type: [String], + default: [], + }, + interactions: { + type: Number, + default: 0, + }, + preferences: { + type: Object, + default: {}, + }, +}); + +const UserAnalysis = mongoose.model('UserAnalysis', userAnalysisSchema); + +module.exports = UserAnalysis; diff --git a/backend/model/shop/sub-model/useranalysiscontroller.js b/backend/model/shop/sub-model/useranalysiscontroller.js new file mode 100644 index 00000000..620ead7b --- /dev/null +++ b/backend/model/shop/sub-model/useranalysiscontroller.js @@ -0,0 +1,172 @@ +const UserAnalysis = require("../models/UserAnalysis"); +const mongoose = require("mongoose"); + +// Utility function to validate ObjectId +const isValidObjectId = (id) => mongoose.Types.ObjectId.isValid(id); + +// Create User Analysis +exports.createUserAnalysis = async (req, res) => { + try { + const { userId, activityLog, interactions, preferences } = req.body; + + // Input validation + if ( + !userId || + !activityLog || + !Array.isArray(activityLog) || + !interactions || + !preferences + ) { + return res + .status(400) + .json({ + message: + "Missing required fields: userId, activityLog, interactions, preferences", + }); + } + + // Check if the userId is valid + if (!isValidObjectId(userId)) { + return res.status(400).json({ message: "Invalid userId format" }); + } + + // Create new UserAnalysis record + const userAnalysis = new UserAnalysis({ + userId, + activityLog, + interactions, + preferences, + }); + await userAnalysis.save(); + res.status(201).json(userAnalysis); + } catch (error) { + console.error(error); // For debugging purposes + if (error.code === 11000) { + return res + .status(409) + .json({ message: "Duplicate entry detected for user analysis" }); + } + res + .status(500) + .json({ message: "Error creating user analysis data", error }); + } +}; + +// Get User Analysis by ID +exports.getUserAnalysis = async (req, res) => { + try { + const { id } = req.params; + + // Check if the id is a valid ObjectId + if (!isValidObjectId(id)) { + return res + .status(400) + .json({ message: "Invalid UserAnalysis ID format" }); + } + + const userAnalysis = await UserAnalysis.findById(id); + + if (!userAnalysis) { + return res.status(404).json({ message: "User analysis not found" }); + } + + res.status(200).json(userAnalysis); + } catch (error) { + console.error(error); // For debugging purposes + res + .status(500) + .json({ message: "Error retrieving user analysis data", error }); + } +}; + +// Update User Analysis by ID +exports.updateUserAnalysis = async (req, res) => { + try { + const { id } = req.params; + const updatedData = req.body; + + // Check if the id is a valid ObjectId + if (!isValidObjectId(id)) { + return res + .status(400) + .json({ message: "Invalid UserAnalysis ID format" }); + } + + // Input validation: Ensure there is at least one field to update + if (!updatedData || Object.keys(updatedData).length === 0) { + return res.status(400).json({ message: "No data provided to update" }); + } + + // Check for fields that shouldn't be updated or are invalid + if (updatedData.userId && !isValidObjectId(updatedData.userId)) { + return res.status(400).json({ message: "Invalid userId format" }); + } + + const userAnalysis = await UserAnalysis.findByIdAndUpdate(id, updatedData, { + new: true, + }); + + if (!userAnalysis) { + return res.status(404).json({ message: "User analysis not found" }); + } + + res.status(200).json(userAnalysis); + } catch (error) { + console.error(error); // For debugging purposes + if (error.code === 11000) { + return res + .status(409) + .json({ message: "Duplicate entry detected during update" }); + } + res + .status(500) + .json({ message: "Error updating user analysis data", error }); + } +}; + +// Delete User Analysis by ID (Optional - In case we want to support deleting analysis data) +exports.deleteUserAnalysis = async (req, res) => { + try { + const { id } = req.params; + + // Check if the id is a valid ObjectId + if (!isValidObjectId(id)) { + return res + .status(400) + .json({ message: "Invalid UserAnalysis ID format" }); + } + + const userAnalysis = await UserAnalysis.findByIdAndDelete(id); + + if (!userAnalysis) { + return res.status(404).json({ message: "User analysis not found" }); + } + + res.status(200).json({ message: "User analysis deleted successfully" }); + } catch (error) { + console.error(error); // For debugging purposes + res + .status(500) + .json({ message: "Error deleting user analysis data", error }); + } +}; + +// Get all User Analysis records (Optional - If we want to list all data) +exports.getAllUserAnalysis = async (req, res) => { + try { + const userAnalysis = await UserAnalysis.find(); + + if (userAnalysis.length === 0) { + return res + .status(404) + .json({ message: "No user analysis data available" }); + } + + res.status(200).json(userAnalysis); + } catch (error) { + console.error(error); // For debugging purposes + res + .status(500) + .json({ message: "Error retrieving all user analysis data", error }); + } +};