how reuse a like model in nodejs

I have 2 models named "posts" and "status" and want to implement likes in them. first of all, I want the like to be able to record data like timestamps and other stuff depending on how it grows, which is why I made "like" be a model of its own.

The issue is since "posts" and "status" two models of their own are going to have "like" functionality.

Is there a way I could reuse the "like" model, instead of creating a separate "like" model for "posts" and "status", or how would you personally implement something like this?

Below is the post model

const postSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        trim: true
    },
    description: {
        type: String,
        required: true,
        trim: true
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    }
}, {
    timestamps: true
})

below is the status model

const statusSchema = new mongoose.Schema({
    Body: {
        type: String,
        required: true,
        trim: true
    },
    owner: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    tags: [{
        type: mongoose.Schema.Types.ObjectId,
        required: True,
        ref: 'User'
    }]
}, {
    timestamps: true
})

here is the like model which I would like users to be able to like both posts by users and statuses, while still able to retain information like the time it was liked and other information depending on the growth and need

const likeSchema = new mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    likedObject: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'Posts'
    }
}, {
    timestamps: true
})

Is there a way I could reuse the "like" model, instead of creating a separate "like" model for "posts" and "status" to capture the users and the time that they liked other user's statuses and posts?

1 answer

  • answered 2020-10-29 22:26 Ntshembo Hlongwane

    Making Like Model was over complicating the entire thing so you could have something like this in your postSchema.

    const postSchema = new mongoose.Schema({
        title: {
            type: String,
            required: true,
            trim: true
        },
        description: {
            type: String,
            required: true,
            trim: true
        },
        owner: {
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: 'User'
        },
        timeStamps:true,
        //Adding the like property in each post but setting it to a default of 0
        likes:{type:Number, default:0}
    
    
    })
    

    Now the key is identifying the post when like button is smashed

    So what you could do on the client side is making sure that when like button is clicked you have an ID of the post to the function that your calling and, then pass that ID back to the server and you can have a logic like this below on your server and you would be able to add like functionality..

    Server logic for adding like functionality

    router.post("/api/likes/:id", async (request, response) => {
      const post_id = request.params.id;
      const post = await postModel.findOne({ _id: post_id });
    
      post.likes += 1;
    
      const updateDocument = await postModel.findOneAndUpdate(
        { _id: post_id },
        post,
        {
          new: true,
        }
      );
    
      return response.status(201).json({ msg: "Liked post" });
    });
    

    So the idea is two always be updating that specific document