Skip to content

Commit

Permalink
add: api-endpoint for creating nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Nov 23, 2023
1 parent 525c4c8 commit edf7be2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/radiator_web/controllers/api/outline_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule RadiatorWeb.Api.OutlineController do
use RadiatorWeb, :controller

alias Radiator.Outline

def create(conn, %{"content" => content}) do
{:ok, node} = Outline.create_node(%{"content" => content})

conn
|> put_resp_content_type("application/json")
|> send_resp(200, Jason.encode!(%{uuid: node.uuid}))
end

def create(conn, _params) do
conn
|> put_resp_content_type("application/json")
|> send_resp(500, Jason.encode!(%{error: "params"}))
end
end
8 changes: 5 additions & 3 deletions lib/radiator_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ defmodule RadiatorWeb.Router do
end

# Other scopes may use custom stacks.
# scope "/api", RadiatorWeb do
# pipe_through :api
# end
scope "/api", RadiatorWeb.Api do
pipe_through :api

post "/v1/outline", OutlineController, :create
end

# Enable LiveDashboard and Swoosh mailbox preview in development
if Application.compile_env(:radiator, :dev_routes) do
Expand Down
16 changes: 16 additions & 0 deletions test/radiator_web/controllers/api/outline_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule RadiatorWeb.Api.OutlineControllerTest do
use RadiatorWeb.ConnCase, async: true

alias Radiator.Outline

describe "POST /api/v1/outline" do
test "creates a node", %{conn: conn} do
body = %{"content" => "new node content"}
conn = post(conn, ~p"/api/v1/outline", body)

%{"uuid" => uuid} = json_response(conn, 200)

assert %{content: "new node content"} = Outline.get_node!(uuid)
end
end
end

0 comments on commit edf7be2

Please sign in to comment.