add and delete likes backend

This commit is contained in:
Sendou
2020-03-13 16:44:58 +02:00
parent 2fd9c81431
commit 4d0f669e16
3 changed files with 58 additions and 1 deletions

View File

@@ -104,7 +104,6 @@ const FreeAgentsPage: React.FC<RouteComponentProps> = () => {
const showModalButton = () => {
if (!userData.user) return false
console.log("ownFAPost", ownFAPost)
if (ownFAPost && ownFAPost.hidden) {
const weekFromCreatingFAPost = parseInt(ownFAPost.createdAt) + 604800000

View File

@@ -4,6 +4,7 @@ const {
gql,
} = require("apollo-server-express")
const FAPost = require("../mongoose-models/fapost")
const FALike = require("../mongoose-models/falike")
const sendFAPostToDiscord = require("../utils/webhook")
const canVCValues = ["YES", "USUALLY", "SOMETIMES", "NO"]
@@ -32,6 +33,8 @@ const typeDef = gql`
past_experience: String
description: String
): Boolean!
addLike(discord_id: String!): Boolean!
deleteLike(discord_id: String!): Boolean!
}
enum CanVC {
@@ -196,6 +199,46 @@ const resolvers = {
})
})
await FALike.deleteMany({
$or: [
{ liker_discord_id: ctx.user.discord_id },
{ liked_discord_id: ctx.user.discord_id },
],
})
return true
},
addLike: async (root, args, ctx) => {
if (!ctx.user) throw new AuthenticationError("Not logged in.")
const post = await FAPost.findOne({ discord_id: ctx.user.discord_id })
if (!post) {
throw new UserInputError("Not a free agent")
}
const likedPost = await FAPost.findOne({ discord_id: args.discord_id })
if (!likedPost) {
throw new UserInputError("Liked user not a free agent")
}
await FALike.findOneAndUpdate(
{ liker_discord_id: ctx.user.discord_id },
{
liker_discord_id: ctx.user.discord_id,
liked_discord_id: args.discord_id,
},
{ upsert: true }
)
return true
},
deleteLike: async (root, args, ctx) => {
if (!ctx.user) throw new AuthenticationError("Not logged in.")
await FALike.deleteOne({ liked_discord_id: args.discord_id })
return true
},
},

15
mongoose-models/falike.js Normal file
View File

@@ -0,0 +1,15 @@
const mongoose = require("mongoose")
const faLikeSchema = new mongoose.Schema({
liker_discord_id: String,
liked_discord_id: String,
})
/*mapBallotSchema.virtual("liker_discord_id", {
ref: "User",
localField: "discord_id",
foreignField: "discord_id",
justOne: true,
})*/
module.exports = mongoose.model("FALike", faLikeSchema)