File size: 3,458 Bytes
88211d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
defmodule PlausibleWeb.Plugins.API.Controllers.SharedLinks do
  @moduledoc """
  Controller for the Shared Link resource under Plugins API
  """
  use PlausibleWeb, :plugins_api_controller

  operation(:index,
    summary: "Retrieve Shared Links",
    parameters: [
      limit: [in: :query, type: :integer, description: "Maximum entries per page", example: 10],
      after: [
        in: :query,
        type: :string,
        description: "Cursor value to seek after - generated internally"
      ],
      before: [
        in: :query,
        type: :string,
        description: "Cursor value to seek before - generated internally"
      ]
    ],
    responses: %{
      ok: {"Shared Links response", "application/json", Schemas.SharedLink.ListResponse},
      unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized}
    }
  )

  @spec index(Plug.Conn.t(), %{}) :: Plug.Conn.t()
  def index(conn, _params) do
    {:ok, pagination} =
      API.SharedLinks.get_shared_links(conn.assigns.authorized_site, conn.query_params)

    conn
    |> put_view(Views.SharedLink)
    |> render("index.json", %{pagination: pagination})
  end

  operation(:create,
    summary: "Get or create Shared Link",
    request_body: {"Shared Link params", "application/json", Schemas.SharedLink.CreateRequest},
    responses: %{
      created: {"Shared Link", "application/json", Schemas.SharedLink},
      unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized},
      unprocessable_entity:
        {"Unprocessable entity", "application/json", Schemas.UnprocessableEntity}
    }
  )

  @spec create(Plug.Conn.t(), map()) :: Plug.Conn.t()
  def create(
        %{
          private: %{
            open_api_spex: %{
              body_params: %Schemas.SharedLink.CreateRequest{
                shared_link: %{name: name} = shared_link
              }
            }
          }
        } = conn,
        _params
      ) do
    site = conn.assigns.authorized_site

    {:ok, shared_link} = API.SharedLinks.get_or_create(site, name, shared_link[:password])

    conn
    |> put_view(Views.SharedLink)
    |> put_status(:created)
    |> put_resp_header("location", plugins_api_shared_links_url(conn, :get, shared_link.id))
    |> render("shared_link.json", shared_link: shared_link, authorized_site: site)
  end

  operation(:get,
    summary: "Retrieve Shared Link by ID",
    parameters: [
      id: [
        in: :path,
        type: :integer,
        description: "Shared Link ID",
        example: 123,
        required: true
      ]
    ],
    responses: %{
      created: {"Shared Link", "application/json", Schemas.SharedLink},
      not_found: {"NotFound", "application/json", Schemas.NotFound},
      unauthorized: {"Unauthorized", "application/json", Schemas.Unauthorized},
      unprocessable_entity:
        {"Unprocessable entity", "application/json", Schemas.UnprocessableEntity}
    }
  )

  @spec get(Plug.Conn.t(), map()) :: Plug.Conn.t()
  def get(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _params) do
    site = conn.assigns.authorized_site

    case API.SharedLinks.get(site, id) do
      nil ->
        conn
        |> put_view(Views.Error)
        |> put_status(:not_found)
        |> render("404.json")

      shared_link ->
        conn
        |> put_view(Views.SharedLink)
        |> put_status(:ok)
        |> render("shared_link.json", shared_link: shared_link, authorized_site: site)
    end
  end
end