philomena/lib/philomena/badges.ex

230 lines
4 KiB
Elixir
Raw Normal View History

2019-08-28 18:38:53 +02:00
defmodule Philomena.Badges do
@moduledoc """
The Badges context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.Badges.Badge
2019-12-15 21:02:13 +01:00
alias Philomena.Badges.Uploader
2019-08-28 18:38:53 +02:00
@doc """
Returns the list of badges.
## Examples
iex> list_badges()
[%Badge{}, ...]
"""
def list_badges do
Repo.all(Badge)
end
@doc """
Gets a single badge.
Raises `Ecto.NoResultsError` if the Badge does not exist.
## Examples
iex> get_badge!(123)
%Badge{}
iex> get_badge!(456)
** (Ecto.NoResultsError)
"""
def get_badge!(id), do: Repo.get!(Badge, id)
@doc """
Creates a badge.
## Examples
iex> create_badge(%{field: value})
{:ok, %Badge{}}
iex> create_badge(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_badge(attrs \\ %{}) do
Squashed commit of the following: commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] <byteslice@airmail.cc> Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable
2020-09-06 07:30:53 +02:00
%Badge{}
|> Badge.changeset(attrs)
|> Uploader.analyze_upload(attrs)
|> Repo.insert()
|> case do
{:ok, badge} ->
Uploader.persist_upload(badge)
Uploader.unpersist_old_upload(badge)
{:ok, badge}
error ->
error
end
2019-08-28 18:38:53 +02:00
end
@doc """
Updates a badge.
## Examples
iex> update_badge(badge, %{field: new_value})
{:ok, %Badge{}}
iex> update_badge(badge, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_badge(%Badge{} = badge, attrs) do
2020-03-30 00:36:24 +02:00
badge
|> Badge.changeset(attrs)
|> Repo.update()
end
def update_badge_image(%Badge{} = badge, attrs) do
Squashed commit of the following: commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] <byteslice@airmail.cc> Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable
2020-09-06 07:30:53 +02:00
badge
|> Badge.changeset(attrs)
|> Uploader.analyze_upload(attrs)
|> Repo.update()
|> case do
{:ok, badge} ->
Uploader.persist_upload(badge)
Uploader.unpersist_old_upload(badge)
{:ok, badge}
error ->
error
end
2019-08-28 18:38:53 +02:00
end
@doc """
Deletes a Badge.
## Examples
iex> delete_badge(badge)
{:ok, %Badge{}}
iex> delete_badge(badge)
{:error, %Ecto.Changeset{}}
"""
def delete_badge(%Badge{} = badge) do
Repo.delete(badge)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking badge changes.
## Examples
iex> change_badge(badge)
%Ecto.Changeset{source: %Badge{}}
"""
def change_badge(%Badge{} = badge) do
Badge.changeset(badge, %{})
end
alias Philomena.Badges.Award
@doc """
Returns the list of badge_awards.
## Examples
iex> list_badge_awards()
[%Award{}, ...]
"""
def list_badge_awards do
Repo.all(Award)
end
@doc """
Gets a single badge_award.
Raises `Ecto.NoResultsError` if the Badge award does not exist.
## Examples
iex> get_badge_award!(123)
%Award{}
iex> get_badge_award!(456)
** (Ecto.NoResultsError)
"""
def get_badge_award!(id), do: Repo.get!(Award, id)
@doc """
Creates a badge_award.
## Examples
iex> create_badge_award(%{field: value})
{:ok, %Award{}}
iex> create_badge_award(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
2019-12-15 21:02:13 +01:00
def create_badge_award(creator, user, attrs \\ %{}) do
%Award{awarded_by_id: creator.id, user_id: user.id}
2019-08-28 18:38:53 +02:00
|> Award.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a badge_award.
## Examples
iex> update_badge_award(badge_award, %{field: new_value})
{:ok, %Award{}}
iex> update_badge_award(badge_award, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_badge_award(%Award{} = badge_award, attrs) do
badge_award
|> Award.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Award.
## Examples
iex> delete_badge_award(badge_award)
{:ok, %Award{}}
iex> delete_badge_award(badge_award)
{:error, %Ecto.Changeset{}}
"""
def delete_badge_award(%Award{} = badge_award) do
Repo.delete(badge_award)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking badge_award changes.
## Examples
iex> change_badge_award(badge_award)
%Ecto.Changeset{source: %Award{}}
"""
def change_badge_award(%Award{} = badge_award) do
Award.changeset(badge_award, %{})
end
end