Hello.
I have a following scenario 1: an array of structs (Post) that within themselves contain another array with struct (Like).
[
Post%{
id: 1,
likes: [
Like%{id: 1, post_id: 1, type: "upvote"},
Like%{id: 2, post_id: 1, type: "downvote"},
]
},
Post%{
id: 2,
likes: [
Like%{id: 3, post_id: 2, type: "downvote"},
]
}
]
Now, I get the request to update the like on the post, like this:
Like%{id: 3, post_id: 2, type: "upvote"},
This means that I need to find the post that has id of 2, then in the array find the struct that has id of 3 and post_id of 2, and change the type to “upvote”, such that produces the following result:
[
Post%{
id: 1,
likes: [
Like%{id: 1, post_id: 1, type: "upvote"},
Like%{id: 2, post_id: 1, type: "downvote"},
]
},
Post%{
id: 2,
likes: [
Like%{id: 3, post_id: 2, type: "upvote"}, <- this changed
]
}
]
Scenario 2:
This scenario is basically the same as the above with one additional request. Lets get another Like struct like this:
Like%{id: 4, post_id: 2, type: "upvote"},
Its the same routine: find the post with the id of 2, but because we dont have a like struct with id of 4, we need to insert the whole Like struct in the array, like this:
[
Post%{
id: 1,
likes: [
Like%{id: 1, post_id: 1, type: "upvote"},
Like%{id: 2, post_id: 1, type: "downvote"},
]
},
Post%{
id: 2,
likes: [
Like%{id: 3, post_id: 2, type: "upvote"},
Like%{id: 4, post_id: 2, type: "upvote"} <- This is new
]
}
]
Can someone help me with this?