Skip to content

Instantly share code, notes, and snippets.

defmodule MyApp.TaskWorker do
use GenServer, restart: :transient
alias MyApp.Notify
alias MyApp.Management
alias MyApp.WorkerStateStorage
# client
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
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
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
...
def MyApp.Management.Project do
...
many_to_many(
:users,
User,
join_through: "user_project",
on_replace: :delete
)
...
defmodule MyApp.Management.UserProject do
@moduledoc """
UserProject module
"""
use Ecto.Schema
import Ecto.Changeset
alias MyApp.Accounts.User
alias MyApp.Management.Project
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,
defmodule MyApp.Management.UserProject do
@moduledoc """
UserProject module
"""
use Ecto.Schema
import Ecto.Changeset
alias MyApp.Accounts.User
alias MyApp.Management.Project
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
@marinho10
marinho10 / default
Last active November 6, 2018 20:41
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