POST Details for Strapi Relations - strapi

I've got a simple data model, which has a tag and that is linked to an upload:
Tag Table Upload Table
id id
name tag_id
title
Now, trying to save an item to the upload, I expected the JSON to be:
{
"title": "Testing",
"tag_id": {"id": 1}
}
But this never works or links. What am I doing wrong?

Related

Cannot update Organization field of a Person while doing a PUT request

So I created a person and also created an Organization and i want to send a PUT request to update a few fields of that Person (+ Organization they belong to).
So if my payload looks like:
{
"name": "Lorem Ipsum"
}
The persons name will be updated, all’s well.
But if I add the field ID for Organizations (org_id) as per the api docs on PUT requests, either nothing happens or i get an error.
If i add to the payload org_id as so, the name will be updated but not the organization.
{
"name": "Lorem Ipsum",
"org_id": 1, // <integer> of the Organizations ID
}
And if i format my payload just to include the org_id as so:
{
"org_id": 1
}
I get the following error:
“{"success":false,"error":"Bad request","error_info":"Please check developers.pipedrive.com for more information about Pipedrive API.","data":null,"additional_data":null}”
I’m stuck.
Can anyone help?

Send array to api via postman?

Beginner in API
I have table post_tag which is posts table has relation ( many to many ) with tags table.
my controller code ( every thing is good ):
$post = Post::create($data);
$post->tag()->attach($request->tags);
return $this->apiRespone($post , 'Added Post Successfuly', null ,200);
My question is here: now I send array of tags like that! Is that the best way or the correct way to send array ( means when I give this api url to mobile developer, he will know what to do with this api url ?
My way is correct or not ?
Although your solution works, a better approach since you are building an endpoint, it would be better to switch your input to accept JSON format rather than using form-data. Make your API endpoint to accept the following payload:
{
"title": "First Post",
"desc": "Desc of Post",
"image": "image3.jpg",
"category_id": 1,
"tags": [
"one",
"two",
"three"
]
}
In Laravel, you get just grab the tags (or any other properties) with the following:
$tags = $request->input('tags');
For the image, you can allow it to be received in base64 encoded image. The image will look like a bunch of string which should be converted by the client (ios or android) e.g:
{
image:"/9j/4AAQSkZJRgABAQAAAQABAAD/7QB8UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAF8cAigAWkZCTUQyMzAwMDk2OTAxMDAwMDgxNTIwMDAwNWY2MDAwMDA3NDZmMDAwMDE2YmEwMDAwNDAxYjAxMDBmMTM0MDEwMGY4YzIwMTAwZDkxNDAyMDA1ZDRhMDIwMAD/2wBDAAcHBwcHBwwHBwwRDAwMERcRERERFx4XFxcXFx4kHh4eHh4eJCQkJCQkJCQrKysrKysyMjIyMjg4O"
}
Then in PHP, if you want to save the image on disk, just use base64_decode. See example here.

Microsoft 365 API : Issue at attaching Contact to Campaign Response

I am trying to attach a contact to campaign response.
I am using rest API for that.
https://learn.microsoft.com/en-us/dynamics365/customer-engagement/web-api/campaignresponse?view=dynamics-ce-odata-9
Post Data :
{
"firstname": "TestFirst",
"lastname": "TestLast",
"emailaddress": "test#test.com",
"telephone": "1234567890",
"prioritycode": 0,
"responsecode": 1,
"subject": "Test Subject",
"statuscode": 1,
"regardingobjectid_campaign#odata.bind": "/campaigns(xxxx90c-11ef-e811-a966-000d3ad24a0d)",
"regardingobjectid_contact#odata.bind": "/contacts(xxxxfa2e-c3b5-e811-a962-000d3ad24a0d)"
}
Here is my JSON.
I am getting Error : “Campaign as RegardingObject must be supplied”. Without contact, it works fine.
I had the same problem and the documentation is not very clear about it, I had to check all the relationships of the CampaignResponse in order to understand how to solve this.
If you want to create a CampaignResponse linked to both a Campaign and a Contact you need to do the following:
Create a CampaignResponse with the "regardingobjectid_campaign#odata.bind" in the params sent.
POST "https://some_subdomain.crm6.dynamics.com/api/data/v9.0/campaignresponses"
{
"regardingobjectid_campaign#odata.bind": "/campaigns(CAMPAIGN_ID_HERE)",
"description": "some desc",
"subject": "some subject "
}
Then find the CampaignResponse you just created to get its activityid (every CampaignResponse is an Activity)
Finally, you need to create a new ActivityParty, that will link the Contact to the CampaignResponse.
POST "https://some_subdomain.crm6.dynamics.com/api/data/v9.0/campaignresponses(CAMPAIGN_ID_HERE)/activitypointer_activity_parties"
{
"partyid_contact#odata.bind": "/contacts(CONTACT_ID_HERE)",
"participationtypemask": 11 //this is the code for customers
}
The "Regarding" lookup field can only be set to a single "regarding" record. Even though it appears that there are different Regarding fields, one for each entity type, those are "helper" fields that let you easily set the main Regarding field by setting one of those regardingobjectid_xxx fields.
You must choose to use either a campaign or a contact as your Regarding field. You can of course create other lookups, so you could use the Regarding field for your campaign and then add an additional Contact lookup field, for example.

Changing data in every document

I am working on an application that has messages and I want to store all the messages. But my problem is the message has a from first name and last name which could change. So if for example my JSON was
{
"subject": "Hello!",
"message": "Hello there",
"from": {
"user_id": 1,
"firstname": "George",
"lastname": "Lastgeorge"
}
}
The user could potentially change their last name or even first name. Which would require basically looping over every record in elasticsearch and updating everyone with the user_id.
Is there a better way to go about doing this?
I feel you should use parent mapping.
Keep the user info as parent with userID as key.
/index/userinfo/userID
{
"name" : "George",
"last" : "Lastgeorge"
}
Next , you need to maintain each chat as a child document and map the parent to the userindo type.
This way , whenever you want to make some change to the user information , simply make the change in userInfo type.
With this feature intact , you can search your logs based on user information , or search users based on chat records.
Link - http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/parent-child.html

Removing specific items from array with MongoDB

I am developing a web app using Codeigniter and MongoDB.
The users can upload files and other users can comment on them.
I store the comments in an array called comments in the main file document.
This is all fine but how can I remove specific comments from the array?
I cannot use ID as key since a user can add multiple comments. How would
you recommend that I can do it?
This is my comment array:
"comments": [
{
"user_id": ObjectId("4f240b433dc7937d68030000"),
"user_name": "james",
"user_comment": "This is a comment",
"created_at": "2012-01-2821: 20: 44"
},
{
"user_id": ObjectId("4f240b433dc7937d68030000"),
"user_name": "mandy",
"user_comment": "This is another comment",
"created_at": "2012-01-2821: 31: 07"
}
],
If you can identify the comment item by matching userid, name or comment -- then you can remove that comment using update() command with $pull modifier along with the appropriate condition.
If you cannot do as above, include an unique id in the comments (like UUID).
To delete the comment, do the following:
db.coll.update({<cond to identify document}, {$pull: {'comments': {'name': <name>}}} )
If you use the id, which is preferred:
db.coll.update({<cond to identify document}, {$pull: {'comments': {'id': <id>}}} )
maybe you can remove comment by its 'created_at' time, as the time is unique.
$db.coll.update({cond to identify document},{$pull:{'comments':{'created_at':<>}}})
I think, you have add mongodb id to each comment when insert it. You can create a new mongodb id like this;
$comment_id = new MongoID();
Now, you can delete comments by id with $pull + $update like #smoorthy's answer.

Resources