Current section

7 Versions

Jump to

Compare versions

8 files changed
+186 additions
-49 deletions
  @@ -43,6 +43,11 @@
43 43 {<<"optional">>,false},
44 44 {<<"repository">>,<<"hexpm">>},
45 45 {<<"requirement">>,<<"~> 3.5">>}],
46 + [{<<"app">>,<<"ecto_soft_delete">>},
47 + {<<"name">>,<<"ecto_soft_delete">>},
48 + {<<"optional">>,false},
49 + {<<"repository">>,<<"hexpm">>},
50 + {<<"requirement">>,<<">= 0.0.0">>}],
46 51 [{<<"app">>,<<"ex_money">>},
47 52 {<<"name">>,<<"ex_money">>},
48 53 {<<"optional">>,false},
  @@ -73,4 +78,4 @@
73 78 {<<"optional">>,true},
74 79 {<<"repository">>,<<"hexpm">>},
75 80 {<<"requirement">>,<<"~> 1.7">>}]]}.
76 - {<<"version">>,<<"1.5.0">>}.
81 + {<<"version">>,<<"1.5.1">>}.
  @@ -5,6 +5,7 @@ defmodule ExCatalog do
5 5 @repo ExCatalog.Config.repo()
6 6
7 7 alias ExCatalog.Category
8 + alias ExCatalog.Product
8 9
9 10 @doc """
10 11 List version.
  @@ -27,14 +28,25 @@ defmodule ExCatalog do
27 28
28 29
29 30 """
30 - def index(limit \\ 25, metadata \\ nil, cursor \\ nil) do
31 + def index(limit \\ 25, metadata \\ nil, cursor \\ nil, deleted \\ false) do
31 32 import Ecto.Query
33 + import Ecto.SoftDelete.Query
32 34
33 35 query =
34 - from(ExCatalog.Category,
35 - preload: [:parent_category],
36 - preload: [:image]
37 - )
36 + case(deleted) do
37 + false ->
38 + from(ExCatalog.Category,
39 + preload: [:parent_category],
40 + preload: [:image]
41 + )
42 +
43 + true ->
44 + from(ExCatalog.Category,
45 + preload: [:parent_category],
46 + preload: [:image]
47 + )
48 + |> with_undeleted
49 + end
38 50
39 51 case cursor do
40 52 :before ->
  @@ -75,22 +87,37 @@ defmodule ExCatalog do
75 87
76 88
77 89 """
78 - def products(limit \\ 25, currency \\ :USD) do
79 - products(limit, nil, nil, currency)
90 + def products(limit \\ 25, currency \\ :USD, deleted \\ false) do
91 + products(limit, nil, nil, currency, deleted)
80 92 end
81 93
82 - def products(limit \\ 25, metadata, cursor, currency) do
94 + def products(limit \\ 25, metadata, cursor, currency, deleted) do
83 95 import Ecto.Query
96 + import Ecto.SoftDelete.Query
84 97
85 98 query =
86 - from(ExCatalog.Product,
87 - preload: [:variations],
88 - preload: [:categories],
89 - preload: [:metas],
90 - preload: [:primary_image],
91 - preload: [:images],
92 - preload: [:videos]
93 - )
99 + case(deleted) do
100 + false ->
101 + from(ExCatalog.Product,
102 + preload: [:variations],
103 + preload: [:categories],
104 + preload: [:metas],
105 + preload: [:primary_image],
106 + preload: [:images],
107 + preload: [:videos]
108 + )
109 +
110 + true ->
111 + from(ExCatalog.Product,
112 + preload: [:variations],
113 + preload: [:categories],
114 + preload: [:metas],
115 + preload: [:primary_image],
116 + preload: [:images],
117 + preload: [:videos]
118 + )
119 + |> with_undeleted
120 + end
94 121
95 122 reply =
96 123 case cursor do
  @@ -147,19 +174,35 @@ defmodule ExCatalog do
147 174
148 175
149 176 """
150 - def product(sku, currency \\ nil) do
177 + def product(sku, currency \\ nil, deleted \\ false) do
151 178 import Ecto.Query
179 + import Ecto.SoftDelete.Query
152 180
153 181 query =
154 - from(ExCatalog.Product,
155 - where: [sku: ^sku],
156 - preload: [:variations],
157 - preload: [:categories],
158 - preload: [:metas],
159 - preload: [:primary_image],
160 - preload: [:images],
161 - preload: [:videos]
162 - )
182 + case(deleted) do
183 + false ->
184 + from(ExCatalog.Product,
185 + where: [sku: ^sku],
186 + preload: [:variations],
187 + preload: [:categories],
188 + preload: [:metas],
189 + preload: [:primary_image],
190 + preload: [:images],
191 + preload: [:videos]
192 + )
193 +
194 + true ->
195 + from(ExCatalog.Product,
196 + where: [sku: ^sku],
197 + preload: [:variations],
198 + preload: [:categories],
199 + preload: [:metas],
200 + preload: [:primary_image],
201 + preload: [:images],
202 + preload: [:videos]
203 + )
204 + |> with_undeleted
205 + end
163 206
164 207 [reply] = @repo.all(query)
165 208
  @@ -182,25 +225,41 @@ defmodule ExCatalog do
182 225
183 226
184 227 """
185 - def products_by_category(slug, limit \\ 25, currency \\ :USD) do
186 - products_by_category(slug, limit, nil, nil, currency)
228 + def products_by_category(slug, limit \\ 25, currency \\ :USD, deleted \\ false) do
229 + products_by_category(slug, limit, nil, nil, currency, deleted)
187 230 end
188 231
189 - def products_by_category(slug, limit, metadata, cursor, currency) do
232 + def products_by_category(slug, limit, metadata, cursor, currency, deleted) do
190 233 category = @repo.get_by(%Category{}, slug: slug)
191 234
192 235 import Ecto.Query
236 + import Ecto.SoftDelete.Query
193 237
194 238 query =
195 - from(ExCatalog.Product,
196 - where: [category_id: ^category.id],
197 - preload: [:variations],
198 - preload: [:categories],
199 - preload: [:metas],
200 - preload: [:primary_image],
201 - preload: [:images],
202 - preload: [:videos]
203 - )
239 + case(deleted) do
240 + false ->
241 + from(ExCatalog.Product,
242 + where: [category_id: ^category.id],
243 + preload: [:variations],
244 + preload: [:categories],
245 + preload: [:metas],
246 + preload: [:primary_image],
247 + preload: [:images],
248 + preload: [:videos]
249 + )
250 +
251 + true ->
252 + from(ExCatalog.Product,
253 + where: [category_id: ^category.id],
254 + preload: [:variations],
255 + preload: [:categories],
256 + preload: [:metas],
257 + preload: [:primary_image],
258 + preload: [:images],
259 + preload: [:videos]
260 + )
261 + |> with_undeleted
262 + end
204 263
205 264 reply =
206 265 case cursor do
  @@ -236,15 +295,67 @@ defmodule ExCatalog do
236 295 reply
237 296
238 297 _ ->
239 - {p, metadata} = reply
298 + {data, meta} = reply
240 299
241 300 modified =
242 - Enum.map(p, fn x ->
301 + Enum.map(data, fn x ->
243 302 {:ok, price} = ExCatalog.Currencies.convert(x.price, currency)
244 303 %{x | price: price}
245 304 end)
246 305
247 - {modified, metadata}
306 + {modified, meta}
307 + end
308 + end
309 +
310 + @doc """
311 + Change the status active or disabled, this controls which products the user can see, (only the active products)
312 +
313 + ## Examples
314 +
315 + iex> ExCatalog.active(:category, "111222233")
316 + iex> ExCatalog.active(:product, "111222233")
317 +
318 +
319 + """
320 + def active(type, id) do
321 + import Ecto.Query
322 + import Ecto.SoftDelete.Query
323 +
324 + query =
325 + case type do
326 + :category ->
327 + from(c in Category, where: c.id == ^id, select: c)
328 + |> with_undeleted
329 +
330 + _ ->
331 + from(p in Product, where: p.id == ^id, select: p)
332 + |> with_undeleted
333 + end
334 +
335 + case Keyword.has_key?(@repo.__info__(:functions), :soft_restore!) do
336 + true ->
337 + @repo.one!(query)
338 + |> @repo.soft_restore!()
339 +
340 + false ->
341 + {:error, "Not Available"}
342 + end
343 + end
344 +
345 + def active(type, id, false) do
346 + import Ecto.Query
347 + import Ecto.SoftDelete.Query
348 +
349 + case type do
350 + :category ->
351 + @repo.get_by!(Category, id: id)
352 +
353 + from(c in Category, where: c.id == ^id, select: c)
354 + |> @repo.soft_delete!()
355 +
356 + _ ->
357 + from(p in Product, where: p.id == ^id, select: p)
358 + |> @repo.soft_delete!()
248 359 end
249 360 end
250 361 end
  @@ -13,10 +13,16 @@ defmodule ExCatalog.Pdf do
13 13
14 14
15 15 """
16 - def export_products(template, limit \\ 500, filename \\ "products.pdf") do
16 + def export_products(
17 + template,
18 + limit \\ 500,
19 + filename \\ "products.pdf",
20 + currency \\ :USD,
21 + deleted \\ false
22 + ) do
17 23 case ExCatalog.Util.module_compiled?(ChromicPDF) do
18 24 true ->
19 - {data, meta} = ExCatalog.products(limit)
25 + {data, meta} = ExCatalog.products(limit, currency, deleted)
20 26
21 27 html =
22 28 Enum.map(data, fn p ->
  @@ -66,10 +72,10 @@ defmodule ExCatalog.Pdf do
66 72
67 73
68 74 """
69 - def export_categories(template, limit \\ 500, filename \\ "categories.csv") do
75 + def export_categories(template, limit \\ 500, filename \\ "categories.csv", deleted \\ false) do
70 76 case ExCatalog.Util.module_compiled?(CSV) do
71 77 true ->
72 - {data, meta} = ExCatalog.index(limit)
78 + {data, meta} = ExCatalog.index(limit, nil, nil, deleted)
73 79
74 80 rendered =
75 81 Enum.map(data, fn p ->
  @@ -121,9 +127,14 @@ defmodule ExCatalog.Pdf do
121 127
122 128 """
123 129
124 - def export({categories_template, products_template}, limit \\ 500) do
130 + def export(
131 + {categories_template, products_template},
132 + limit \\ 500,
133 + currency \\ :USD,
134 + deleted \\ false
135 + ) do
125 136 {category_status, _} = export_categories(categories_template, limit)
126 - {products_status, _} = export_products(products_template, limit)
137 + {products_status, _} = export_products(products_template, limit, currency, deleted)
127 138
128 139 case category_status == :ok && products_status == :ok do
129 140 true -> {:ok, "Export Success"}
  @@ -3,6 +3,8 @@ defmodule ExCatalog.Repo do
3 3 otp_app: :ex_catalog,
4 4 adapter: Ecto.Adapters.Postgres
5 5
6 + use Ecto.SoftDelete.Repo
7 +
6 8 use Paginator
7 9
8 10 @doc """
  @@ -3,6 +3,7 @@ defmodule ExCatalog.Category do
3 3 use EctoAutoslugField.Slug, from: :title, to: :slug
4 4 use ExCatalog.AutoSlug
5 5 import Ecto.Changeset
6 + import Ecto.SoftDelete.Schema
6 7
7 8 alias ExCatalog.Category.TitleSlug
8 9
  @@ -15,6 +16,8 @@ defmodule ExCatalog.Category do
15 16
16 17 belongs_to(:parent_category, ExCatalog.Category)
17 18 belongs_to(:image, ExCatalog.Image)
19 +
20 + soft_delete_schema()
18 21 end
19 22
20 23 def changeset(schema, attrs) do
Loading more files…