Skip to content

Commit

Permalink
Merge branch 'main' into second
Browse files Browse the repository at this point in the history
  • Loading branch information
IkkiOcean authored Nov 10, 2024
2 parents 05db7ba + 86830a3 commit c19c891
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 0 deletions.
51 changes: 51 additions & 0 deletions backend/controllers/shop/sub-controllers/analyticcontroller.js
Original file line number Diff line number Diff line change
@@ -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 });
}
};
29 changes: 29 additions & 0 deletions backend/model/shop/sub-model/productanalysis.jsx
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions backend/model/shop/sub-model/useranalysis.js
Original file line number Diff line number Diff line change
@@ -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;
172 changes: 172 additions & 0 deletions backend/model/shop/sub-model/useranalysiscontroller.js
Original file line number Diff line number Diff line change
@@ -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 });
}
};

0 comments on commit c19c891

Please sign in to comment.