Strapi: How to add/remove entries to a relational field via API

Viewed 641

I'm using Strapi CMS for an event posting app, with user registration and event sign-up...

Given Strapi models Event (new) and User (Strapi existing), Event has a relational field Participants connected to Users.

WORKS: In the Strapi admin dashboard I can successfully add users to events. In my client front-end, I can get event details and participants via Strapi API.

QUESTION: What is the cleanest way to add/remove event participants via API ?

I have routes and controllers setup (within the Event model definition), and I'm successfully making dummy POST (or PUT) requests with a return:

events/1/rsvp_add
events/1/rsvp_remove

Route definition:

    {
        "method": "PUT",
        "path": "/events/:id/rsvp_add",
        "handler": "event.rsvp_add",
        "config": {
          "policies": []
        }
    }

Controller:

  rsvp_add: async (ctx) => {
      console.log("Attempting add rsvp")
      SendResponse(ctx, "Got rsvp attempt")
  }

Is there a one-line Strapi statement I can use to add/remove users to the relational participants field in the event? Whatever the case, I want to avoid having to re-submit the entire event and/or list of participants. I'm just looking for "add/remove user X in event participants".

Strapi has auto-generated quite a bit API-wise, and I'm hoping this holds true for relational fields. However there is a Github thread that seems to touch on this, and someone termed this "nested put" and mentioned it's not supported by Strapi (yet). I wonder if this is the same thing and/or has Strapi addressed this since.

I can manually build queries (have done so for some geolocation stuff) but I want to avoid that for simple relation updates, which I'm going to have a lot of for various models.

0 Answers
Related