~robertgzr/greenfy

79bc699395a3f74147d9e630ac113fcc9feb2d1c — Robert G├╝nzler 2 years ago bcee093
rename events to event
M lib/greenfy/log.ex => lib/greenfy/log.ex +35 -35
@@ 6,7 6,7 @@ defmodule Greenfy.Log do
  import Ecto.Query, warn: false
  alias Greenfy.Repo

  alias Greenfy.Log.Events
  alias Greenfy.Log.Event

  @topic inspect(__MODULE__)



@@ 31,95 31,95 @@ defmodule Greenfy.Log do

  ## Examples

      iex> list_events()
      [%Events{}, ...]
      iex> list_event()
      [%Event{}, ...]

  """
  def list_events do
    Repo.all(Events)
    Repo.all(Event)
  end

  @doc """
  Gets a single events.
  Gets a single event.

  Raises `Ecto.NoResultsError` if the Events does not exist.
  Raises `Ecto.NoResultsError` if the Event does not exist.

  ## Examples

      iex> get_events!(123)
      %Events{}
      iex> get_event!(123)
      %Event{}

      iex> get_events!(456)
      iex> get_event!(456)
      ** (Ecto.NoResultsError)

  """
  def get_events!(id), do: Repo.get!(Events, id)
  def get_event!(id), do: Repo.get!(Event, id)

  @doc """
  Creates a events.
  Creates a event.

  ## Examples

      iex> create_events(%{field: value})
      {:ok, %Events{}}
      iex> create_event(%{field: value})
      {:ok, %Event{}}

      iex> create_events(%{field: bad_value})
      iex> create_event(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_events(attrs \\ %{}) do
    %Events{}
    |> Events.changeset(attrs)
  def create_event(attrs \\ %{}) do
    %Event{}
    |> Event.changeset(attrs)
    |> Repo.insert()
    |> broadcast(:event_created)
  end

  @doc """
  Updates a events.
  Updates a event.

  ## Examples

      iex> update_events(events, %{field: new_value})
      {:ok, %Events{}}
      iex> update_event(event, %{field: new_value})
      {:ok, %Event{}}

      iex> update_events(events, %{field: bad_value})
      iex> update_event(event, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_events(%Events{} = events, attrs) do
    events
    |> Events.changeset(attrs)
  def update_event(%Event{} = event, attrs) do
    event
    |> Event.changeset(attrs)
    |> Repo.update()
    |> broadcast(:event_updated)
  end

  @doc """
  Deletes a events.
  Deletes a event.

  ## Examples

      iex> delete_events(events)
      {:ok, %Events{}}
      iex> delete_event(event)
      {:ok, %Event{}}

      iex> delete_events(events)
      iex> delete_event(event)
      {:error, %Ecto.Changeset{}}

  """
  def delete_events(%Events{} = events) do
    Repo.delete(events)
  def delete_event(%Event{} = event) do
    Repo.delete(event)
    |> broadcast(:event_deleted)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking events changes.
  Returns an `%Ecto.Changeset{}` for tracking event changes.

  ## Examples

      iex> change_events(events)
      %Ecto.Changeset{data: %Events{}}
      iex> change_event(event)
      %Ecto.Changeset{data: %Event{}}

  """
  def change_events(%Events{} = events, attrs \\ %{}) do
    Events.changeset(events, attrs)
  def change_event(%Event{} = event, attrs \\ %{}) do
    Event.changeset(event, attrs)
  end
end

M lib/greenfy/log/events.ex => lib/greenfy/log/events.ex +4 -4
@@ 1,8 1,8 @@
defmodule Greenfy.Log.Events do
defmodule Greenfy.Log.Event do
  use Ecto.Schema
  import Ecto.Changeset

  schema "events" do
  schema "event" do
    field :event_type, :string
    field :message, :string
    field :payload, :string


@@ 11,8 11,8 @@ defmodule Greenfy.Log.Events do
  end

  @doc false
  def changeset(events, attrs) do
    events
  def changeset(event, attrs) do
    event
    |> cast(attrs, [:event_type, :message, :payload])
    |> validate_required([:event_type, :message, :payload])
  end

M lib/greenfy_web/live/logs.ex => lib/greenfy_web/live/logs.ex +2 -2
@@ 36,7 36,7 @@ defmodule GreenfyWeb.LogsLive do
    {:noreply, assign(socket, events: Log.list_events(), new: new.id)}
  end

  def handle_info({:event_deleted, del}, socket) do
    {:noreply, assign(socket, events: Log.list_events(), del: del.id)}
  def handle_info({:event_deleted, _}, socket) do
    {:noreply, assign(socket, events: Log.list_events(), del: true)}
  end
end

M lib/greenfy_web/schema/schema.ex => lib/greenfy_web/schema/schema.ex +2 -2
@@ 22,7 22,7 @@ defmodule Greenfy.Schema do

      resolve(fn _root, args, _info ->
        try do
          case Log.create_events(args) do
          case Log.create_event(args) do
            {:ok, event} -> {:ok, event}
            _error -> {:error, "could not create event"}
          end


@@ 40,7 40,7 @@ defmodule Greenfy.Schema do
        try do
          {id, ""} = Integer.parse(args.id)

          case Log.delete_events(%Log.Events{id: id}) do
          case Log.delete_event(%Log.Event{id: id}) do
            {:ok, event} -> {:ok, event}
            _error -> {:error, "could not delete event"}
          end

A priv/repo/migrations/20220422191348_rename_event.exs => priv/repo/migrations/20220422191348_rename_event.exs +7 -0
@@ 0,0 1,7 @@
defmodule Greenfy.Repo.Migrations.RenameEvent do
  use Ecto.Migration

  def change do
    rename table(:events), to: table(:event)
  end
end

M test/greenfy/log_test.exs => test/greenfy/log_test.exs +43 -34
@@ 3,61 3,70 @@ defmodule Greenfy.LogTest do

  alias Greenfy.Log

  describe "events" do
    alias Greenfy.Log.Events
  describe "event" do
    alias Greenfy.Log.Event

    import Greenfy.LogFixtures

    @invalid_attrs %{event_type: nil, message: nil, payload: nil}

    test "list_events/0 returns all events" do
      events = events_fixture()
      assert Log.list_events() == [events]
    test "list_events/0 returns all event" do
      event = event_fixture()
      assert Log.list_events() == [event]
    end

    test "get_events!/1 returns the events with given id" do
      events = events_fixture()
      assert Log.get_events!(events.id) == events
    test "get_event!/1 returns the event with given id" do
      event = event_fixture()
      assert Log.get_event!(event.id) == event
    end

    test "create_events/1 with valid data creates a events" do
      valid_attrs = %{event_type: "some event_type", message: "some message", payload: "some payload"}
    test "create_event/1 with valid data creates a event" do
      valid_attrs = %{
        event_type: "some event_type",
        message: "some message",
        payload: "some payload"
      }

      assert {:ok, %Events{} = events} = Log.create_events(valid_attrs)
      assert events.event_type == "some event_type"
      assert events.message == "some message"
      assert events.payload == "some payload"
      assert {:ok, %Event{} = event} = Log.create_event(valid_attrs)
      assert event.event_type == "some event_type"
      assert event.message == "some message"
      assert event.payload == "some payload"
    end

    test "create_events/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Log.create_events(@invalid_attrs)
    test "create_event/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Log.create_event(@invalid_attrs)
    end

    test "update_events/2 with valid data updates the events" do
      events = events_fixture()
      update_attrs = %{event_type: "some updated event_type", message: "some updated message", payload: "some updated payload"}
    test "update_event/2 with valid data updates the event" do
      event = event_fixture()

      assert {:ok, %Events{} = events} = Log.update_events(events, update_attrs)
      assert events.event_type == "some updated event_type"
      assert events.message == "some updated message"
      assert events.payload == "some updated payload"
      update_attrs = %{
        event_type: "some updated event_type",
        message: "some updated message",
        payload: "some updated payload"
      }

      assert {:ok, %Event{} = event} = Log.update_event(event, update_attrs)
      assert event.event_type == "some updated event_type"
      assert event.message == "some updated message"
      assert event.payload == "some updated payload"
    end

    test "update_events/2 with invalid data returns error changeset" do
      events = events_fixture()
      assert {:error, %Ecto.Changeset{}} = Log.update_events(events, @invalid_attrs)
      assert events == Log.get_events!(events.id)
    test "update_event/2 with invalid data returns error changeset" do
      event = event_fixture()
      assert {:error, %Ecto.Changeset{}} = Log.update_event(event, @invalid_attrs)
      assert event == Log.get_event!(event.id)
    end

    test "delete_events/1 deletes the events" do
      events = events_fixture()
      assert {:ok, %Events{}} = Log.delete_events(events)
      assert_raise Ecto.NoResultsError, fn -> Log.get_events!(events.id) end
    test "delete_event/1 deletes the event" do
      event = event_fixture()
      assert {:ok, %Event{}} = Log.delete_event(event)
      assert_raise Ecto.NoResultsError, fn -> Log.get_event!(event.id) end
    end

    test "change_events/1 returns a events changeset" do
      events = events_fixture()
      assert %Ecto.Changeset{} = Log.change_events(events)
    test "change_event/1 returns a event changeset" do
      event = event_fixture()
      assert %Ecto.Changeset{} = Log.change_event(event)
    end
  end
end

M test/support/fixtures/log_fixtures.ex => test/support/fixtures/log_fixtures.ex +2 -2
@@ 7,7 7,7 @@ defmodule Greenfy.LogFixtures do
  @doc """
  Generate a events.
  """
  def events_fixture(attrs \\ %{}) do
  def event_fixture(attrs \\ %{}) do
    {:ok, events} =
      attrs
      |> Enum.into(%{


@@ 15,7 15,7 @@ defmodule Greenfy.LogFixtures do
        message: "some message",
        payload: "some payload"
      })
      |> Greenfy.Log.create_events()
      |> Greenfy.Log.create_event()

    events
  end