Skip to content

Commit

Permalink
create Stars schema #13
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jun 8, 2024
1 parent 065e3f2 commit c6ced68
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
9 changes: 5 additions & 4 deletions BUILDIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ This app stores data in **five** schemas:
2. `orgs` - [https://docs.github.com/en/rest/orgs/orgs](https://docs.github.com/en/rest/orgs/orgs?#get-an-organization) - organizations which can have `users` as members and `repositories`.
3. `repositories` - https://docs.github.com/en/rest/repos/repos - the repositories of code on GitHub.
4. `stars` - [https://docs.github.com/en/rest/activity/starring](https://docs.github.com/en/rest/activity/starring?apiVersion=2022-11-28#list-stargazers) - the `stars` (on `repositories`) associated with each `user`.
5. `follows` - https://docs.github.com/en/rest/users/followers - List the `people` a `user` follows
5. `follows` - https://docs.github.com/en/rest/users/followers - List the `people` a `user` follows.

For each of these schemas we are storing
a _subset_ of the data;
Expand All @@ -451,7 +451,8 @@ commands:
mix phx.gen.schema User users login:string avatar_url:string name:string company:string bio:string blog:string location:string email:string created_at:string two_factor_authentication:boolean followers:integer following:integer
mix phx.gen.schema Org orgs login:string avatar_url:string name:string company:string public_repos:integer location:string description:string followers:integer
mix phx.gen.schema Repository repositories name:string full_name:string owner_id:integer owner_name:string description:string fork:boolean forks_count:integer watchers_count:integer stargazers_count:integer topics:string open_issues_count:integer created_at:string pushed_at:string
mix phx.gen.schema Stars stars repo_id:integer
mix phx.gen.schema Stars stars repo_id:integer user_id:integer stop:utc_datetime
mix phx.gen.schema Follows follows follower_id:integer following_id:integer stop:utc_datetime
```

At the end of this step,
Expand All @@ -462,8 +463,8 @@ we have the following database

![erd](https://user-images.githubusercontent.com/194400/194425189-e44d6161-c8df-4a0d-9d86-bc1045785c95.png)

We created **2 database tables**;
`users` and `repositories`.
We created **5 database tables**;
`users`, `orgs`, `follows`, `repositories` and `stars`.
At present the two tables are unrelated
but eventually `repository.owner_id` will refer to `user.id`
and we will be creating other schemas below.
Expand Down
31 changes: 31 additions & 0 deletions lib/app/star.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule App.Star do
alias App.{Repo}
use Ecto.Schema
import Ecto.Changeset
require Logger
alias __MODULE__

schema "stars" do
field :stop, :utc_datetime
field :repo_id, :integer
field :user_id, :integer

timestamps()
end

@doc false
def changeset(stars, attrs) do
stars
|> cast(attrs, [:repo_id, :user_id, :stop])
|> validate_required([:repo_id, :user_id])
end

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

def change do
create table(:stars) do
add :repo_id, :integer
add :user_id, :integer
add :stop, :utc_datetime, default: nil

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

test "App.Star.create/1" do
star = %{
repo_id: 42,
user_id: 73
}
assert {:ok, inserted_star} = App.Star.create(star)
assert inserted_star.repo_id == star.repo_id
end
end

0 comments on commit c6ced68

Please sign in to comment.