summaryrefslogtreecommitdiff
path: root/lib/silmataivas_web/controllers
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-03-23 17:11:39 +0100
committerDawid Rycerz <dawid@rycerz.xyz>2025-04-05 21:16:51 +0200
commit0ab2e5ba2b0631b28b5b1405559237b3913c878f (patch)
tree791cea788b0a62bc483d0041fbd0c655d2ad49e8 /lib/silmataivas_web/controllers
feat: initialize Phoenix application for weather alerts
This commit sets up the initial Silmataivas project structure, including: Phoenix web framework configuration, database models for users and locations, weather polling service, notification system, Docker and deployment configurations, CI/CD pipeline setup
Diffstat (limited to 'lib/silmataivas_web/controllers')
-rw-r--r--lib/silmataivas_web/controllers/changeset_json.ex25
-rw-r--r--lib/silmataivas_web/controllers/error_json.ex21
-rw-r--r--lib/silmataivas_web/controllers/fallback_controller.ex24
-rw-r--r--lib/silmataivas_web/controllers/health_controller.ex9
-rw-r--r--lib/silmataivas_web/controllers/location_controller.ex46
-rw-r--r--lib/silmataivas_web/controllers/location_json.ex25
6 files changed, 150 insertions, 0 deletions
diff --git a/lib/silmataivas_web/controllers/changeset_json.ex b/lib/silmataivas_web/controllers/changeset_json.ex
new file mode 100644
index 0000000..ac0226d
--- /dev/null
+++ b/lib/silmataivas_web/controllers/changeset_json.ex
@@ -0,0 +1,25 @@
+defmodule SilmataivasWeb.ChangesetJSON do
+ @doc """
+ Renders changeset errors.
+ """
+ def error(%{changeset: changeset}) do
+ # When encoded, the changeset returns its errors
+ # as a JSON object. So we just pass it forward.
+ %{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)}
+ end
+
+ defp translate_error({msg, opts}) do
+ # You can make use of gettext to translate error messages by
+ # uncommenting and adjusting the following code:
+
+ # if count = opts[:count] do
+ # Gettext.dngettext(SilmataivasWeb.Gettext, "errors", msg, msg, count, opts)
+ # else
+ # Gettext.dgettext(SilmataivasWeb.Gettext, "errors", msg, opts)
+ # end
+
+ Enum.reduce(opts, msg, fn {key, value}, acc ->
+ String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
+ end)
+ end
+end
diff --git a/lib/silmataivas_web/controllers/error_json.ex b/lib/silmataivas_web/controllers/error_json.ex
new file mode 100644
index 0000000..a2ca902
--- /dev/null
+++ b/lib/silmataivas_web/controllers/error_json.ex
@@ -0,0 +1,21 @@
+defmodule SilmataivasWeb.ErrorJSON do
+ @moduledoc """
+ This module is invoked by your endpoint in case of errors on JSON requests.
+
+ See config/config.exs.
+ """
+
+ # If you want to customize a particular status code,
+ # you may add your own clauses, such as:
+ #
+ # def render("500.json", _assigns) do
+ # %{errors: %{detail: "Internal Server Error"}}
+ # end
+
+ # By default, Phoenix returns the status message from
+ # the template name. For example, "404.json" becomes
+ # "Not Found".
+ def render(template, _assigns) do
+ %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
+ end
+end
diff --git a/lib/silmataivas_web/controllers/fallback_controller.ex b/lib/silmataivas_web/controllers/fallback_controller.ex
new file mode 100644
index 0000000..f315110
--- /dev/null
+++ b/lib/silmataivas_web/controllers/fallback_controller.ex
@@ -0,0 +1,24 @@
+defmodule SilmataivasWeb.FallbackController do
+ @moduledoc """
+ Translates controller action results into valid `Plug.Conn` responses.
+
+ See `Phoenix.Controller.action_fallback/1` for more details.
+ """
+ use SilmataivasWeb, :controller
+
+ # This clause handles errors returned by Ecto's insert/update/delete.
+ def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
+ conn
+ |> put_status(:unprocessable_entity)
+ |> put_view(json: SilmataivasWeb.ChangesetJSON)
+ |> render(:error, changeset: changeset)
+ end
+
+ # This clause is an example of how to handle resources that cannot be found.
+ def call(conn, {:error, :not_found}) do
+ conn
+ |> put_status(:not_found)
+ |> put_view(html: SilmataivasWeb.ErrorHTML, json: SilmataivasWeb.ErrorJSON)
+ |> render(:"404")
+ end
+end
diff --git a/lib/silmataivas_web/controllers/health_controller.ex b/lib/silmataivas_web/controllers/health_controller.ex
new file mode 100644
index 0000000..959b84b
--- /dev/null
+++ b/lib/silmataivas_web/controllers/health_controller.ex
@@ -0,0 +1,9 @@
+defmodule SilmataivasWeb.HealthController do
+ use SilmataivasWeb, :controller
+
+ def index(conn, _params) do
+ conn
+ |> put_status(:ok)
+ |> json(%{status: "ok"})
+ end
+end
diff --git a/lib/silmataivas_web/controllers/location_controller.ex b/lib/silmataivas_web/controllers/location_controller.ex
new file mode 100644
index 0000000..d494d59
--- /dev/null
+++ b/lib/silmataivas_web/controllers/location_controller.ex
@@ -0,0 +1,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
diff --git a/lib/silmataivas_web/controllers/location_json.ex b/lib/silmataivas_web/controllers/location_json.ex
new file mode 100644
index 0000000..db7e469
--- /dev/null
+++ b/lib/silmataivas_web/controllers/location_json.ex
@@ -0,0 +1,25 @@
+defmodule SilmataivasWeb.LocationJSON do
+ alias Silmataivas.Locations.Location
+
+ @doc """
+ Renders a list of locations.
+ """
+ def index(%{locations: locations}) do
+ %{data: for(location <- locations, do: data(location))}
+ end
+
+ @doc """
+ Renders a single location.
+ """
+ def show(%{location: location}) do
+ %{data: data(location)}
+ end
+
+ defp data(%Location{} = location) do
+ %{
+ id: location.id,
+ latitude: location.latitude,
+ longitude: location.longitude
+ }
+ end
+end