-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ash Radio API, Feed with admin panel (#150)
* Rename Radio context to RadioLegacy * Create Radio Api with Ash, move old Radio context to RadioLegacy. Use new queries for gql Feed resolvers * Add calculation for long, lat * Update admin display
- Loading branch information
Showing
21 changed files
with
389 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Orcasite.Radio do | ||
use Ash.Api, extensions: [AshAdmin.Api] | ||
|
||
resources do | ||
registry Orcasite.Radio.Registry | ||
end | ||
|
||
admin do | ||
show? true | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
server/lib/orcasite/radio/calculations/longitude_latitude.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Orcasite.Radio.Calculations.LongitudeLatitude do | ||
use Ash.Calculation | ||
|
||
@impl true | ||
def load(_query, _opts, _context) do | ||
[:location_point] | ||
end | ||
|
||
@impl true | ||
def calculate(records, _opts, _arguments) do | ||
Enum.map(records, fn %{location_point: %{coordinates: {lng, lat}}} -> | ||
"#{lng},#{lat}" | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,113 @@ | ||
defmodule Orcasite.Radio.Feed do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
use Ash.Resource, | ||
extensions: [AshAdmin.Resource], | ||
data_layer: AshPostgres.DataLayer | ||
|
||
alias __MODULE__ | ||
attributes do | ||
integer_primary_key :id | ||
|
||
schema "feeds" do | ||
field(:name, :string) | ||
field(:slug, :string) | ||
field(:node_name, :string) | ||
field(:location_point, Geo.PostGIS.Geometry) | ||
attribute :name, :string | ||
attribute :node_name, :string | ||
attribute :slug, :string | ||
attribute :location_point, :geometry | ||
|
||
timestamps() | ||
create_timestamp :inserted_at | ||
update_timestamp :updated_at | ||
end | ||
|
||
@doc false | ||
def changeset(feed, attrs) do | ||
feed | ||
|> cast(attrs, [:name, :node_name, :slug, :location_point]) | ||
|> validate_required([:name, :node_name, :slug]) | ||
postgres do | ||
table "feeds" | ||
repo Orcasite.Repo | ||
end | ||
|
||
def latlong_to_geo(lat, long) when is_float(lat) and is_float(long), | ||
do: Geo.WKT.decode!("SRID=4326;POINT(#{lat} #{long})") | ||
identities do | ||
identity :unique_slug, [:slug] | ||
end | ||
|
||
actions do | ||
defaults [:destroy] | ||
|
||
read :read do | ||
primary? true | ||
|
||
prepare fn query, _context -> | ||
query | ||
|> Ash.Query.load(:longitude_latitude) | ||
end | ||
end | ||
|
||
read :get_by_slug do | ||
get_by :slug | ||
end | ||
|
||
create :create do | ||
primary? true | ||
reject [:location_point] | ||
|
||
argument :longitude_latitude, :string do | ||
description "A comma-separated string of longitude and latitude" | ||
end | ||
|
||
change &change_longitude_latitude/2 | ||
end | ||
|
||
update :update do | ||
primary? true | ||
reject [:location_point] | ||
|
||
argument :longitude_latitude, :string do | ||
description "A comma-separated string of longitude and latitude" | ||
end | ||
|
||
# TODO: Find the actual json -> schema function | ||
def from_json(attrs) do | ||
%Feed{} | ||
|> cast( | ||
decode_location_point(attrs), | ||
Map.keys(Orcasite.Utils.atomize_keys(attrs)) | ||
) | ||
|> apply_changes() | ||
change &change_longitude_latitude/2 | ||
end | ||
end | ||
|
||
def decode_location_point(%{"location_point" => point} = attrs) when is_binary(point), | ||
do: %{attrs | "location_point" => Geo.WKB.decode!(attrs["location_point"])} | ||
code_interface do | ||
define_for Orcasite.Radio | ||
|
||
def decode_location_point(attrs), do: attrs | ||
define :get_feed_by_slug, action: :get_by_slug, args: [:slug], get?: true | ||
end | ||
|
||
calculations do | ||
calculate :longitude_latitude, | ||
:string, | ||
{Orcasite.Radio.Calculations.LongitudeLatitude, | ||
keys: [:location_point], select: [:location_point]} | ||
end | ||
|
||
defp change_longitude_latitude(changeset, _context) do | ||
with {:is_string, lng_lat} when is_binary(lng_lat) <- | ||
{:is_string, Ash.Changeset.get_argument(changeset, :longitude_latitude)}, | ||
{:two_els, [lng, lat]} <- | ||
{:two_els, lng_lat |> String.split(",") |> Enum.map(&String.trim/1)}, | ||
{:two_floats, [{longitude, _}, {latitude, _}]} <- | ||
{:two_floats, [lng, lat] |> Enum.map(&Float.parse/1)} do | ||
changeset | ||
|> Ash.Changeset.change_attribute(:location_point, %Geo.Point{ | ||
coordinates: {longitude, latitude}, | ||
srid: 4326 | ||
}) | ||
else | ||
{:is_string, _} -> | ||
changeset | ||
|
||
{:two_els, _} -> | ||
changeset | ||
|> Ash.Changeset.add_error( | ||
field: :longitude_latitude, | ||
message: "must be a comma-separated string" | ||
) | ||
|
||
{:two_floats, _} -> | ||
changeset | ||
|> Ash.Changeset.add_error(field: :longitude_latitude, message: "must be two floats") | ||
end | ||
end | ||
|
||
admin do | ||
table_columns [:id, :name, :slug, :node_name, :location_point] | ||
|
||
format_fields location_point: {Jason, :encode!, []} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Orcasite.Radio.Registry do | ||
use Ash.Registry, extensions: [Ash.Registry.ResourceValidations] | ||
|
||
entries do | ||
entry Orcasite.Radio.Feed | ||
end | ||
|
||
end |
4 changes: 2 additions & 2 deletions
4
server/lib/orcasite/radio/candidate.ex → ...er/lib/orcasite/radio_legacy/candidate.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
server/lib/orcasite/radio/detection.ex → ...er/lib/orcasite/radio_legacy/detection.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Orcasite.RadioLegacy.Feed do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias __MODULE__ | ||
|
||
schema "feeds" do | ||
field(:name, :string) | ||
field(:slug, :string) | ||
field(:node_name, :string) | ||
field(:location_point, Geo.PostGIS.Geometry) | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(feed, attrs) do | ||
feed | ||
|> cast(attrs, [:name, :node_name, :slug, :location_point]) | ||
|> validate_required([:name, :node_name, :slug]) | ||
end | ||
|
||
def latlong_to_geo(lat, long) when is_float(lat) and is_float(long), | ||
do: Geo.WKT.decode!("SRID=4326;POINT(#{lat} #{long})") | ||
|
||
# TODO: Find the actual json -> schema function | ||
def from_json(attrs) do | ||
%Feed{} | ||
|> cast( | ||
decode_location_point(attrs), | ||
Map.keys(Orcasite.Utils.atomize_keys(attrs)) | ||
) | ||
|> apply_changes() | ||
end | ||
|
||
def decode_location_point(%{"location_point" => point} = attrs) when is_binary(point), | ||
do: %{attrs | "location_point" => Geo.WKB.decode!(attrs["location_point"])} | ||
|
||
def decode_location_point(attrs), do: attrs | ||
end |
31 changes: 3 additions & 28 deletions
31
server/lib/orcasite/radio/radio.ex → ...lib/orcasite/radio_legacy/radio_legacy.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule Orcasite.Types.Geometry do | ||
@moduledoc false | ||
|
||
use Ash.Type | ||
|
||
@impl true | ||
def storage_type, do: :geometry | ||
|
||
@impl true | ||
def cast_input(nil, _), do: {:ok, nil} | ||
|
||
def cast_input(value, _) do | ||
Geo.PostGIS.Geometry.cast(value) | ||
end | ||
|
||
@impl true | ||
def cast_stored(nil, _), do: {:ok, nil} | ||
|
||
def cast_stored(value, _) do | ||
Geo.PostGIS.Geometry.load(value) | ||
end | ||
|
||
@impl true | ||
def dump_to_native(nil, _), do: {:ok, nil} | ||
|
||
def dump_to_native(value, _) do | ||
Geo.PostGIS.Geometry.dump(value) | ||
end | ||
end | ||
|
||
if Code.ensure_loaded?(Ecto.DevLogger) do | ||
defimpl Ecto.DevLogger.PrintableParameter, for: Geo.Point do | ||
def to_expression(point) do | ||
point | ||
|> to_string_literal() | ||
|> Ecto.DevLogger.Utils.in_string_quotes() | ||
end | ||
|
||
def to_string_literal(point) do | ||
Geo.WKT.Encoder.encode!(point) | ||
end | ||
end | ||
end |
Oops, something went wrong.