architex/lib/matrix_server_web/client/controllers/room_controller.ex

35 lines
841 B
Elixir
Raw Normal View History

2021-08-06 21:14:27 +00:00
defmodule MatrixServerWeb.Client.RoomController do
2021-07-10 21:16:00 +00:00
use MatrixServerWeb, :controller
import MatrixServerWeb.Error
2021-07-10 21:16:00 +00:00
import Ecto.Changeset
2021-07-29 20:06:02 +00:00
alias MatrixServer.Room
alias MatrixServerWeb.Client.Request.CreateRoom
2021-07-10 21:16:00 +00:00
alias Ecto.Changeset
2021-07-17 15:38:20 +00:00
alias Plug.Conn
2021-07-10 21:16:00 +00:00
2021-07-17 15:38:20 +00:00
def create(%Conn{assigns: %{account: account}} = conn, params) do
2021-07-10 21:16:00 +00:00
case CreateRoom.changeset(params) do
%Changeset{valid?: true} = cs ->
input = apply_changes(cs)
2021-07-29 20:06:02 +00:00
case Room.create(account, input) do
{:ok, room_id} ->
conn
|> put_status(200)
|> json(%{room_id: room_id})
2021-07-10 21:16:00 +00:00
2021-07-29 20:06:02 +00:00
{:error, :authorization} ->
put_error(conn, :invalid_room_state)
{:error, :unknown} ->
put_error(conn, :unknown)
end
2021-07-10 21:16:00 +00:00
_ ->
put_error(conn, :bad_json)
end
end
end