• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

camatcode / basenji / a4bf1f5509804ed15c16dac5d914cfac51d46ef1

03 Jul 2025 08:35PM UTC coverage: 57.545% (-0.8%) from 58.297%
a4bf1f5509804ed15c16dac5d914cfac51d46ef1

Pull #20

github

camatcode
feat(collections): crud
Pull Request #20: feat(collections): crud

31 of 57 new or added lines in 4 files covered. (54.39%)

286 of 497 relevant lines covered (57.55%)

160.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/lib/basenji/collections.ex
1
defmodule Basenji.Collections do
2
  @moduledoc false
3

4
  import Basenji.ContextUtils
5
  import Ecto.Query
6

7
  alias Basenji.Collection
8
  alias Basenji.CollectionComic
9
  alias Basenji.Comic
10
  alias Basenji.Comics
11
  alias Basenji.Repo
12

13
  def create_collection(attrs, opts \\ []) do
14
    opts = Keyword.merge([repo_opts: []], opts)
1✔
15

16
    %Collection{collection_comics: []}
17
    |> Collection.changeset(attrs)
18
    |> Repo.insert(opts[:repo_opts])
1✔
19
  end
20

21
  def from_directory(attrs, path, opts \\ []) do
NEW
22
    path = Path.expand(path)
×
23

NEW
24
    comics =
×
NEW
25
      Path.wildcard("#{path}/**/*.cb*")
×
26
      |> Enum.map(fn file ->
27
        Comics.from_resource(file, %{}, opts)
NEW
28
        |> case do
×
NEW
29
          {:ok, created} -> created
×
NEW
30
          _ -> nil
×
31
        end
32
      end)
33
      |> Enum.filter(&Function.identity/1)
34

NEW
35
    with {:ok, collection} <- create_collection(attrs, opts) do
×
NEW
36
      Enum.each(comics, fn c -> add_to_collection(collection, c) end)
×
NEW
37
      get_collection(collection.id, preload: [collection_comics: [:comic]])
×
38
    end
39
  end
40

41
  def list_collections(opts \\ []) do
42
    opts = Keyword.merge([repo_opts: []], opts)
1✔
43

44
    Collection
45
    |> reduce_opts(opts)
46
    |> reduce_collection_opts(opts)
47
    |> Repo.all(opts[:repo_opts])
1✔
48
  end
49

50
  def get_collection(id, opts \\ []) do
NEW
51
    opts = Keyword.merge([repo_opts: []], opts)
×
52

53
    from(c in Collection, where: c.id == ^id)
54
    |> reduce_opts(opts)
55
    |> reduce_collection_opts(opts)
56
    |> Repo.one(opts[:repo_opts])
NEW
57
    |> case do
×
NEW
58
      nil -> {:error, :not_found}
×
NEW
59
      result -> {:ok, result}
×
60
    end
61
  end
62

63
  def update_collection(%Collection{} = collection, attrs) do
64
    collection
65
    |> Collection.changeset(attrs)
66
    |> Repo.update()
11✔
67
  end
68

69
  def update_collection(id, attrs) when is_bitstring(id) do
NEW
70
    with {:ok, collection} <- get_collection(id) do
×
NEW
71
      update_collection(collection, attrs)
×
72
    end
73
  end
74

NEW
75
  def add_to_collection(collection_ref, comic_ref, attrs \\ %{}, opts \\ [])
×
76

77
  def add_to_collection(%Collection{id: collection_id}, %Comic{id: comic_id}, attrs, opts) do
NEW
78
    add_to_collection(collection_id, comic_id, attrs, opts)
×
79
  end
80

81
  def add_to_collection(collection_id, comic_id, attrs, opts) do
NEW
82
    opts = Keyword.merge([repo_opts: []], opts)
×
83

84
    %CollectionComic{collection_id: collection_id, comic_id: comic_id}
85
    |> Basenji.CollectionComic.changeset(attrs)
NEW
86
    |> Repo.insert(opts[:repo_opts])
×
87
  end
88

89
  def delete_collection(collection_ref)
90

NEW
91
  def delete_collection(nil), do: nil
×
92

93
  def delete_collection(%Collection{id: collection_id}) do
94
    delete_collection(collection_id)
10✔
95
  end
96

97
  def delete_collection(collection_id) do
98
    Repo.delete(%Collection{id: collection_id})
10✔
99
  end
100

101
  def remove_from_collection(%CollectionComic{id: _coll_comic_id} = coll_comic) do
102
    Repo.delete(coll_comic)
10✔
103
  end
104

105
  def remove_from_collection(collection_ref, comic_ref)
106

107
  def remove_from_collection(%Collection{id: collection_id}, %Comic{id: comic_id}) do
NEW
108
    remove_from_collection(collection_id, comic_id)
×
109
  end
110

111
  def remove_from_collection(collection_id, comic_id) do
112
    from(c in CollectionComic,
113
      where: c.collection_id == ^collection_id and c.comic_id == ^comic_id
114
    )
115
    |> Repo.one()
116
    |> case do
10✔
NEW
117
      nil -> {:ok, nil}
×
118
      col_comic -> remove_from_collection(col_comic)
10✔
119
    end
120
  end
121

122
  defp reduce_collection_opts(query, opts) do
123
    Enum.reduce(opts, query, fn
1✔
124
      {_any, ""}, query ->
NEW
125
        query
×
126

127
      {_any, nil}, query ->
NEW
128
        query
×
129

130
      _, query ->
131
        query
2✔
132
    end)
133
  end
134
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc