doormitor

doormitor

Find and upsert struct in an array of structs

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?

Marked As Solved

joey_the_snake

joey_the_snake

I had some sloppy mistakes in there. This is a cleaned up version that should compile:

%{id: new_like_id, post_id: new_like_post_id} = new_like

posts
|> Enum.map(fn 
    %{id: ^new_like_post_id} = post ->
      {likes, found} =
        Enum.map_reduce(post.likes, false, fn 
          %{id: ^new_like_id} = like, _found -> {%{like | type: new_like.type}, true}
          like, found -> {like, found}
       end)
      
      likes = if found, do: likes, else: [new_like | likes]
      %{post | likes: likes}
    
    post ->
      post
end)

The mistakes:

  1. get rid of the first album ->
  2. You can’t pin the ids the way I did so you can assign them to variables using pattern matching

Also Liked

doormitor

doormitor

Yes that worked! Now I will put effort to study those lines. Thank you!

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement