diff --git a/lib/bump/database.ex b/lib/bump/database.ex index 89e050a..3cd5134 100644 --- a/lib/bump/database.ex +++ b/lib/bump/database.ex @@ -1,6 +1,7 @@ defmodule Bump.Database do alias Bump.Repo alias Bump.Messages.Message + alias Bump.Senders.Sender import Ecto.Query def pop(sender) do @@ -58,5 +59,8 @@ defmodule Bump.Database do end + def addSender(sender, %{"firebase" => firebase}) do + Repo.insert(%Sender{sender: sender, firebase_token: firebase}) + end end diff --git a/lib/bump/messages.ex b/lib/bump/messages.ex index a317d4f..c764ed8 100644 --- a/lib/bump/messages.ex +++ b/lib/bump/messages.ex @@ -3,7 +3,7 @@ defmodule Bump.Messages do alias Bump.Firebase def pop(sender) do - Database.pop(sender) + Database.pop(sender) end def peek(sender) do diff --git a/lib/bump/senders.ex b/lib/bump/senders.ex new file mode 100644 index 0000000..f844e82 --- /dev/null +++ b/lib/bump/senders.ex @@ -0,0 +1,12 @@ +defmodule Bump.Senders do + alias Bump.Database + + def firebase(sender, firebase) do + Database.addSender(sender, %{"firebase" => firebase}) + end + + def peek(sender) do + Database.peek(sender) + end + +end diff --git a/lib/bump/senders/sender.ex b/lib/bump/senders/sender.ex new file mode 100644 index 0000000..b45c193 --- /dev/null +++ b/lib/bump/senders/sender.ex @@ -0,0 +1,18 @@ +defmodule Bump.Senders.Sender do + use Ecto.Schema + import Ecto.Changeset + + schema "senders" do + field :sender, :string + field :firebase_token, :string, default: "" + timestamps() + end + + def changeset(sender, params) do + sender + |> cast(params, [:sender, :firebase_token]) + end +end + + + diff --git a/lib/bump_web/controllers/message_controller.ex b/lib/bump_web/controllers/message_controller.ex index f29a67b..f802496 100644 --- a/lib/bump_web/controllers/message_controller.ex +++ b/lib/bump_web/controllers/message_controller.ex @@ -2,6 +2,7 @@ defmodule BumpWeb.MessageController do use BumpWeb, :controller alias Bump.Messages + alias Bump.Senders def index(conn, _params) do text conn, "Bump Api" @@ -34,4 +35,9 @@ defmodule BumpWeb.MessageController do message = Messages.peek(sender) text conn, Jason.encode!(message) end + + def firebase(conn, %{"sender" => sender, "token" => token}) do + Senders.push(sender, token) + text conn, Jason.encode!(%{"status" => "OK"}) + end end diff --git a/lib/bump_web/controllers/sender_controller.ex b/lib/bump_web/controllers/sender_controller.ex new file mode 100644 index 0000000..56e4405 --- /dev/null +++ b/lib/bump_web/controllers/sender_controller.ex @@ -0,0 +1,10 @@ +defmodule BumpWeb.SenderController do + use BumpWeb, :controller + + alias Bump.Senders + + def firebase(conn, %{"sender" => sender, "token" => token}) do + Senders.firebase(sender, token) + text conn, Jason.encode!(%{"status" => "OK"}) + end +end diff --git a/lib/bump_web/router.ex b/lib/bump_web/router.ex index 19e9f68..ff76710 100644 --- a/lib/bump_web/router.ex +++ b/lib/bump_web/router.ex @@ -12,6 +12,7 @@ defmodule BumpWeb.Router do post "/pop", MessageController, :pop post "/list", MessageController, :list post "/clear", MessageController, :clear + post "/firebase", SenderController, :firebase end diff --git a/priv/repo/migrations/20211230132931_add_senders.exs b/priv/repo/migrations/20211230132931_add_senders.exs new file mode 100644 index 0000000..084e084 --- /dev/null +++ b/priv/repo/migrations/20211230132931_add_senders.exs @@ -0,0 +1,13 @@ +defmodule Bump.Repo.Migrations.AddSenders do + use Ecto.Migration + + def change do + create table :senders do + + add :sender, :string, size: 255, null: false + add :firebase_token, :string, size: 2048 + + timestamps([type: :utc_datetime]) + end + end +end