From 8262b61085a7fd4666102dfa905c462ddf204b4d Mon Sep 17 00:00:00 2001 From: raphael Date: Fri, 17 Dec 2021 16:23:48 +0100 Subject: [PATCH] updates list rest api - list no longer returns a json array but a json object, which contains a "messages" json array instead --- config/config.exs | 2 +- config/dev.exs | 2 +- config/test.exs | 2 +- curl.sh | 4 ++-- lib/bump/messages.ex | 4 ++-- lib/bump_web/controllers/message_controller.ex | 12 ++++++------ lib/bump_web/router.ex | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/config/config.exs b/config/config.exs index 44b02be..bccd212 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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"] diff --git a/config/dev.exs b/config/dev.exs index 2a83a66..1ce9156 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -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, diff --git a/config/test.exs b/config/test.exs index 0a81dfb..2091fa7 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/curl.sh b/curl.sh index 270d5e1..da10b85 100644 --- a/curl.sh +++ b/curl.sh @@ -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"}' diff --git a/lib/bump/messages.ex b/lib/bump/messages.ex index 33aa68b..6cb28f3 100644 --- a/lib/bump/messages.ex +++ b/lib/bump/messages.ex @@ -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], diff --git a/lib/bump_web/controllers/message_controller.ex b/lib/bump_web/controllers/message_controller.ex index 56f8abb..f29a67b 100644 --- a/lib/bump_web/controllers/message_controller.ex +++ b/lib/bump_web/controllers/message_controller.ex @@ -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 diff --git a/lib/bump_web/router.ex b/lib/bump_web/router.ex index 5d80ff5..19e9f68 100644 --- a/lib/bump_web/router.ex +++ b/lib/bump_web/router.ex @@ -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