Implementing message functions in our context

We'll start with the easiest functionality to implement and then we'll spend a little bit of time writing some tests for our new functions as well. One thing we'll also need to do is clean up some of the broken tests that are the result of the work from the last chapter; we'll tackle that as well. First, in our Votes context (lib/vocial/votes/votes.ex), we'll need to add an alias statement for our new message schema:

  alias Vocial.Votes.Message

Next, we'll continue by implementing the simplest bit of functionality: the list_messages/0 function. This will give us a list of messages not associated with a Poll. We'll add this to our votes context:

  def list_lobby_messages do
Repo.all(
from m in Message,
where: is_nil(m.poll_id),
order_by: [desc: :inserted_at],
limit: 100
)
end

Instead of just using list_messages/0 as our function name, we should make it clear that we're not attempting to get every message that exists, just the ones that do not have a poll associated with them! By naming this list_lobby_messages/0, we're making it very clear that this function's purpose is 100% to get the messages not associated with a poll! In keeping consistent with the name of the function and what the name implies, we also need to specifically tell Ecto that we're looking for messages with a null (nil in the case of Elixir) poll ID value! We don't need any additional preloads or anything as part of this function, but we do need to make sure that we're ordering our messages by when they were inserted into the database and we also want to limit how many messages we're pulling at a time. Right now, we've arbitrarily set that limit to 100, but it could be any amount depending on what you want or how you can scale your application!

Now, we'll write up the function that will handle giving us a list of messages that are associated with a poll! We'll call this function list_poll_messages/1, again keeping the intent of the function very clear for anyone else that might be working on this application with us. Let's take a look at what this function looks like:

  def list_poll_messages(poll_id) do
Repo.all(
from m in Message,
where: m.poll_id == ^poll_id,
order_by: [desc: :inserted_at],
limit: 100,
preload: [:poll]
)
end

This isn't too dramatically different from the lobby messages function, but one thing we are including here is a preload statement to make sure the poll is preloaded into our list of poll messages.

Finally, we'll need to implement a function to create new messages, but this is just the same sort of code we've implemented everywhere else, nothing too fancy:

  def create_message(attrs) do
%Message{}
|> Message.changeset(attrs)
|> Repo.insert()
end

Now that we have the code in place, the next step is for us to write some tests to make sure our code is actually doing what we expect it to!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset