updates list rest api

- list no longer returns a json array
  but a json object, which contains a
  "messages" json array instead
This commit is contained in:
Raphael Maenle 2021-12-17 16:23:48 +01:00
parent 5d50bb30e6
commit 8262b61085
7 changed files with 14 additions and 14 deletions

View File

@ -12,7 +12,7 @@ config :bump,
# Configures the endpoint # Configures the endpoint
config :bump, BumpWeb.Endpoint, config :bump, BumpWeb.Endpoint,
url: [host: "localhost"], url: [host: "0.0.0.0"],
render_errors: [view: BumpWeb.ErrorView, accepts: ~w(html json), layout: false], render_errors: [view: BumpWeb.ErrorView, accepts: ~w(html json), layout: false],
pubsub_server: Bump.PubSub, pubsub_server: Bump.PubSub,
live_view: [signing_salt: "IpqpsnNX"] live_view: [signing_salt: "IpqpsnNX"]

View File

@ -18,7 +18,7 @@ config :bump, Bump.Repo,
config :bump, BumpWeb.Endpoint, config :bump, BumpWeb.Endpoint,
# Binding to loopback ipv4 address prevents access from other machines. # Binding to loopback ipv4 address prevents access from other machines.
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines. # Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
http: [ip: {127, 0, 0, 1}, port: 4000], http: [ip: {0, 0, 0, 0}, port: 4000],
check_origin: false, check_origin: false,
code_reloader: true, code_reloader: true,
debug_errors: true, debug_errors: true,

View File

@ -9,7 +9,7 @@ config :bump, Bump.Repo,
username: "postgres", username: "postgres",
password: "postgres", password: "postgres",
database: "bump_test#{System.get_env("MIX_TEST_PARTITION")}", database: "bump_test#{System.get_env("MIX_TEST_PARTITION")}",
hostname: "localhost", hostname: "tower.local",
pool: Ecto.Adapters.SQL.Sandbox, pool: Ecto.Adapters.SQL.Sandbox,
pool_size: 10 pool_size: 10

View File

@ -1,5 +1,5 @@
curl -X POST http://localhost:4000/api/push -H "Content-Type: application/json" -d '{"sender": "raphael", "message": "Hello"}' curl -X POST http://127.0.0.1:4000/api/push -H "Content-Type: application/json" -d '{"sender": "raphael", "data": "Hello"}'
# curl -X POST http://localhost:4000/api/peak -H "Content-Type: application/json" -d '{"sender": "raphael"}' # curl -X POST http://localhost:4000/api/peek -H "Content-Type: application/json" -d '{"sender": "raphael"}'
# curl -X POST http://localhost:4000/api/pop -H "Content-Type: application/json" -d '{"sender": "raphael"}' # curl -X POST http://localhost:4000/api/pop -H "Content-Type: application/json" -d '{"sender": "raphael"}'
# curl -X POST http://localhost:4000/api/list -H "Content-Type: application/json" -d '{"sender": "raphael", "minutes": "40"}' # curl -X POST http://localhost:4000/api/list -H "Content-Type: application/json" -d '{"sender": "raphael", "minutes": "40"}'
# curl -X POST http://localhost:4000/api/clear -H "Content-Type: application/json" -d '{"sender": "raphael"}' # curl -X POST http://localhost:4000/api/clear -H "Content-Type: application/json" -d '{"sender": "raphael"}'

View File

@ -16,12 +16,12 @@ defmodule Bump.Messages do
Repo.delete_all(from m in "messages", where: m.id == ^res.id) Repo.delete_all(from m in "messages", where: m.id == ^res.id)
%{data: res.data, timestamp: res.timestamp} %{data: res.data, timestamp: res.timestamp}
else else
nil %{}
end end
end end
def peak(sender) do def peek(sender) do
query = from m in "messages", query = from m in "messages",
where: m.sender == ^sender, where: m.sender == ^sender,
order_by: [desc: m.timestamp], order_by: [desc: m.timestamp],

View File

@ -9,19 +9,19 @@ defmodule BumpWeb.MessageController do
def list(conn, %{"sender" => sender, "minutes" => minutes}) do def list(conn, %{"sender" => sender, "minutes" => minutes}) do
list = Messages.list(sender, String.to_integer(minutes)) list = Messages.list(sender, String.to_integer(minutes))
text conn, Jason.encode!(list) text conn, Jason.encode!(%{"messages" => list})
end end
def clear(conn, %{"sender" => sender}) do def clear(conn, %{"sender" => sender}) do
Messages.clear(sender) Messages.clear(sender)
text conn, "OK" text conn, Jason.encode!(%{"status" => "OK"})
end end
def push(conn, %{"sender" => sender, "message" => message}) do def push(conn, %{"sender" => sender, "data" => message}) do
Messages.push(sender, message) Messages.push(sender, message)
text conn, "OK" text conn, Jason.encode!(%{"status" => "OK"})
end end
@ -30,8 +30,8 @@ defmodule BumpWeb.MessageController do
text conn, Jason.encode!(message) text conn, Jason.encode!(message)
end end
def peak(conn, %{"sender" => sender}) do def peek(conn, %{"sender" => sender}) do
message = Messages.peak(sender) message = Messages.peek(sender)
text conn, Jason.encode!(message) text conn, Jason.encode!(message)
end end
end end

View File

@ -8,7 +8,7 @@ defmodule BumpWeb.Router do
scope "/api", BumpWeb do scope "/api", BumpWeb do
pipe_through :api pipe_through :api
post "/push", MessageController, :push post "/push", MessageController, :push
post "/peak", MessageController, :peak post "/peek", MessageController, :peek
post "/pop", MessageController, :pop post "/pop", MessageController, :pop
post "/list", MessageController, :list post "/list", MessageController, :list
post "/clear", MessageController, :clear post "/clear", MessageController, :clear