mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 17:30:16 +01:00
✨ feat: Implement Favorites functionality with controllers, hooks, and UI components
This commit is contained in:
parent
28cdc06209
commit
d2faf9c67d
19 changed files with 588 additions and 76 deletions
52
api/server/controllers/FavoritesController.js
Normal file
52
api/server/controllers/FavoritesController.js
Normal 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,
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue