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

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.

Related

Apollo GraphQL "writeQuery" causing re-render to the app which is leading to an unnecessary network request

I'm working on a chat app, developed using Apollo GraphQL and React and I have the following issue:
I have a component called "Conversation" which is fetching an array of messages between two users (the logged-in user and other user) using the "useQuery" hook of Apollo which is invoked every time the component mounts.
When a new message is sent by the logged-in user, I'm updating the cached array of messages using the "writeQuery" function provided by Apollo.
By doing that, the "Conversation" component is getting re-rendered, which is leading to an unnecessary network request because the "useQuery" hook is triggered again.
I would love to hear if there is a way of preventing "writeQuery" from triggering a re-render to the relevant component.
Thanks a lot in advance!

Apollo GraphQL Client - Modifying The Data From JS (React)

I'm developing a real time chat app using graphql and I want to modify fetched data instead of refetch it again - in order to reduce unnecessary network requests.
When my app first init I'm fetching a list of users (that are not the current logged-in user) to display on the sidebar of the app.
Each user contains a lastMessage object that represent the last message that he sent or received.
When a user clicks on a user to chat with, another query is submitted in order to get all the messages between the users.
Using subscriptions, I'm updating the messages state when a new message is sent (either by the logged-in user or by the other user).
Now, I want to update the lastMessage object of the related user displayed on the sidebar, but I don't want to refetch all the users again in order to do so, because I have all the updated data in the client, which I received by the subscription event. Instead, I want to update the graphql queried data using js.
Is there a way to do that?
Thanks.
Apollo cache has a 'modify' method that you can use to set the cache of user's lastMessage.
Apollo docs for cache modify

How to minimize axios posts with controller actions

I am creating an application with Laravel and VueJs. In this application a user can create an event. These events have participants. In my application the user fills out all of the event information and participants information and then clicks submit.
What I am looking for guidance on is how to best structure this in the backend. What I need to accomplish is the following:
create an event to the database
create participants
do an action based on the participants in the event (would happen in the event controller)
right now the way I have structured this is:
Send an axios post to create the event (EventController)
then send an axios post to create participants (ParticipantController)
then send another axios post to the event controller to do the last action (EventController)
this to me feels like way too many axios post calls, but I don't want to just put everything in one controller. Is there a better way to do what I am wanting to do while minimizing the number of axios calls?
if you need to create participants and perform particular action after event create then you can use laravel observers you can use it's created function to perform any action like after createing an event you have to make some enteries on participant table so you can perform those actions in that observer function.

How to add a dynamic results section to Laravel

I made a laravel app which uses an index method that delivers a collection of the client's available appointments (think hairdresser) to the page (as an api), which I then display using vue/vuetify.
The client is saying they would like the appointments on the page to be dynamic/live eg if someone books an appointment, then all other logged in users will see that appointment disappear from the list on their screen.
I have no idea how I would do this, although I have had one idea - I somehow incorporate node/non-blocking on the server, like a chat room, but only for this part of the app.
Or is there a way to do this with laravel/nginx?
Thanks in advance - I don't know what to search for!
I believe you are looking for Broadcasting (Documentation Link). You would need to:
Configure your broadcasting driver (you could give pusher a try for quick setup and tinkering)
Configure your Laravel backend to dispatch a new event whenever a new appointment is made, (e.g. event(new AppointmentCreated($appointment)) where AppointmentCreated implements the ShouldBroadcast interface. You can combine this with Model Events
Update your frontend to receive your broadcast (Check Laravel Echo). Once you receive a broadcast, update the UI to mark this appointment as unavailable i.e, make it disappear

Strapi: how to send confirmation email when user signup?

I want to send emails to signup user and activate it until cerntain actions are done.
I don't know whether this feature is available already, or I need to implement the logic myself. With the default authentication and user models, it looks like quite complicated to modify the logics. how difficult is it to implement such features?
As you said, there is already a default logic for the Users in Strapi. However, the files can be edited and you can custom the behavior.
In your case, you need to go to ./api/user/controllers/User.js file in the create method and add your custom logic for sending an email where the User has been created (see https://github.com/strapi/strapi-generate-users/blob/master/files/api/user/controllers/User.js#L52).
I hope this answer will help you!
PS: I'm one of the authors of Strapi.
You can add a user model file in extensions/user-permissions/models/user.js with an afterCreate hook:
lifecycles: {
async afterCreate(data) {
// SEND EMAIL HERE
},
}
It seems there is an option for this feature.

Resources