Skip to content

Commit

Permalink
create follow schema for #13
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jun 8, 2024
1 parent c6ced68 commit ea2f7e2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/app/follow.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule App.Follow do
alias App.{Repo}
use Ecto.Schema
import Ecto.Changeset
require Logger
alias __MODULE__

schema "follows" do
field :follower_id, :integer
field :following_id, :integer
field :stop, :utc_datetime

timestamps()
end

@doc false
def changeset(follow, attrs) do
follow
|> cast(attrs, [:follower_id, :following_id, :stop])
|> validate_required([:follower_id, :following_id])
end

@doc """
Creates a `follow` record.
"""
def create(attrs) do
%Follow{}
|> changeset(attrs)
|> Repo.insert()
end
end
13 changes: 13 additions & 0 deletions priv/repo/migrations/20240608144843_create_follows.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule App.Repo.Migrations.CreateFollows do
use Ecto.Migration

def change do
create table(:follows) do
add :follower_id, :integer
add :following_id, :integer
add :stop, :utc_datetime

timestamps()
end
end
end
12 changes: 12 additions & 0 deletions test/app/follow_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule App.FollowTest do
use App.DataCase

test "App.Follow.create/1" do
follow = %{
follower_id: 42,
following_id: 73
}
assert {:ok, inserted_follow} = App.Follow.create(follow)
assert inserted_follow.follower_id == follow.follower_id
end
end

0 comments on commit ea2f7e2

Please sign in to comment.