This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.TaskWorker do | |
use GenServer, restart: :transient | |
alias MyApp.Notify | |
alias MyApp.Management | |
alias MyApp.WorkerStateStorage | |
# client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.TaskSupervisor do | |
use DynamicSupervisor | |
alias MyApp.Management | |
alias MyApp.WorkerStateStorage | |
def start_link(arg) do | |
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upsert_user_projects(user, project_ids) when is_list(project_ids) do | |
projects = | |
Project | |
|> where([project], project.id in ^project_ids) | |
|> Repo.all() | |
with {:ok, _struct} <- | |
user | |
|> User.changeset_update_projects(projects) | |
|> Repo.update() do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def MyApp.Accounts.User do | |
... | |
def changeset_update_projects(%User{} = user, projects) do | |
user | |
|> cast(%{}, @required_fields) | |
# associate projects to the user | |
|> put_assoc(:projects, projects) | |
end | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
def MyApp.Management.Project do | |
... | |
many_to_many( | |
:users, | |
User, | |
join_through: "user_project", | |
on_replace: :delete | |
) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Management.UserProject do | |
@moduledoc """ | |
UserProject module | |
""" | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias MyApp.Accounts.User | |
alias MyApp.Management.Project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upsert_user_projects(user_id, project_ids) when is_list(project_ids) do | |
{:ok, time} = Ecto.Type.cast(:naive_datetime, Timex.now()) | |
project_users = | |
project_ids | |
|> Enum.uniq() | |
|> Enum.map(fn project_id -> | |
%{ | |
user_id: user_id, | |
project_id: project_id, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Management.UserProject do | |
@moduledoc """ | |
UserProject module | |
""" | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias MyApp.Accounts.User | |
alias MyApp.Management.Project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Repo.Migrations.AddTableUserProject do | |
use Ecto.Migration | |
def change do | |
create table(:user_project, primary_key: false) do | |
add(:project_id, references(:project, on_delete: :delete_all), primary_key: true) | |
add(:user_id, references(:user, on_delete: :delete_all), primary_key: true) | |
timestamps() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream phoenix { | |
server 127.0.0.1:4000 max_fails=5 fail_timeout=60s; | |
} | |
server { | |
root /var/www/html; | |
# Add index.php to the list if you are using PHP | |
index index.html index.htm index.nginx-debian.html; | |
server_name <server_name>; | |
location / { | |
# First attempt to serve request as file, then |