From 0b818452b0a0f2863139d67d0c281c187d5c39ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Tue, 4 Jun 2019 22:46:26 +0200 Subject: [PATCH] Added mass disable / enable --- Tv7Playlist/Controllers/HomeController.cs | 38 ++++++- Tv7Playlist/Models/HomeModel.cs | 9 +- Tv7Playlist/Models/PlaylistEntryModel.cs | 24 +++++ Tv7Playlist/Views/Home/Index.cshtml | 120 ++++++++++++---------- 4 files changed, 133 insertions(+), 58 deletions(-) create mode 100644 Tv7Playlist/Models/PlaylistEntryModel.cs diff --git a/Tv7Playlist/Controllers/HomeController.cs b/Tv7Playlist/Controllers/HomeController.cs index 0b3de43..5bc45e5 100644 --- a/Tv7Playlist/Controllers/HomeController.cs +++ b/Tv7Playlist/Controllers/HomeController.cs @@ -12,9 +12,9 @@ namespace Tv7Playlist.Controllers { public class HomeController : Controller { + private readonly IAppConfig _appConfig; private readonly PlaylistContext _playlistContext; private readonly IPlaylistSynchronizer _playlistSynchronizer; - private readonly IAppConfig _appConfig; public HomeController(PlaylistContext playlistContext, IPlaylistSynchronizer playlistSynchronizer, IAppConfig appConfig) { @@ -26,12 +26,29 @@ namespace Tv7Playlist.Controllers [HttpGet] public async Task Index() { - var playlistEntries = await _playlistContext.PlaylistEntries.AsNoTracking().OrderBy(e => e.Position).ToListAsync(); + var playlistEntries = await _playlistContext.PlaylistEntries.AsNoTracking().OrderBy(e => e.Position) + .Select(e => new PlaylistEntryModel(e)).ToListAsync(); var model = new HomeModel(playlistEntries); return View(model); } + [HttpPost] + public async Task DisableSelectedEntries([FromForm] HomeModel model) + { + if (ModelState.IsValid) await UpdateEnabledForItems(model, false); + + return Redirect("/"); + } + + [HttpPost] + public async Task EnableSelectedEntries([FromForm] HomeModel model) + { + if (ModelState.IsValid) await UpdateEnabledForItems(model, true); + + return Redirect("/"); + } + [HttpGet] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() @@ -54,8 +71,23 @@ namespace Tv7Playlist.Controllers public async Task Synchronize(bool ok) { await _playlistSynchronizer.SynchronizeAsync(); - + return RedirectToAction("Index", "Home"); } + + private async Task UpdateEnabledForItems(HomeModel model, bool isEnabled) + { + if (model == null) throw new ArgumentNullException(nameof(model)); + var idsToUpdate = model.PlaylistEntries.Where(e => e.Selected).Select(e => e.Id); + foreach (var id in idsToUpdate) + { + var entry = await _playlistContext.PlaylistEntries.FindAsync(id); + if (entry == null) continue; + + entry.IsEnabled = isEnabled; + } + + await _playlistContext.SaveChangesAsync(); + } } } \ No newline at end of file diff --git a/Tv7Playlist/Models/HomeModel.cs b/Tv7Playlist/Models/HomeModel.cs index 49003c7..0c4a2a8 100644 --- a/Tv7Playlist/Models/HomeModel.cs +++ b/Tv7Playlist/Models/HomeModel.cs @@ -1,16 +1,19 @@ using System; using System.Collections.Generic; -using Tv7Playlist.Data; namespace Tv7Playlist.Models { public class HomeModel { - public HomeModel(List playlistEntries) + public HomeModel() + { + } + + public HomeModel(List playlistEntries) { PlaylistEntries = playlistEntries ?? throw new ArgumentNullException(nameof(playlistEntries)); } - public List PlaylistEntries { get; } + public List PlaylistEntries { get; set; } } } \ No newline at end of file diff --git a/Tv7Playlist/Models/PlaylistEntryModel.cs b/Tv7Playlist/Models/PlaylistEntryModel.cs new file mode 100644 index 0000000..ef97dde --- /dev/null +++ b/Tv7Playlist/Models/PlaylistEntryModel.cs @@ -0,0 +1,24 @@ +using System; +using Tv7Playlist.Data; + +namespace Tv7Playlist.Models +{ + public class PlaylistEntryModel + { + public PlaylistEntryModel() + { + } + + public PlaylistEntryModel(PlaylistEntry entry) + { + Entry = entry ?? throw new ArgumentNullException(nameof(entry)); + Id = entry.Id; + } + + public Guid Id { get; set; } + + public PlaylistEntry Entry { get; set; } + + public bool Selected { get; set; } + } +} \ No newline at end of file diff --git a/Tv7Playlist/Views/Home/Index.cshtml b/Tv7Playlist/Views/Home/Index.cshtml index 79a3972..450d9f1 100644 --- a/Tv7Playlist/Views/Home/Index.cshtml +++ b/Tv7Playlist/Views/Home/Index.cshtml @@ -3,56 +3,72 @@ ViewData["Title"] = "TV7 Playlist"; } -
-
- - - - - - - - - - - - - - - - - @{ - foreach (var channel in Model.PlaylistEntries) - { - - - - - - - - - - - - - - - } - } -
Number ImportNumber ExportPositionNameEPG NameEnabledAvailableURL ProxyURL OriginalCreatedModified
- Edit - Delete - @{ - if (channel.IsEnabled) - { - Disable - } - else - { - Enable - } - } - @channel.ChannelNumberImport@channel.ChannelNumberExport@channel.Position@channel.Name@channel.EpgMatchName@Html.Raw(channel.IsEnabled ? "Enabled" : "Disabled")@Html.Raw(channel.IsAvailable ? "yes" : "no")@channel.UrlProxy@channel.UrlOriginal@channel.Created.ToString("g")@channel.Modified.ToString("g")
+
+ +
+
+ + +
-
\ No newline at end of file + +
+
+ + + + + + + + + + + + + + + + + + @{ + for (var i = 0; i < Model.PlaylistEntries.Count; i++) + { + @Html.HiddenFor(m => m.PlaylistEntries[i].Id) + + + + + + + + + + + + + + + + } + } +
SelectedSingle ActionNumber ImportNumber ExportPositionNameEPG NameEnabledAvailableURL ProxyURL OriginalCreatedModified
+ + + Edit + Delete + @{ + if (Model.PlaylistEntries[i].Entry.IsEnabled) + { + Disable + } + else + { + Enable + } + } + @Model.PlaylistEntries[i].Entry.ChannelNumberImport@Model.PlaylistEntries[i].Entry.ChannelNumberExport@Model.PlaylistEntries[i].Entry.Position@Model.PlaylistEntries[i].Entry.Name@Model.PlaylistEntries[i].Entry.EpgMatchName@Html.Raw(Model.PlaylistEntries[i].Entry.IsEnabled ? "Enabled" : "Disabled")@Html.Raw(Model.PlaylistEntries[i].Entry.IsAvailable ? "yes" : "no")@Model.PlaylistEntries[i].Entry.UrlProxy@Model.PlaylistEntries[i].Entry.UrlOriginal@Model.PlaylistEntries[i].Entry.Created.ToString("g")@Model.PlaylistEntries[i].Entry.Modified.ToString("g")
+
+
+ + \ No newline at end of file