Skip to content

Commit

Permalink
Merge pull request ufosc#155 from ufosc/feature/courses-to-users-join
Browse files Browse the repository at this point in the history
Add a many to many relationship between Users and Courses
  • Loading branch information
hjarrell authored Jul 15, 2019
2 parents 9a20ecc + 3cc549e commit 39d55ae
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 11,507 deletions.
19 changes: 19 additions & 0 deletions lib/ask_a_gator/accounts/user.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
defmodule AskAGator.Accounts.User do
use Ecto.Schema

import Ecto.Changeset

import Bcrypt, only: [hash_pwd_salt: 1]

alias AskAGator.Accounts.User
alias AskAGator.Accounts.UserCourse
alias AskAGator.Courses.Course
alias AskAGator.Repo

schema "users" do
field :email, :string
Expand All @@ -15,6 +19,8 @@ defmodule AskAGator.Accounts.User do
field(:token, :string)

timestamps()

many_to_many :courses, AskAGator.Courses.Course, join_through: UserCourse
end

@doc false
Expand All @@ -35,6 +41,19 @@ defmodule AskAGator.Accounts.User do
|> cast(attrs, [:token])
end

def add_course(%User{} = user, %Course{} = course) do
user = Repo.preload(user, :courses)

courses =
(user.courses ++ [course])
|> Enum.map(&Ecto.Changeset.change/1)

user
|> Ecto.Changeset.change
|> Ecto.Changeset.put_assoc(:courses, courses)
|> Repo.update
end

defp put_password_hash(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: pass}} ->
Expand Down
18 changes: 18 additions & 0 deletions lib/ask_a_gator/accounts/user_course.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule AskAGator.Accounts.UserCourse do
use Ecto.Schema
import Ecto.Changeset

@primary_key false
schema "user_course" do
belongs_to :user, AskAGator.Accounts.User
belongs_to :course, AskAGator.Courses.Course

timestamps()
end

def changeset(user_course, attrs) do
user_course
|> cast(attrs, [:user_id, :course_id])
|> validate_required([:user_id, :course_id])
end
end
17 changes: 12 additions & 5 deletions lib/ask_a_gator/courses/course.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule AskAGator.Courses.Course do
import Ecto.Changeset
import Puid

alias AskAGator.Accounts.UserCourse
alias AskAGator.Courses.Course

schema "courses" do
Expand All @@ -11,24 +12,30 @@ defmodule AskAGator.Courses.Course do
field :join_code, :string

timestamps()

many_to_many :users, AskAGator.Accounts.User, join_through: UserCourse
end

@doc false
def changeset(%Course{} = course, attrs) do
course
|> cast(attrs, [:name, :code])
|> cast(attrs, [:name, :code, :join_code])
|> validate_required([:name, :code])
|> unique_constraint(:join_code)
|> put_join_code
|> unique_constraint(:join_code)
end

defmodule(CourseUUID, do: use(Puid, charset: :alphanum_lower, total: 1.0e4, risk: 1.0e8))
defmodule(CourseUUID, do: use(Puid, charset: :alphanum_upper, total: 1.0e4, risk: 1.0e8))

defp put_join_code(changeset) do
case changeset do
%Ecto.Changeset{valid?: true} ->
put_change(changeset, :join_code, CourseUUID.generate())

case get_field(changeset, :join_code) do
nil ->
put_change(changeset, :join_code, CourseUUID.generate())
_ ->
changeset
end
_ ->
changeset
end
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"credo": {:hex, :credo, "1.1.0", "e0c07b2fd7e2109495f582430a1bc96b2c71b7d94c59dfad120529f65f19872f", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"crypto_rand": {:hex, :crypto_rand, "1.0.2", "e1381d3fbe743cfb6082150f4f95dc7ac3fcaf5b53dae0fc96f7e0f006d91dd1", [:mix], [], "hexpm"},
"db_connection": {:hex, :db_connection, "2.1.0", "122e2f62c4906bf2e49554f1e64db5030c19229aa40935f33088e7d543aa79d0", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [], [], "hexpm"},
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "3.1.7", "fa21d06ef56cdc2fdaa62574e8c3ba34a2751d44ea34c30bc65f0728421043e5", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_sql": {:hex, :ecto_sql, "3.1.6", "1e80e30d16138a729c717f73dcb938590bcdb3a4502f3012414d0cbb261045d8", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0 or ~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.5.2", "96a28c79f5b8d34879cd95ebc04d2a0d678cfbbd3e74c43cb63a76adf0ee8054", [:mix], [], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.6.0", "38349f3e29aff4864352084fc736fa7fa0f2995a819a737554f7ebd28b85aaab", [:mix], [], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.11.1", "dd677fbdd49114fdbdbf445540ec735808250d56b011077798316505064edb2c", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"},
"gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"},
Expand Down
Loading

0 comments on commit 39d55ae

Please sign in to comment.