adds senders to database and firebase connect

- senders, which now have a firebase token
  are added as their own table in the database
- firebase token api call added, which allows
  a user to connect a sender id to a firebase token
- firebase library not yet connected to the saved
  firebase tokens
This commit is contained in:
Raphael Maenle 2021-12-30 16:11:53 +01:00
parent 745b0dc4f2
commit 60888fd63c
8 changed files with 65 additions and 1 deletions

View File

@ -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

View File

@ -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

12
lib/bump/senders.ex Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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