Created
October 22, 2016 18:42
-
-
Save bnhansn/c341677ebad04b8a33bdf242470fdd50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Sling.MessageController do | |
| use Sling.Web, :controller | |
| plug Guardian.Plug.EnsureAuthenticated, handler: Sling.SessionController | |
| def index(conn, params) do | |
| last_seen_id = params["last_seen_id"] || 0 | |
| room = Repo.get!(Sling.Room, params["room_id"]) | |
| page = | |
| Sling.Message | |
| |> where([m], m.room_id == ^room.id) | |
| |> where([m], m.id < ^last_seen_id) | |
| |> order_by([desc: :inserted_at, desc: :id]) | |
| |> preload(:user) | |
| |> Sling.Repo.paginate() | |
| render(conn, "index.json", %{messages: page.entries, pagination: Sling.PaginationHelpers.pagination(page)}) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment