Skip to content

Commit

Permalink
Merge pull request #1 from Guo-William/tasks2
Browse files Browse the repository at this point in the history
Tasks2 merge for deploy
  • Loading branch information
Guo-William authored Mar 1, 2018
2 parents 56acd86 + 0f59eeb commit f7e97e4
Show file tree
Hide file tree
Showing 33 changed files with 1,128 additions and 97 deletions.
82 changes: 81 additions & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,90 @@
// If you no longer want to use a dependency, remember
// to also remove its path from "config.paths.watched".
import "phoenix_html"

import $ from "jquery";
// Import local files
//
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".

// import socket from "./socket"

// Boiler plate code from https://github.com/NatTuck/microblog
// heavily modified to fit my own needs
function update_buttons() {
$('.manage-button').each((_, bb) => {
let managee_id = $(bb).data('managee-id');
let manage = $(bb).data('manages-id');
if (manage != "") {
$(bb).text("Un-manage");
}
else {
$(bb).text("Add Managee");
}
});
}

function set_button(managee_id, value) {
$('.manage-button').each((_, bb) => {
if (managee_id == $(bb).data('managee-id')) {
$(bb).data('manages-id', value);
}
});
update_buttons();
}

function manage(managee_id) {
let text = JSON.stringify({
manage: {
manager_id: current_user_id,
managee_id: managee_id
},
});

$.ajax(manage_path, {
method: "post",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: text,
success: (resp) => {
set_button(managee_id, resp.data.id);
},
});
}

function unmanage(managee_id, manages_id) {
$.ajax(manage_path + "/" + manages_id, {
method: "delete",
dataType: "json",
contentType: "application/json; charset=UTF-8",
data: "",
success: () => {
set_button(managee_id, "");
},
});
}

function manage_click(ev) {
let btn = $(ev.target);
let manages_id = btn.data('manages-id');
let managee_id = btn.data('managee-id');

if (manages_id != "") {
unmanage(managee_id, manages_id);
}
else {
manage(managee_id);
}
}

function init_manage() {
if (!$('.manage-button')) {
return;
}

$(".manage-button").click(manage_click);

update_buttons();
}

$(init_manage);
129 changes: 127 additions & 2 deletions lib/tasktracker/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ defmodule Tasktracker.Accounts do
alias Tasktracker.Accounts.User

# below two functions obtained from https://github.com/NatTuck/microblog
def get_user(id), do: Repo.get(User, id)
def get_user(id) do
Repo.get(User, id)
|> Repo.preload(:managee_manages)
|> Repo.preload(:managees)
|> Repo.preload(:manager_manages)
|> Repo.preload(:managers)
end

# And we want by-email lookup
def get_user_by_email(e) do
Expand All @@ -27,6 +33,10 @@ defmodule Tasktracker.Accounts do
"""
def list_users do
Repo.all(User)
|> Repo.preload(:managee_manages)
|> Repo.preload(:managees)
|> Repo.preload(:manager_manages)
|> Repo.preload(:managers)
end

@doc """
Expand All @@ -43,7 +53,13 @@ defmodule Tasktracker.Accounts do
** (Ecto.NoResultsError)
"""
def get_user!(id), do: Repo.get!(User, id)
def get_user!(id) do
Repo.get!(User, id)
|> Repo.preload(:managee_manages)
|> Repo.preload(:managees)
|> Repo.preload(:manager_manages)
|> Repo.preload(:managers)
end

@doc """
Creates a user.
Expand Down Expand Up @@ -109,4 +125,113 @@ defmodule Tasktracker.Accounts do
def change_user(%User{} = user) do
User.changeset(user, %{})
end

alias Tasktracker.Accounts.Manage

@doc """
Returns the list of manages.
## Examples
iex> list_manages()
[%Manage{}, ...]
"""
def list_manages do
Repo.all(Manage)
end

@doc """
Gets a single manage.
Raises `Ecto.NoResultsError` if the Manage does not exist.
## Examples
iex> get_manage!(123)
%Manage{}
iex> get_manage!(456)
** (Ecto.NoResultsError)
"""
def get_manage!(id), do: Repo.get!(Manage, id)

@doc """
Creates a manage.
## Examples
iex> create_manage(%{field: value})
{:ok, %Manage{}}
iex> create_manage(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_manage(attrs \\ %{}) do
%Manage{}
|> Manage.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a manage.
## Examples
iex> update_manage(manage, %{field: new_value})
{:ok, %Manage{}}
iex> update_manage(manage, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_manage(%Manage{} = manage, attrs) do
manage
|> Manage.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a Manage.
## Examples
iex> delete_manage(manage)
{:ok, %Manage{}}
iex> delete_manage(manage)
{:error, %Ecto.Changeset{}}
"""
def delete_manage(%Manage{} = manage) do
Repo.delete(manage)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking manage changes.
## Examples
iex> change_manage(manage)
%Ecto.Changeset{source: %Manage{}}
"""
def change_manage(%Manage{} = manage) do
Manage.changeset(manage, %{})
end

# boiler plate code from https://github.com/NatTuck/microblog
def manages_map_for(user_id) do
Repo.all(from(f in Manage, where: f.manager_id == ^user_id))
|> Enum.map(&{&1.managee_id, &1.id})
|> Enum.into(%{})
end

def get_manager_id_for(user_id) do
Repo.all(from(f in Manage, where: f.managee_id == ^user_id))
|> Enum.map(& &1.manager_id)
|> Enum.at(0)
end
end
20 changes: 20 additions & 0 deletions lib/tasktracker/accounts/manage.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Tasktracker.Accounts.Manage do
use Ecto.Schema
import Ecto.Changeset
alias Tasktracker.Accounts.Manage
alias Tasktracker.Accounts.User

schema "manages" do
belongs_to(:manager, User)
belongs_to(:managee, User)

timestamps()
end

@doc false
def changeset(%Manage{} = manage, attrs) do
manage
|> cast(attrs, [:manager_id, :managee_id])
|> validate_required([:manager_id, :managee_id])
end
end
18 changes: 16 additions & 2 deletions lib/tasktracker/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@ defmodule Tasktracker.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
alias Tasktracker.Accounts.User
alias Tasktracker.Accounts.Manage

schema "users" do
field(:email, :string)
field(:username, :string)
field(:is_manager, :boolean)

# boiler code from https://github.com/NatTuck/microblog
# changed to fit my needs

# all Manage records where I am the manager
has_many(:manager_manages, Manage, foreign_key: :manager_id)
# all Manage records where I am the managed
has_one(:managee_manages, Manage, foreign_key: :managee_id)
# User who manages me
has_one(:managers, through: [:managee_manages, :manager])
# all Users I manage
has_many(:managees, through: [:manager_manages, :managee])

timestamps()
end

@doc false
def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:email, :username])
|> validate_required([:email, :username])
|> cast(attrs, [:email, :username, :is_manager])
|> validate_required([:email, :username, :is_manager])
|> validate_format(:email, ~r/@/)
|> unique_constraint(:email)
|> unique_constraint(:username)
Expand Down
Loading

0 comments on commit f7e97e4

Please sign in to comment.