Skip to content
Permalink
Browse files

The User List

  • Loading branch information...
dnsbty committed Aug 18, 2019
1 parent fef2f39 commit 12fc14159e3fa546444e4575ba16fd743b945150
@@ -1,4 +1,5 @@
defmodule UserAdmin.Users do
import Ecto.Query
alias UserAdmin.Repo
alias UserAdmin.Users.{Role, User}

@@ -27,4 +28,15 @@ defmodule UserAdmin.Users do
def list_roles do
Repo.all(Role)
end

@doc """
List all users in the database.
"""
@spec list_users :: list(User.t())
def list_users do
User
|> join(:inner, [u], r in assoc(u, :role))
|> preload([_, r], role: r)
|> Repo.all()
end
end
@@ -0,0 +1,15 @@
defmodule UserAdminWeb.UserLive.Index do
use Phoenix.LiveView
alias UserAdmin.Users
alias UserAdminWeb.UserView

def mount(_session, socket) do
users = Users.list_users()

{:ok, assign(socket, :users, users)}
end

def render(assigns) do
UserView.render("index.html", assigns)
end
end
@@ -17,6 +17,7 @@ defmodule UserAdminWeb.Router do
scope "/", UserAdminWeb do
pipe_through :browser

live "/users", UserLive.Index
live "/users/new", UserLive.New
end

@@ -0,0 +1,15 @@
<h2>Users</h2>
<table>
<thead>
<th>Name</th>
<th>Role</th>
</thead>
<tbody>
<%= for user <- @users do %>
<tr>
<td><%= user.name %></td>
<td><%= user.role.name %></td>
</tr>
<% end %>
</tbody>
</table>

0 comments on commit 12fc141

Please sign in to comment.
You can’t perform that action at this time.