Created
October 21, 2016 19:23
-
-
Save bnhansn/10da4c25bbc587c2763b308f9bc957ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Sling.RoomController do | |
| use Sling.Web, :controller | |
| alias Sling.Room | |
| plug Guardian.Plug.EnsureAuthenticated, handler: Sling.SessionController | |
| def index(conn, _params) do | |
| rooms = Repo.all(Room) | |
| render(conn, "index.json", rooms: rooms) | |
| end | |
| def create(conn, params) do | |
| current_user = Guardian.Plug.current_resource(conn) | |
| changeset = Room.changeset(%Room{}, params) | |
| case Repo.insert(changeset) do | |
| {:ok, room} -> | |
| assoc_changeset = Sling.UserRoom.changeset( | |
| %Sling.UserRoom{}, | |
| %{user_id: current_user.id, room_id: room.id} | |
| ) | |
| Repo.insert(assoc_changeset) | |
| conn | |
| |> put_status(:created) | |
| |> render("show.json", room: room) | |
| {:error, changeset} -> | |
| conn | |
| |> put_status(:unprocessable_entity) | |
| |> render(Sling.ChangesetView, "error.json", changeset: changeset) | |
| end | |
| end | |
| def join(conn, %{"id" => room_id}) do | |
| current_user = Guardian.Plug.current_resource(conn) | |
| room = Repo.get(Room, room_id) | |
| changeset = Sling.UserRoom.changeset( | |
| %Sling.UserRoom{}, | |
| %{room_id: room.id, user_id: current_user.id} | |
| ) | |
| case Repo.insert(changeset) do | |
| {:ok, _user_room} -> | |
| conn | |
| |> put_status(:created) | |
| |> render("show.json", %{room: room}) | |
| {:error, changeset} -> | |
| conn | |
| |> put_status(:unprocessable_entity) | |
| |> render(Sling.ChangesetView, "error.json", changeset: changeset) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment