architex/lib/matrix_server_web/client/room_controller.ex
2021-08-06 23:14:27 +02:00

34 lines
841 B
Elixir

defmodule MatrixServerWeb.Client.RoomController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Plug.Error
import Ecto.Changeset
alias MatrixServer.Room
alias MatrixServerWeb.Request.{CreateRoom}
alias Ecto.Changeset
alias Plug.Conn
def create(%Conn{assigns: %{account: account}} = conn, params) do
case CreateRoom.changeset(params) do
%Changeset{valid?: true} = cs ->
input = apply_changes(cs)
case Room.create(account, input) do
{:ok, room_id} ->
conn
|> put_status(200)
|> json(%{room_id: room_id})
{:error, :authorization} ->
put_error(conn, :invalid_room_state)
{:error, :unknown} ->
put_error(conn, :unknown)
end
_ ->
put_error(conn, :bad_json)
end
end
end