Brandon Leichty
06/09/2021, 4:46 PMuserId of the currently logged in user matches the quoteId that the user is looking to update/delete.
This is what I'm currently doing (and it works). But I'm wondering if there's a way I can do this without multiple calls to the database? Would be great if I could do something like prisma.update({ where: {id AND userId}}) 👈 pseudocode.
editQuote: async (_, { ...input }, context) => {
const { prisma, user } = context;
const { id, quote, author, categories, favorite } = input;
const originalQuote = await prisma.quote.findUnique({
where: { id },
});
// Check to see if the logged in user owns this quote before updating
if (user == originalQuote.userId) {
const editedQuote = await prisma.quote
.update({
where: {
id: id,
},
data: {
quote,
author,
},
})
.catch((e) => {
throw e;
});
return editedQuote;
} else {
console.log("You are not authorized to edit this quote");
return null;
}
},Samrith Shankar
06/09/2021, 5:10 PMif(await prisma.quote.count({ where: { id, userId: user })) {
return await prisma.quote.update.....
}
return null;Samrith Shankar
06/09/2021, 5:12 PMAndre Oliveira
06/09/2021, 6:12 PMupdateMany with both the id and userId conditions, and check the result to see if any rows were updated.Brandon Leichty
06/10/2021, 2:28 AM