feat(api): create user fav management endpoint

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2023-10-11 16:43:32 -04:00
parent 3ce048ced2
commit 8713f442c1
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

View File

@ -0,0 +1,36 @@
import { FavPayload, SubPayload } from "~/lib/client/types/form/favSub";
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
import { User } from "~/models/user";
export default eventHandler(async (ev) => {
isLoggedIn(ev);
const body = await readBody<FavPayload>(ev);
await User.findByIdAndUpdate(ev.context.currentUser!._id, {
$pull: {
"favs.authors": {
$in: body.pull!.authors,
},
"favs.stories": {
$in: body.pull!.stories,
},
},
});
const nu = await User.findByIdAndUpdate(
ev.context.currentUser!._id,
{
$addToSet: {
"favs.authors": {
$each: body.pull!.authors,
},
"favs.stories": {
$each: body.pull!.stories,
},
},
},
{ new: true },
);
return {
success: true,
data: nu?.favs,
};
});