gbrlcustodio

gbrlcustodio

Hey everyone! It’s been a while since I last built a Phoenix app, and I noticed that Phoenix 1.7 no longer supports Phoenix views. I know I can always add Phoenix views as a dependency, but I’m trying to do things the new way. Unfortunately, I’m having a hard time finding resources for building JSON-APIs that work with newer Phoenix versions. The JSON and APIs documentation only covers simple scenarios.

So, I’m curious to know how you’re organizing your JSON views when it comes to reusing them for rendering associations like when we used to have render_one and render_many helper functions to cross-reference them. For example, let’s say we have a post with many comments. How are you reusing the rendering logic for both the /posts and /comments resources, where the post also includes its comments but rendering them referencing the CommentJSON view or something?

Are you doing something like this or extracting code that translates Ecto schemas into Elixir maps and reusing it?

defmodule HelloWeb.PostJSON do
  @doc """
  Renders a single post.
  """
  def show(%{post: post}) do
    %{data: data(post)}
  end

  defp data(%Post{} = post) do
    %{
      id: post.id,
      title: post.title,
      comments: HelloWeb.CommentJSON.index(%{comments: post.comments})[:data]
    }
  end
end

defmodule HelloWeb.CommentJSON do
  def index(%{comments: comments}) do
    %{data: for(comment <- comments, do: comment_json(comment))}
  end

  defp comment_json(%Comment{} = comment) do
    %{
      id: comment.id,
      text: comment.type
    }
  end
end

Showing Posts 1 to 2

krasenyp

krasenyp

Just use functional programming, like in your example. In my opinion, Ecto schemas have no business in the controller.

gbrlcustodio

gbrlcustodio OP

Hi @krasenyp, thanks for your reply!

Regarding reusing serialization logic from the CommentJSON module in the PostJSON, are you referring to the module directly? Doing comments: HelloWeb.CommentJSON.index(%{comments: post.comments})[:data] felt a bit strange because almost sounding like I should have it extracted elsewhere and reused across JSON modules like this:

defmodule HelloWeb.PostJSON do
  def show(%{post: post}) do
    %{data: HelloWeb.Serializers.Post.json(post)}
  end
end

defmodule HelloWeb.CommentJSON do
  def index(%{comments: comments}) do
    %{data: for(comment <- comments, do: HelloWeb.Serializers.Comment.json(post)(comment))}
  end
end

Maybe I’m overcomplicating things. CommentJSON.comment_json/1 and PostJSON.data/1 (just realized it’s not called post_json) could be exposed as public functions that associated modules could call, like HelloWeb.CommentJSON.comment_json(comment).

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews