feat: add current room occupancy endpoint
This commit is contained in:
@@ -2,12 +2,21 @@ defmodule Localiser.Web.Controllers.TagController do
|
||||
use Phoenix.Controller, formats: [:json]
|
||||
use OpenApiSpex.ControllerSpecs
|
||||
|
||||
alias Localiser.Domain.Tags
|
||||
alias Localiser.Domain.{Rooms, Tags}
|
||||
alias Localiser.Localisation.Tag.Filter, as: TagFilter
|
||||
alias Localiser.Web.Schemas
|
||||
|
||||
tags ["Tags"]
|
||||
security [%{"bearerAuth" => []}]
|
||||
|
||||
operation :occupancy,
|
||||
summary: "Get every tag's current room",
|
||||
responses: [
|
||||
ok: {"Tag occupancy list", "application/json",
|
||||
%OpenApiSpex.Schema{type: :array, items: Schemas.TagOccupancy}},
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
|
||||
]
|
||||
|
||||
operation :index,
|
||||
summary: "List all tags",
|
||||
responses: [
|
||||
@@ -50,6 +59,18 @@ defmodule Localiser.Web.Controllers.TagController do
|
||||
unauthorized: {"Unauthorized", "application/json", Schemas.Error}
|
||||
]
|
||||
|
||||
def occupancy(conn, _params) do
|
||||
tag_to_room = build_tag_to_room_map()
|
||||
|
||||
result =
|
||||
Tags.list_tags()
|
||||
|> Enum.map(fn tag ->
|
||||
%{id: tag.id, tag_id: tag.tag_id, name: tag.name, room_id: Map.get(tag_to_room, tag.tag_id)}
|
||||
end)
|
||||
|
||||
json(conn, result)
|
||||
end
|
||||
|
||||
def index(conn, _params) do
|
||||
json(conn, Enum.map(Tags.list_tags(), &render_tag/1))
|
||||
end
|
||||
@@ -94,6 +115,20 @@ defmodule Localiser.Web.Controllers.TagController do
|
||||
send_resp(conn, :no_content, "")
|
||||
end
|
||||
|
||||
defp build_tag_to_room_map do
|
||||
Rooms.list_rooms()
|
||||
|> Enum.flat_map(fn room ->
|
||||
occupants =
|
||||
case Registry.lookup(Localiser.Registry, {:room, room.id}) do
|
||||
[{pid, _}] -> GenServer.call(pid, :get_occupants)
|
||||
[] -> MapSet.new()
|
||||
end
|
||||
|
||||
Enum.map(MapSet.to_list(occupants), &{&1, room.id})
|
||||
end)
|
||||
|> Map.new()
|
||||
end
|
||||
|
||||
defp render_tag(tag) do
|
||||
live = TagFilter.get_live_state(tag.tag_id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user