Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir and Phoenix

Elixir and Phoenix

Building a basic CRUD application with Elixir and Phoenix.

Garrett Heinlen

October 07, 2014
Tweet

More Decks by Garrett Heinlen

Other Decks in Programming

Transcript

  1. Se#ng&up&the&Database file:!mix.exs def application do [ mod: { PhoenixCrud, []

    }, applications: [:phoenix, :cowboy, :postgrex, :ecto] ] end defp deps do [ {:phoenix, github: "phoenixframework/phoenix"}, {:cowboy, "~> 1.0.0"}, {:postgrex, ">= 0.0.0"}, {:ecto, "~> 0.2.0"} ] end
  2. Se#ng&up&the&Database file:!lib/phoenix_crud/repo.ex defmodule Repo do use Ecto.Repo, adapter: Ecto.Adapters.Postgres def

    conf do parse_url "ecto://postgres:postgres@localhost/phoenix_crud" end def priv do app_dir(:phoenix_crud, "priv/repo") end end
  3. Se#ng&up&the&Database file:!lib/phoenix_crud/ supervisor.ex defmodule PhoenixCrud.Supervisor do use Supervisor def start_link

    do :supervisor.start_link(__MODULE__, []) end def init([]) do # Adding repo to be sent into supervise tree = [worker(Repo, [])] supervise(tree, strategy: :one_for_one) end end
  4. Crea%ng(a(Migra%on file:!lib/phoenix_crud/repo/ migrations/ 20140815053139_create_users.exs defmodule PhoenixCrud.Repo.Migrations.CreateUsers do use Ecto.Migration def

    up do "CREATE TABLE users(id serial primary key, content varchar(140))" end def down do "DROP TABLE users" end end
  5. Adding&a&simple&Welcome&Page file:!web/router.ex defmodule PhoenixCrud.Router do use Phoenix.Router plug Plug.Static, at:

    "/static", from: :phoenix_crud scope alias: PhoenixCrud do get "/", WelcomeController, :index, as: :root end end
  6. Adding&a&simple&Welcome&Page file:!web/templates/index.eex <div class="jumbotron"> <h2>Simple CRUD app using Phoenix and

    Elixir!</h2> <p class="lead">First try at getting a basic crud app up and working with Phoenix.</p> </div>
  7. Add#the#User#Resource file:!web/router.ex defmodule PhoenixCrud.Router do use Phoenix.Router plug Plug.Static, at:

    "/static", from: :phoenix_crud scope alias: PhoenixCrud do get "/", WelcomeController, :index, as: :root resources "users", UserController end end
  8. Stru%ng(our(stuff user = %User{content: "Hello"} user.content #=> "Hello" Repo.insert(user) #

    Saves to the database user = Repo.get(User, 1) #=> %User{content: "Hello} user = %{user | content: "Goodbye"} Repo.update(user) #=> %User{content: "Goodbye"} Repo.delete(user) # Deleted the database record
  9. User%Controller file:!web/controllers/ user_controller.ex defmodule PhoenixCrud.UserController do use Phoenix.Controller use Jazz

    alias PhoenixCrud.Router alias PhoenixCrud.User def index(conn, _params) do render conn, "index", users: Repo.all(User) end end
  10. User%Controller file:!web/controllers/ user_controller.ex def show(conn, %{"id" => id}) do case

    Repo.get(User, id) do user when is_map(user) -> render conn, "show", user: user _ -> redirect conn, Router.page_path(page: "unauthorized") end end
  11. User%Controller file:!web/controllers/ user_controller.ex def create(conn, %{"user" => %{"content" => content}})

    do user = %User{content: content} case User.validate(user) do [] -> user = Repo.insert(user) render conn, "show", user: user errors -> render conn, "new", user: user, errors: errors end end
  12. User%Controller file:!web/controllers/ user_controller.ex def edit(conn, %{"id" => id}) do case

    Repo.get(User, id) do user when is_map(user) -> render conn, "edit", user: user _ -> redirect conn, Router.page_path(page: "unauthorized") end end
  13. User%Controller file:!web/controllers/ user_controller.ex def update(conn, %{"id" => id, "user" =>

    params}) do user = Repo.get(User, id) user = %{user | content: params["content"]} case User.validate(user) do [] -> Repo.update(user) # don't do this json conn, 201, JSON.encode!(%{location: Router.user_path(id: user.id)}) errors -> json conn, errors: errors end end
  14. User%Controller file:!web/controllers/ user_controller.ex def destroy(conn, %{"id" => id}) do user

    = Repo.get(User, id) case user do user when is_map(user) -> Repo.delete(user) json conn, 200, JSON.encode!(%{location: Router.users_path}) _ -> redirect conn, Router.page_path(page: "unauthorized") end end