-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
68fb755
commit 95e3704
Showing
34 changed files
with
906 additions
and
121 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule Erlef.AcademicPapers do | ||
@moduledoc """ | ||
Context responsible for managing Academic Papers | ||
""" | ||
|
||
alias Erlef.AcademicPapers.Query | ||
alias Erlef.AcademicPapers.AcademicPaper | ||
|
||
@spec all() :: [AcademicPaper.t()] | ||
def all(), do: Query.published() | ||
|
||
@spec get(Ecto.UUID.t()) :: AcademicPaper.t() | ||
def get(id), do: Query.get(id) | ||
|
||
@spec create_academic_paper(params :: map()) :: | ||
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()} | ||
def create_academic_paper(params) do | ||
new_params = normalize_params(params) | ||
|
||
%AcademicPaper{} | ||
|> change_academic_paper(new_params) | ||
|> Query.create() | ||
end | ||
|
||
@spec update_academic_paper(academic_paper :: AcademicPaper.t(), params :: map()) :: | ||
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()} | ||
def update_academic_paper(%AcademicPaper{} = academic_paper, params) do | ||
new_params = normalize_params(params) | ||
|
||
academic_paper | ||
|> change_academic_paper(new_params) | ||
|> Query.update() | ||
end | ||
|
||
@spec delete_academic_paper(academic_paper :: AcademicPaper.t()) :: | ||
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()} | ||
def delete_academic_paper(%AcademicPaper{} = academic_paper) do | ||
academic_paper | ||
|> change_academic_paper(%{deleted_at: DateTime.utc_now()}) | ||
|> Query.update() | ||
end | ||
|
||
@spec change_academic_paper(academic_paper :: Ecto.Schema.t(), params :: map()) :: | ||
Ecto.Changeset.t() | ||
def change_academic_paper(%AcademicPaper{} = academic_paper, attrs \\ %{}), | ||
do: AcademicPaper.changeset(academic_paper, attrs) | ||
|
||
@spec unapproved_all() :: [AcademicPaper.t()] | ||
def unapproved_all(), do: Query.unpublished() | ||
|
||
@spec approve_academic_paper(academic_paper :: AcademicPaper.t(), params :: map()) :: | ||
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()} | ||
def approve_academic_paper(%AcademicPaper{} = unapproved_academic_paper, params) do | ||
params0 = normalize_params(params) | ||
new_params = Map.put(params0, "published_at", DateTime.utc_now()) | ||
|
||
unapproved_academic_paper | ||
|> change_academic_paper(new_params) | ||
|> Query.update() | ||
end | ||
|
||
# Private Internal Functions | ||
|
||
defp normalize_params(%{"technologies" => _val} = params), do: params | ||
|
||
defp normalize_params(params), do: Map.put(params, "technologies", "") | ||
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,39 @@ | ||
defmodule ErlefWeb.AcademicPaperController do | ||
use ErlefWeb, :controller | ||
|
||
alias Erlef.Publications | ||
alias Erlef.AcademicPapers | ||
alias Erlef.AcademicPapers.AcademicPaper | ||
alias Erlef.{Accounts, Admins, Members} | ||
|
||
action_fallback ErlefWeb.FallbackController | ||
|
||
def index(conn, _params) do | ||
render(conn, :index, academic_papers: Publications.all()) | ||
render(conn, :index, academic_papers: AcademicPapers.all()) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = AcademicPapers.change_academic_paper(%AcademicPaper{}) | ||
render(conn, :new, changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"academic_paper" => params}) do | ||
submitter_id = conn.assigns.current_user.id | ||
new_params = Map.put(params, "submitted_by", submitter_id) | ||
|
||
case AcademicPapers.create_academic_paper(new_params) do | ||
{:ok, _academic_paper} -> | ||
type = :new_academic_paper_submitted | ||
submitter = Accounts.get_member(submitter_id) | ||
opts = %{member: submitter} | ||
{:ok, _notify} = Admins.notify(type, opts) | ||
{:ok, _notify} = Members.notify(type, opts) | ||
|
||
conn | ||
|> put_flash(:info, "Paper Submitted and has to be Approved") | ||
|> redirect(to: "/") | ||
|
||
{:error, changeset} -> | ||
render(conn, :new, changeset: changeset) | ||
end | ||
end | ||
end |
Oops, something went wrong.