Skip to content

Commit

Permalink
adds insert function to append only, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Danwhy committed Sep 12, 2018
1 parent b2f29b1 commit d060bcb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/append/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ defmodule Append.Address do
use Ecto.Schema
import Ecto.Changeset


schema "addresses" do
field :address_line_1, :string
field :address_line_2, :string
field :city, :string
field :name, :string
field :postcode, :string
field :tel, :string
field(:address_line_1, :string)
field(:address_line_2, :string)
field(:city, :string)
field(:name, :string)
field(:postcode, :string)
field(:tel, :string)

timestamps()
end

# Because we are referencing the Address struct in our Append Only behaviour/macro
# it needs to be used after we have defined the struct using 'schema'
use Append.AppendOnlyLog

@doc false
def changeset(address, attrs) do
address
Expand Down
6 changes: 6 additions & 0 deletions lib/append/append_only_log.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Append.AppendOnlyLog do
alias Append.Repo

@moduledoc """
Behaviour that defines functions for accessing and inserting data in an
Append-Only database
Expand All @@ -14,6 +16,9 @@ defmodule Append.AppendOnlyLog do
@behaviour Append.AppendOnlyLog

def insert(attrs) do
%__MODULE__{}
|> __MODULE__.changeset(attrs)
|> Repo.insert()
end

def get(id) do
Expand All @@ -22,6 +27,7 @@ defmodule Append.AppendOnlyLog do
def update(%__MODULE__{} = item, attrs) do
end

defoverridable Append.AppendOnlyLog
end
end
end
17 changes: 17 additions & 0 deletions test/append/address_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Append.AddressTest do
use Append.DataCase
alias Append.Address

test "add item to database" do
assert {:ok, item} = Address.insert(%{
name: "Thor",
address_line_1: "The Hall",
address_line_2: "Valhalla",
city: "Asgard",
postcode: "AS1 3DG",
tel: "0800123123"
})

assert item.name == "Thor"
end
end

0 comments on commit d060bcb

Please sign in to comment.