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
config :bump, BumpWeb.Endpoint,
url: [host: "localhost"],
url: [host: "0.0.0.0"],
render_errors: [view: BumpWeb.ErrorView, accepts: ~w(html json), layout: false],
pubsub_server: Bump.PubSub,
live_view: [signing_salt: "IpqpsnNX"]

View File

@ -18,7 +18,7 @@ config :bump, Bump.Repo,
config :bump, BumpWeb.Endpoint,
# Binding to loopback ipv4 address prevents 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,
code_reloader: true,
debug_errors: true,

View File

@ -9,7 +9,7 @@ config :bump, Bump.Repo,
username: "postgres",
password: "postgres",
database: "bump_test#{System.get_env("MIX_TEST_PARTITION")}",
hostname: "localhost",
hostname: "tower.local",
pool: Ecto.Adapters.SQL.Sandbox,
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://localhost:4000/api/peak -H "Content-Type: application/json" -d '{"sender": "raphael"}'
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/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/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"}'

View File

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

View File

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

View File

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