Skip to content

Commit

Permalink
remvoe Ctx and fix CreateApps migration #95
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 30, 2020
1 parent 5389ea4 commit f90ef84
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
28 changes: 24 additions & 4 deletions lib/auth/ctx.ex → lib/auth/app.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
defmodule Auth.Ctx do
defmodule Auth.App do
@moduledoc """
The Ctx context.
Schema and helper functions for creating/managing Apps.
"""

use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, warn: false
alias Auth.Repo
# https://stackoverflow.com/a/47501059/1148249
alias __MODULE__

schema "apps" do
field :description, :binary
field :end, :naive_datetime
field :name, :binary
field :url, :binary
field :person_id, :id
field :status, :id
field :apikey_id, :id

timestamps()
end

alias Auth.Ctx.App
@doc false
def changeset(app, attrs) do
app
|> cast(attrs, [:name, :description, :url, :end])
|> validate_required([:name, :url])
end

@doc """
Returns the list of apps.
Expand Down
23 changes: 0 additions & 23 deletions lib/auth/ctx/app.ex

This file was deleted.

24 changes: 11 additions & 13 deletions lib/auth_web/controllers/app_controller.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
defmodule AuthWeb.AppController do
use AuthWeb, :controller

alias Auth.Ctx
alias Auth.Ctx.App
alias Auth.App

def index(conn, _params) do
apps = Ctx.list_apps()
apps = App.list_apps()
render(conn, "index.html", apps: apps)
end

def new(conn, _params) do
changeset = Ctx.change_app(%App{})
changeset = App.change_app(%App{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"app" => app_params}) do
case Ctx.create_app(app_params) do
case App.create_app(app_params) do
{:ok, app} ->
conn
|> put_flash(:info, "App created successfully.")
Expand All @@ -27,20 +25,20 @@ defmodule AuthWeb.AppController do
end

def show(conn, %{"id" => id}) do
app = Ctx.get_app!(id)
app = App.get_app!(id)
render(conn, "show.html", app: app)
end

def edit(conn, %{"id" => id}) do
app = Ctx.get_app!(id)
changeset = Ctx.change_app(app)
app = App.get_app!(id)
changeset = App.change_app(app)
render(conn, "edit.html", app: app, changeset: changeset)
end

def update(conn, %{"id" => id, "app" => app_params}) do
app = Ctx.get_app!(id)
app = App.get_app!(id)

case Ctx.update_app(app, app_params) do
case App.update_app(app, app_params) do
{:ok, app} ->
conn
|> put_flash(:info, "App updated successfully.")
Expand All @@ -52,8 +50,8 @@ defmodule AuthWeb.AppController do
end

def delete(conn, %{"id" => id}) do
app = Ctx.get_app!(id)
{:ok, _app} = Ctx.delete_app(app)
app = App.get_app!(id)
{:ok, _app} = App.delete_app(app)

conn
|> put_flash(:info, "App deleted successfully.")
Expand Down
1 change: 1 addition & 0 deletions lib/auth_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule AuthWeb.Router do
resources "/roles", RoleController

resources "/permissions", PermissionController
resources "/apps", AppController
resources "/settings/apikeys", ApikeyController
end

Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20200830061923_create_apps.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Auth.Repo.Migrations.CreateApps do
add :end, :naive_datetime
add :person_id, references(:people, on_delete: :nothing)
add :status, references(:status, on_delete: :nothing)
add :apikey_id, references(:api, on_delete: :nothing)
add :apikey_id, references(:apikeys, on_delete: :nothing)

timestamps()
end
Expand Down
5 changes: 3 additions & 2 deletions test/auth_web/controllers/app_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
defmodule AuthWeb.AppControllerTest do
use AuthWeb.ConnCase

alias Auth.Ctx
alias Auth.App

@create_attrs %{description: "some description", end: ~N[2010-04-17 14:00:00], name: "some name", url: "some url"}
@update_attrs %{description: "some updated description", end: ~N[2011-05-18 15:01:01], name: "some updated name", url: "some updated url"}
@invalid_attrs %{description: nil, end: nil, name: nil, url: nil}

def fixture(:app) do
{:ok, app} = Ctx.create_app(@create_attrs)
{:ok, app} = App.create_app(@create_attrs)
app
end

describe "index" do
test "lists all apps", %{conn: conn} do
conn = admin_login(conn)
conn = get(conn, Routes.app_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Apps"
end
Expand Down

0 comments on commit f90ef84

Please sign in to comment.