mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 01:40:15 +01:00
feat: Refactor FavoritesController to use model methods for user updates and retrieval
This commit is contained in:
parent
df46b3875c
commit
c006630a45
1 changed files with 4 additions and 8 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
const { User } = require('~/db/models');
|
const { updateUser, getUserById } = require('~/models');
|
||||||
|
|
||||||
const updateFavoritesController = async (req, res) => {
|
const updateFavoritesController = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -30,11 +30,7 @@ const updateFavoritesController = async (req, res) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await User.findByIdAndUpdate(
|
const user = await updateUser(userId, { favorites });
|
||||||
userId,
|
|
||||||
{ $set: { favorites } },
|
|
||||||
{ new: true, select: 'favorites' },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ message: 'User not found' });
|
return res.status(404).json({ message: 'User not found' });
|
||||||
|
|
@ -50,7 +46,7 @@ const updateFavoritesController = async (req, res) => {
|
||||||
const getFavoritesController = async (req, res) => {
|
const getFavoritesController = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
const user = await User.findById(userId).select('favorites').lean();
|
const user = await getUserById(userId, 'favorites');
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ message: 'User not found' });
|
return res.status(404).json({ message: 'User not found' });
|
||||||
|
|
@ -60,7 +56,7 @@ const getFavoritesController = async (req, res) => {
|
||||||
|
|
||||||
if (!Array.isArray(favorites)) {
|
if (!Array.isArray(favorites)) {
|
||||||
favorites = [];
|
favorites = [];
|
||||||
await User.findByIdAndUpdate(userId, { $set: { favorites: [] } });
|
await updateUser(userId, { favorites: [] });
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json(favorites);
|
res.status(200).json(favorites);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue