-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
absorb database into common #40
base: master
Are you sure you want to change the base?
Changes from all commits
5185b31
b64e278
8890cdb
a4f5f4a
01fa5c0
4c0a9f5
f764f7a
0ee22e4
b7c9a05
c81a7f0
f999477
19398f2
4fd69f6
f7d1b67
ff67381
e656945
10017a8
345a264
f3b20fc
08b1cdd
2baead0
eb24f99
99593fd
9e58f60
d48c14c
b4aa58f
042ff05
6e38992
c744f1a
bda033f
179ab46
255db6d
e70915b
a07bab5
d0450e7
a6b41d8
7365dbe
ae8a30a
fdb778d
d58aa68
b431ad1
1d5bcde
95769c2
f53924a
603e9e4
539f015
1729ed6
dd17fe5
da7e26c
4a03729
a6c380d
852a781
f0703f2
abf0a10
7c8171e
c11df67
fbe89e2
a148518
a4942c5
81e20f5
b6c6fa9
1d166f5
6b037ca
a96c322
07ef3b4
460ac62
64a8b3e
a6ee1b4
ac56546
5fa0db4
d5cfd1e
978b289
fe8ed1f
bee5268
75c13b1
ed7cf1f
90cd540
74940cc
f0e77bf
3a8ce50
162c28a
e73b9a5
2174a4f
599a875
a34b289
9baf15a
93c450d
2912db1
f5ea5bf
5eff11a
a21015e
1ed8a6a
ad31656
6337c7f
cdfce4d
5e64d11
5733dd5
a89a05a
40449dc
f051d94
eb8ea50
2ce6547
e20d4d4
255c04b
bc7c80e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
defmodule Common.ModDB do | ||
@moduledoc """ | ||
The Mods context. | ||
""" | ||
|
||
require Logger | ||
import Common.Utils, only: [debug: 1] | ||
|
||
alias Common.Repo | ||
alias Common.Schema.Mod | ||
# alias Common.Schema.ModFile | ||
alias Common.Schema.ModTag | ||
alias Common.Schema.Modlist | ||
alias Common.Utils | ||
|
||
alias Ecto.Changeset | ||
|
||
### ====== TAGS ====== | ||
|
||
@doc """ | ||
Returns the list of _all_ tags. | ||
""" | ||
@spec list_tags() :: [ModTag.t()] | ||
def list_tags, do: Repo.all(ModTag) | ||
|
||
@doc """ | ||
Gets a single tag. | ||
|
||
Raises `Ecto.NoResultsError` if the ModTag does not exist. | ||
""" | ||
@spec get_tag!(id :: ModTag.id()) :: ModTag.t() | ||
def get_tag!(id), do: Repo.get!(ModTag, id) | ||
|
||
@doc """ | ||
Creates a tag. | ||
""" | ||
@spec create_tag(change :: Utils.unclean_change()) :: | ||
{:ok, ModTag.t()} | {:error, Changeset.t(Mod.t())} | ||
def create_tag(change \\ %{}) do | ||
clean_ch = ModTag.clean_changes(change) | ||
|
||
%ModTag{} | ||
|> ModTag.changeset(clean_ch) | ||
|> Repo.insert() | ||
end | ||
|
||
### ====== MODS ====== | ||
|
||
@doc """ | ||
Returns the list of _all_ mods. | ||
""" | ||
@spec list_mods() :: [Mod.t()] | ||
def list_mods, do: Repo.all(Mod) | ||
# TODO page this stuff, Repo.stream/2 seems useful | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found a TODO tag in a comment: # TODO page this stuff, Repo.stream/2 seems useful |
||
|
||
@doc """ | ||
Creates a mod. | ||
""" | ||
@spec create_mod(change :: Utils.change()) :: {:ok, Mod.t()} | {:error, Changeset.t(Mod.t())} | ||
def create_mod(change \\ %{}) do | ||
cs = | ||
change | ||
|> Mod.clean_changes() | ||
|> Mod.new() | ||
|
||
if cs.valid? do | ||
Repo.insert(cs) | ||
else | ||
{:error, cs} | ||
end | ||
end | ||
|
||
@doc """ | ||
Gets a single mod. The tags are not loaded, to access `mod.tags` do `Repo.preload(mod, [:tags])` before try to access them. | ||
|
||
Raises `Ecto.NoResultsError` if the ModTag does not exist. | ||
""" | ||
@spec get_mod!(id :: Mod.id()) :: Mod.t() | ||
def get_mod!(id), do: Repo.get!(Mod, id) | ||
|
||
@doc """ | ||
Updates a mod. | ||
""" | ||
@spec update_mod(mod :: Mod.t(), change :: Utils.unclean_change()) :: | ||
{:ok, Mod.t()} | {:error, Changeset.t(Mod.t())} | ||
def update_mod(%Mod{} = mod, change) do | ||
clean_change = Mod.clean_changes(change) | ||
|
||
mod | ||
|> Mod.changeset(clean_change) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a mod. | ||
""" | ||
@spec delete_mod(Mod.t()) :: {:ok, Mod.t()} | {:error, Changeset.t(Mod.t())} | ||
def delete_mod(%Mod{} = mod), do: Repo.delete(mod) | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking mod changes. | ||
""" | ||
@spec change_mod(Mod.t()) :: Changeset.t(Mod.t()) | ||
def change_mod(%Mod{} = mod), do: Mod.changeset(mod, %{}) | ||
|
||
### ====== MOD LISTS ====== | ||
|
||
@doc """ | ||
Returns the list of _all_ modlists. | ||
""" | ||
@spec list_modlists() :: [Modlist.t()] | ||
def list_modlists(), do: Repo.all(Modlist) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use parentheses when defining a function which has no arguments. |
||
# TODO needs paging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found a TODO tag in a comment: # TODO needs paging |
||
|
||
@spec get_modlist!(id :: Modlist.id()) :: Modlist.t() | ||
def get_modlist!(id), do: Repo.get!(Modlist, id) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no more than 1 consecutive blank lines. |
||
@doc """ | ||
Return the list of _all_ modlist. | ||
""" | ||
@spec add_mod_to_list(list :: Modlist.t(), mod :: Mod.t()) :: | ||
{:ok, Modlist.t()} | {:error, Changeset.t(Modlist.t())} | ||
def add_mod_to_list(list, mod) do | ||
_cs = Modlist.changeset(list) | ||
# TODO | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO found |
||
{:ok, list} | ||
end | ||
|
||
@doc """ | ||
Creates a modlist. | ||
""" | ||
@spec create_modlist(change :: Utils.unclean_change()) :: | ||
{:ok, Modlist.t()} | {:error, Changeset.t(Modlist.t())} | ||
def create_modlist(change \\ %{}) do | ||
change | ||
|> Modlist.clean_changes() | ||
|> Modlist.new() | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Gets a single modlist. | ||
|
||
Raises `Ecto.NoResultsError` if the ModTag does not exist. | ||
""" | ||
@spec get_modlist!(id :: Modlist.id()) :: Modlist.t() | ||
def get_modlist!(id), do: Repo.get!(Modlist, id) | ||
|
||
@doc """ | ||
Updates a modlist. | ||
""" | ||
@spec update_modlist(Modlist.t(), change :: Utils.unclean_change()) :: | ||
{:ok, Modlist.t()} | {:error, Changeset.t(Modlist.t())} | ||
def update_modlist(%Modlist{} = modlist, change) do | ||
modlist | ||
|> Modlist.changeset(change) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a modlist. | ||
""" | ||
@spec delete_modlist(Modlist.t()) :: {:ok, Modlist.t()} | {:error, Changeset.t(Modlist.t())} | ||
def delete_modlist(%Modlist{} = modlist), do: Repo.delete(modlist) | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking modlist changes. | ||
""" | ||
@spec change_modlist(modlist :: Modlist.t()) :: Changeset.t(Modlist.t()) | ||
def change_modlist(%Modlist{} = modlist), do: Modlist.changeset(modlist, %{}) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alias
Common.Schema.Modlist
is not alphabetically ordered among its group.