From 8ef5e0f41f4f99392336517b47f02a024bfc0723 Mon Sep 17 00:00:00 2001 From: Kieran Date: Wed, 27 Mar 2024 10:27:48 -0700 Subject: [PATCH] Added total downloaded count to source view (#134) --- .../controllers/sources/source_controller.ex | 17 ++++++++++++++++- .../sources/source_html/show.html.heex | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/pinchflat_web/controllers/sources/source_controller.ex b/lib/pinchflat_web/controllers/sources/source_controller.ex index 3211ccc..fab66b3 100644 --- a/lib/pinchflat_web/controllers/sources/source_controller.ex +++ b/lib/pinchflat_web/controllers/sources/source_controller.ex @@ -1,12 +1,15 @@ defmodule PinchflatWeb.Sources.SourceController do use PinchflatWeb, :controller + import Ecto.Query, warn: false + alias Pinchflat.Repo alias Pinchflat.Media alias Pinchflat.Tasks alias Pinchflat.Sources alias Pinchflat.Profiles alias Pinchflat.Sources.Source + alias Pinchflat.Media.MediaItem def index(conn, _params) do sources = Repo.preload(Sources.list_sources(), :media_profile) @@ -54,7 +57,8 @@ defmodule PinchflatWeb.Sources.SourceController do source: source, pending_tasks: pending_tasks, pending_media: pending_media, - downloaded_media: downloaded_media + downloaded_media: downloaded_media, + total_downloaded: total_downloaded_for(source) ) end @@ -104,6 +108,17 @@ defmodule PinchflatWeb.Sources.SourceController do Profiles.list_media_profiles() end + # NOTE: should move this out of the controller + # once I finally add some query fragment layer + defp total_downloaded_for(source) do + from( + m in MediaItem, + where: m.source_id == ^source.id, + where: not is_nil(m.media_filepath) + ) + |> Repo.aggregate(:count, :id) + end + defp get_onboarding_layout do if Settings.get!(:onboarding) do {Layouts, :onboarding} diff --git a/lib/pinchflat_web/controllers/sources/source_html/show.html.heex b/lib/pinchflat_web/controllers/sources/source_html/show.html.heex index 3c2934b..c98ba87 100644 --- a/lib/pinchflat_web/controllers/sources/source_html/show.html.heex +++ b/lib/pinchflat_web/controllers/sources/source_html/show.html.heex @@ -88,7 +88,7 @@ <:tab title="Downloaded Media"> <%= if match?([_|_], @downloaded_media) do %> -

Shows a maximum of 100 media items

+

Shows a maximum of 100 media items (<%= @total_downloaded %> total)

<.table rows={@downloaded_media} table_class="text-black dark:text-white"> <:col :let={media_item} label="Title"> <%= StringUtils.truncate(media_item.title, 50) %>