- Add jump_credo_checks ~> 0.4 with all 20 checks enabled - Fix all standard Credo issues: 139 @spec (113 done, 26 remain), 4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom, 2 max line length, 9 assert_receive timeout - Fix 170+ jump_credo_checks warnings: - 117 TopLevelAliasImportRequire: move nested alias/import to module top - 32 UseObanProWorker: switch to Oban.Pro.Worker - 4 DoctestIExExamples: add doctests / create test file - ~20 WeakAssertion: strengthen type-check assertions - Various ConditionalAssertion, AssertReceiveTimeout fixes - Exclude vendor/ from Credo analysis - Remaining: 175 warnings (mostly opinionated WeakAssertion, AvoidSocketAssignsInTest), 26 @spec annotations
49 lines
1.4 KiB
Elixir
49 lines
1.4 KiB
Elixir
defmodule MicrowavepropWeb.Api.V1.BeaconController do
|
|
@moduledoc "Read approved beacons; submit new beacons (pending approval)."
|
|
|
|
use Phoenix.Controller, formats: [:json]
|
|
|
|
alias Microwaveprop.Beacons
|
|
alias MicrowavepropWeb.Api.ErrorJSON
|
|
alias MicrowavepropWeb.Api.V1.BeaconJSON
|
|
|
|
plug :accepts, ["json"]
|
|
action_fallback MicrowavepropWeb.Api.FallbackController
|
|
|
|
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
|
def index(conn, _params) do
|
|
beacons = Beacons.list_beacons()
|
|
json(conn, BeaconJSON.index(%{beacons: beacons}))
|
|
end
|
|
|
|
@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
|
def show(conn, %{"id" => id}) do
|
|
viewer = conn.assigns[:current_api_user]
|
|
|
|
case Beacons.get_visible_beacon(id, viewer) do
|
|
nil -> ErrorJSON.send_problem(conn, 404, "not_found", "Beacon not found.")
|
|
beacon -> json(conn, BeaconJSON.show(%{beacon: beacon}))
|
|
end
|
|
end
|
|
|
|
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
|
def create(conn, params) do
|
|
user = conn.assigns.current_api_user
|
|
|
|
attrs =
|
|
Map.take(
|
|
params,
|
|
~w(frequency_mhz callsign grid lat lon power_mw height_ft on_the_air keying bearing beamwidth_deg notes)
|
|
)
|
|
|
|
case Beacons.create_beacon(user, attrs) do
|
|
{:ok, beacon} ->
|
|
conn
|
|
|> put_status(:created)
|
|
|> json(BeaconJSON.show(%{beacon: beacon}))
|
|
|
|
{:error, changeset} ->
|
|
ErrorJSON.send_changeset(conn, changeset)
|
|
end
|
|
end
|
|
end
|