From e87b6da30953dbbf2c479912826b3b39428eef25 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 27 Dec 2024 11:00:34 -0600 Subject: [PATCH] WIP --- main.ex | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ mix.exs | 1 + mix.lock | 2 ++ 3 files changed, 52 insertions(+) diff --git a/main.ex b/main.ex index 8c4b0e5..fd53a7a 100644 --- a/main.ex +++ b/main.ex @@ -187,3 +187,52 @@ defmodule Sfe.HomeLive do {:noreply, assign(socket, :count, counter_value())} end end + +defmodule UserGroup do + use Ecto.Schema + schema "users_groups" do + field(:user_id) + field(:group_id) + timestamps() + end +end + +defmodule User do + use Ecto.Schema + import Ecto.Changeset + + schema "users" do + field(:email) + field(:avatar) + many_to_many(:groups, Group, join_through: UserGroup) + timestamps() + end + + def create(struct \\ %__MODULE__{}, params) do + struct + |> cast(params, @required) + |> validate_required([:avatar, :email, :name]) + |> validate_format(:avatar, :url) + |> validate_url_is_an_image(:avatar) + |> validate_format(:email, ~r/@/) + |> validate_length(:email, {:gt, 3}) # I don't know how to use this off the top of my head + |> validate_length(:name, 2) + # and more validations for name and email and whatever + end + + def create_from_referral(struct \\ %__MODULE__{}, params) do + struct + |> create() + |> cast(params, [:referrer]) + |> validate_referrer(:referrer) + end +end + +defmodule Group do + use Ecto.Schema + schema "groups" do + field(:name) + many_to_many(:users, User, join_through: UserGroup) + timestamps() + end +end diff --git a/mix.exs b/mix.exs index 42bc0e1..8e4f331 100644 --- a/mix.exs +++ b/mix.exs @@ -23,6 +23,7 @@ defmodule Sfe.MixProject do defp deps(), do: [ # {:credo, "~> 1.6", only: [:dev, :test], runtime: false}, + {:ecto, "~> 3.12"}, {:bandit, "~> 1.2"}, {:jason, "~> 1.0"}, {:phoenix, "~> 1.7"}, diff --git a/mix.lock b/mix.lock index 7794b84..6b8d0a0 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,8 @@ %{ "bandit": {:hex, :bandit, "1.4.1", "6ff703b33a967bc20b41ed3840a4c58e62abe62b4cc598cff7429af78e174990", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "5bbc0a4c185358b7d566a3b7b32806723ae139a8704cdc841ad787b677adcb9a"}, "castore": {:hex, :castore, "1.0.6", "ffc42f110ebfdafab0ea159cd43d31365fa0af0ce4a02ecebf1707ae619ee727", [:mix], [], "hexpm", "374c6e7ca752296be3d6780a6d5b922854ffcc74123da90f2f328996b962d33a"}, + "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, + "ecto": {:hex, :ecto, "3.12.4", "267c94d9f2969e6acc4dd5e3e3af5b05cdae89a4d549925f3008b2b7eb0b93c3", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ef04e4101688a67d061e1b10d7bc1fbf00d1d13c17eef08b71d070ff9188f747"}, "file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"}, "hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},