add steps

This commit is contained in:
shibao 2022-07-25 20:18:39 -04:00
parent 0fca0ead12
commit 8b5806116a
5 changed files with 228 additions and 0 deletions

104
lib/memex/steps.ex Normal file
View File

@ -0,0 +1,104 @@
defmodule Memex.Steps do
@moduledoc """
The Steps context.
"""
import Ecto.Query, warn: false
alias Memex.Repo
alias Memex.Steps.Step
@doc """
Returns the list of steps.
## Examples
iex> list_steps()
[%Step{}, ...]
"""
def list_steps do
Repo.all(Step)
end
@doc """
Gets a single step.
Raises `Ecto.NoResultsError` if the Step does not exist.
## Examples
iex> get_step!(123)
%Step{}
iex> get_step!(456)
** (Ecto.NoResultsError)
"""
def get_step!(id), do: Repo.get!(Step, id)
@doc """
Creates a step.
## Examples
iex> create_step(%{field: value})
{:ok, %Step{}}
iex> create_step(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_step(attrs \\ %{}) do
%Step{}
|> Step.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a step.
## Examples
iex> update_step(step, %{field: new_value})
{:ok, %Step{}}
iex> update_step(step, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_step(%Step{} = step, attrs) do
step
|> Step.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a step.
## Examples
iex> delete_step(step)
{:ok, %Step{}}
iex> delete_step(step)
{:error, %Ecto.Changeset{}}
"""
def delete_step(%Step{} = step) do
Repo.delete(step)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking step changes.
## Examples
iex> change_step(step)
%Ecto.Changeset{data: %Step{}}
"""
def change_step(%Step{} = step, attrs \\ %{}) do
Step.changeset(step, attrs)
end
end

22
lib/memex/steps/step.ex Normal file
View File

@ -0,0 +1,22 @@
defmodule Memex.Steps.Step do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "steps" do
field :description, :string
field :position, :integer
field :title, :string
field :pipeline_id, :binary_id
timestamps()
end
@doc false
def changeset(step, attrs) do
step
|> cast(attrs, [:title, :description, :position])
|> validate_required([:title, :description, :position])
end
end

View File

@ -0,0 +1,17 @@
defmodule Memex.Repo.Migrations.CreateSteps do
use Ecto.Migration
def change do
create table(:steps, primary_key: false) do
add :id, :binary_id, primary_key: true
add :title, :string
add :description, :text
add :position, :integer
add :pipeline_id, references(:pipelines, on_delete: :nothing, type: :binary_id)
timestamps()
end
create index(:steps, [:pipeline_id])
end
end

63
test/memex/steps_test.exs Normal file
View File

@ -0,0 +1,63 @@
defmodule Memex.StepsTest do
use Memex.DataCase
alias Memex.Steps
describe "steps" do
alias Memex.Steps.Step
import Memex.StepsFixtures
@invalid_attrs %{description: nil, position: nil, title: nil}
test "list_steps/0 returns all steps" do
step = step_fixture()
assert Steps.list_steps() == [step]
end
test "get_step!/1 returns the step with given id" do
step = step_fixture()
assert Steps.get_step!(step.id) == step
end
test "create_step/1 with valid data creates a step" do
valid_attrs = %{description: "some description", position: 42, title: "some title"}
assert {:ok, %Step{} = step} = Steps.create_step(valid_attrs)
assert step.description == "some description"
assert step.position == 42
assert step.title == "some title"
end
test "create_step/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Steps.create_step(@invalid_attrs)
end
test "update_step/2 with valid data updates the step" do
step = step_fixture()
update_attrs = %{description: "some updated description", position: 43, title: "some updated title"}
assert {:ok, %Step{} = step} = Steps.update_step(step, update_attrs)
assert step.description == "some updated description"
assert step.position == 43
assert step.title == "some updated title"
end
test "update_step/2 with invalid data returns error changeset" do
step = step_fixture()
assert {:error, %Ecto.Changeset{}} = Steps.update_step(step, @invalid_attrs)
assert step == Steps.get_step!(step.id)
end
test "delete_step/1 deletes the step" do
step = step_fixture()
assert {:ok, %Step{}} = Steps.delete_step(step)
assert_raise Ecto.NoResultsError, fn -> Steps.get_step!(step.id) end
end
test "change_step/1 returns a step changeset" do
step = step_fixture()
assert %Ecto.Changeset{} = Steps.change_step(step)
end
end
end

View File

@ -0,0 +1,22 @@
defmodule Memex.StepsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Memex.Steps` context.
"""
@doc """
Generate a step.
"""
def step_fixture(attrs \\ %{}) do
{:ok, step} =
attrs
|> Enum.into(%{
description: "some description",
position: 42,
title: "some title"
})
|> Memex.Steps.create_step()
step
end
end