From c6ced68dce4a4cbb590508370bc40268842c8010 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sat, 8 Jun 2024 15:44:23 +0100 Subject: [PATCH] create Stars schema #13 --- BUILDIT.md | 9 +++--- lib/app/star.ex | 31 +++++++++++++++++++ .../20240608143836_create_stars.exs | 13 ++++++++ test/app/star_test.exs | 12 +++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 lib/app/star.ex create mode 100644 priv/repo/migrations/20240608143836_create_stars.exs create mode 100644 test/app/star_test.exs diff --git a/BUILDIT.md b/BUILDIT.md index cbd5af6..9607c40 100644 --- a/BUILDIT.md +++ b/BUILDIT.md @@ -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; @@ -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, @@ -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. diff --git a/lib/app/star.ex b/lib/app/star.ex new file mode 100644 index 0000000..9afa76b --- /dev/null +++ b/lib/app/star.ex @@ -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 diff --git a/priv/repo/migrations/20240608143836_create_stars.exs b/priv/repo/migrations/20240608143836_create_stars.exs new file mode 100644 index 0000000..8e7eb37 --- /dev/null +++ b/priv/repo/migrations/20240608143836_create_stars.exs @@ -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 diff --git a/test/app/star_test.exs b/test/app/star_test.exs new file mode 100644 index 0000000..362e1dd --- /dev/null +++ b/test/app/star_test.exs @@ -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