lib/athena_web/router.ex

defmodule AthenaWeb.Router do
  use AthenaWeb, :router

  import Phoenix.LiveDashboard.Router

  @subresource_actions [:index, :new, :create]

  pipeline :api do
    plug :accepts, ["json"]
  end

  pipeline :browser do
    plug :accepts, ["html"]

    plug :fetch_session

    plug Cldr.Plug.AcceptLanguage, cldr_backend: AthenaWeb.Cldr

    plug Cldr.Plug.PutLocale,
      apps: [:cldr, :gettext],
      from: [:query, :body, :cookie, :accept_language],
      param: "locale",
      default: "de",
      gettext: AthenaWeb.Gettext,
      cldr: AthenaWeb.Cldr

    plug Cldr.Plug.PutSession

    plug :fetch_flash
    plug :fetch_live_flash

    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :admin do
    plug :auth
    plug :put_layout, {AthenaWeb.Admin.LayoutView, :app}
    plug :put_root_layout, {AthenaWeb.Admin.LayoutView, :root}
  end

  pipeline :frontend do
    plug :put_layout, {AthenaWeb.Frontend.LayoutView, :app}
    plug :put_root_layout, {AthenaWeb.Frontend.LayoutView, :root}
  end

  scope "/admin", AthenaWeb.Admin, as: :admin, assigns: %{access: :admin} do
    pipe_through [:browser, :admin]

    resources "/events", EventController
    resources "/events/:event/locations", LocationController, only: @subresource_actions
    resources "/locations", LocationController, except: @subresource_actions
    resources "/events/:event/item_groups", ItemGroupController, only: @subresource_actions
    resources "/item_groups", ItemGroupController, except: @subresource_actions
    resources "/item_groups/:item_group/items", ItemController, only: @subresource_actions
    resources "/items", ItemController, except: @subresource_actions
    resources "/items/:item/movements", MovementController, only: @subresource_actions
    resources "/movements", MovementController, except: @subresource_actions

    live_dashboard "/dashboard",
      metrics: {Athena.Telemetry, :dashboard_metrics},
      ecto_repos: [Athena.Repo],
      allow_destructive_actions: true
  end

  scope "/logistics/", AthenaWeb.Frontend, as: :frontend_logistics, assigns: %{access: :logistics} do
    pipe_through [:browser, :frontend]

    live "/locations/:location", Location.InventoryLive, :show_logistics
    live "/locations/:location/stats", Location.StatsLive, :show

    live "/events/:event/overview", Dashboard.TableLive
    live "/events/:event/overview/item", Dashboard.ItemLive
    live "/events/:event/overview/location", Dashboard.LocationLive
    live "/items/:item/overview", Dashboard.ItemStatsLive

    live "/events/:event/movements/supply", MovementLive, :supply
    live "/events/:event/movements/relocate", MovementLive, :relocate
  end

  scope "/vendor/", AthenaWeb.Frontend, as: :frontend_vendor, assigns: %{access: :vendor} do
    pipe_through [:browser, :frontend]

    live "/locations/:location", Location.InventoryLive, :show_vendor
  end

  scope "/", AthenaWeb do
    get "/", Redirector, to: "/admin/events"
  end

  scope "/api" do
    pipe_through [:api]

    forward(
      "/",
      Absinthe.Plug.GraphiQL,
      schema: AthenaWeb.Schema,
      socket: AthenaWeb.UserSocket,
      interface: :playground
    )
  end

  defp auth(conn, _opts),
    do: Plug.BasicAuth.basic_auth(conn, Application.fetch_env!(:athena_logistics, Plug.BasicAuth))
end