feat: Implement Favorites functionality with controllers, hooks, and UI components

This commit is contained in:
Marco Beretta 2025-11-23 00:27:41 +01:00
parent 28cdc06209
commit d2faf9c67d
No known key found for this signature in database
GPG key ID: D918033D8E74CC11
19 changed files with 588 additions and 76 deletions

View file

@ -0,0 +1,52 @@
const { User } = require('~/db/models');
const updateFavoritesController = async (req, res) => {
try {
const { favorites } = req.body;
const userId = req.user.id;
if (!favorites) {
return res.status(400).json({ message: 'Favorites data is required' });
}
const user = await User.findByIdAndUpdate(
userId,
{ $set: { favorites } },
{ new: true, select: 'favorites' },
);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
res.status(200).json(user.favorites);
} catch (error) {
console.error('Error updating favorites:', error);
res.status(500).json({ message: 'Internal server error' });
}
};
const getFavoritesController = async (req, res) => {
try {
const userId = req.user.id;
const user = await User.findById(userId).select('favorites').lean();
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
const favorites = user.favorites || {};
res.status(200).json({
agents: favorites.agents || [],
models: favorites.models || [],
});
} catch (error) {
console.error('Error fetching favorites:', error);
res.status(500).json({ message: 'Internal server error' });
}
};
module.exports = {
updateFavoritesController,
getFavoritesController,
};