summaryrefslogtreecommitdiff
path: root/lib/silmataivas_web/controllers/location_controller.ex
blob: d494d59eb9425ca7f8ed6c3887bb52eaa9d1dc0f (plain)
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
defmodule SilmataivasWeb.LocationController do
  use SilmataivasWeb, :controller

  alias Silmataivas.Locations
  alias Silmataivas.Locations.Location

  action_fallback SilmataivasWeb.FallbackController

  def index(conn, _params) do
    locations = Locations.list_locations()
    render(conn, :index, locations: locations)
  end

  def create(conn, params) do
    user = conn.assigns.current_user
    params = Map.put(params, "user_id", user.id)

    with {:ok, %Location{} = location} <- Locations.create_location(params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", ~p"/api/locations/#{location}")
      |> render(:show, location: location)
    end
  end

  def show(conn, %{"id" => id}) do
    location = Locations.get_location!(id)
    render(conn, :show, location: location)
  end

  def update(conn, %{"id" => id, "location" => location_params}) do
    location = Locations.get_location!(id)

    with {:ok, %Location{} = location} <- Locations.update_location(location, location_params) do
      render(conn, :show, location: location)
    end
  end

  def delete(conn, %{"id" => id}) do
    location = Locations.get_location!(id)

    with {:ok, %Location{}} <- Locations.delete_location(location) do
      send_resp(conn, :no_content, "")
    end
  end
end