Skip to content

Commit

Permalink
Merge pull request #3 from inaka/cabol.109538988.users_model
Browse files Browse the repository at this point in the history
Added users and repositories models.
  • Loading branch information
igaray committed Dec 28, 2015
2 parents 148bbaf + 0c016dc commit 32cdbae
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
19 changes: 19 additions & 0 deletions priv/repo/migrations/20151228162209_create_user.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule CredoServer.Repo.Migrations.CreateUser do
use Ecto.Migration

def change do
create table(:users) do
add :name, :string
add :username, :string
add :github_token, :string
add :email, :string
add :email_code, :string
add :auth_token, :string
add :auth_expires, :datetime
add :synced_at, :datetime

timestamps
end

end
end
18 changes: 18 additions & 0 deletions priv/repo/migrations/20151228163106_create_repository.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule CredoServer.Repo.Migrations.CreateRepo do
use Ecto.Migration

def change do
create table(:repositories) do
add :user_id, references(:users)
add :github_id, :integer
add :name, :string
add :full_name, :string
add :html_url, :string
add :private, :boolean, default: false
add :status, :string

timestamps
end

end
end
18 changes: 18 additions & 0 deletions test/models/repository_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule CredoServer.RepoTest do
use CredoServer.ModelCase

alias CredoServer.Repository

@valid_attrs %{full_name: "some content", github_id: 42, html_url: "some content", name: "some content", private: true, status: "some content"}
@invalid_attrs %{}

test "changeset with valid attributes" do
changeset = Repository.changeset(%Repository{}, @valid_attrs)
assert changeset.valid?
end

test "changeset with invalid attributes" do
changeset = Repository.changeset(%Repository{}, @invalid_attrs)
refute changeset.valid?
end
end
18 changes: 18 additions & 0 deletions test/models/user_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule CredoServer.UserTest do
use CredoServer.ModelCase

alias CredoServer.User

@valid_attrs %{auth_expires: "2010-04-17 14:00:00", auth_token: "some content", email: "some content", email_code: "some content", github_token: "some content", name: "some content", synced_at: "2010-04-17 14:00:00", username: "some content"}
@invalid_attrs %{}

test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end

test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end
end
29 changes: 29 additions & 0 deletions web/models/repository.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule CredoServer.Repository do
use CredoServer.Web, :model

schema "repositories" do
belongs_to :user_id, CredoServer.User
field :github_id, :integer
field :name, :string
field :full_name, :string
field :html_url, :string
field :private, :boolean, default: false
field :status, :string

timestamps
end

@required_fields ~w(github_id name full_name html_url private status)
@optional_fields ~w()

@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
30 changes: 30 additions & 0 deletions web/models/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule CredoServer.User do
use CredoServer.Web, :model

schema "users" do
field :name, :string
field :username, :string
field :github_token, :string
field :email, :string
field :email_code, :string
field :auth_token, :string
field :auth_expires, Ecto.DateTime
field :synced_at, Ecto.DateTime

timestamps
end

@required_fields ~w(name username github_token email email_code auth_token auth_expires synced_at)
@optional_fields ~w()

@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end

0 comments on commit 32cdbae

Please sign in to comment.