diff --git a/Tv7Playlist/Controllers/HomeController.cs b/Tv7Playlist/Controllers/HomeController.cs index 5bc45e5..03a5864 100644 --- a/Tv7Playlist/Controllers/HomeController.cs +++ b/Tv7Playlist/Controllers/HomeController.cs @@ -33,22 +33,6 @@ namespace Tv7Playlist.Controllers 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() @@ -75,19 +59,6 @@ namespace Tv7Playlist.Controllers 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/Controllers/PlaylistApiController.cs b/Tv7Playlist/Controllers/PlaylistApiController.cs index c8ad960..d45835d 100644 --- a/Tv7Playlist/Controllers/PlaylistApiController.cs +++ b/Tv7Playlist/Controllers/PlaylistApiController.cs @@ -1,8 +1,11 @@ using System; +using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Tv7Playlist.Core; +using Tv7Playlist.Data; namespace Tv7Playlist.Controllers { @@ -16,13 +19,14 @@ namespace Tv7Playlist.Controllers private readonly ILogger _logger; private readonly IPlaylistBuilder _playlistBuilder; + private readonly PlaylistContext _playlistContext; - public PlaylistApiController(ILogger logger, IPlaylistSynchronizer playlistSynchronizer, - IPlaylistBuilder playlistBuilder, IAppConfig appConfig) + public PlaylistApiController(ILogger logger, IPlaylistBuilder playlistBuilder, IAppConfig appConfig, PlaylistContext playlistContext) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _playlistBuilder = playlistBuilder ?? throw new ArgumentNullException(nameof(playlistBuilder)); _appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig)); + _playlistContext = playlistContext ?? throw new ArgumentNullException(nameof(playlistContext)); } [HttpGet] @@ -46,6 +50,22 @@ namespace Tv7Playlist.Controllers return await GetPlaylistInternal(true); } + [HttpPut] + [Route("disable")] + public async Task DisableChannels([FromBody]ICollection ids) + { + await UpdateEnabledForItems(ids, false); + return Ok(); + } + + [HttpPut] + [Route("enable")] + public async Task EnableChannels([FromBody]ICollection ids) + { + await UpdateEnabledForItems(ids, true); + return Ok(); + } + private async Task GetPlaylistInternal(bool useProxy) { var playlistStream = await _playlistBuilder.GeneratePlaylistAsync(useProxy); @@ -67,5 +87,18 @@ namespace Tv7Playlist.Controllers return downloadFileName; } + + private async Task UpdateEnabledForItems(IEnumerable ids, bool isEnabled) + { + foreach (var id in ids) + { + 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/Views/Home/Index.cshtml b/Tv7Playlist/Views/Home/Index.cshtml index 020cc0f..95d0e71 100644 --- a/Tv7Playlist/Views/Home/Index.cshtml +++ b/Tv7Playlist/Views/Home/Index.cshtml @@ -1,4 +1,5 @@ -@model HomeModel; +@using Microsoft.AspNetCore.Mvc.Routing +@model HomeModel; @{ ViewData["Title"] = "TV7 Playlist"; } @@ -116,20 +117,21 @@ $(document).ready(function() { { text: 'Disable selected', action: function ( e, dt, node, config ) { - alert( - 'Row data: '+ - JSON.stringify( dt.row( { selected: true } ).data() ) - ); + const ids = $.map(table.rows('.selected').data(), function (item) { + return item[1] + }); + setEnabledForChannels(urlDisable, ids); }, enabled: false }, { text: 'Enable selected', action: function ( e, dt, node, config ) { - const ids = $.map(table.rows('.selected').data(), function (item) { - return item[1] - }); - alert(JSON.stringify( ids )); + const ids = $.map(table.rows('.selected').data(), function (item) { + return item[1] + }); + setEnabledForChannels(urlEnable, ids); + }, enabled: false }, @@ -153,6 +155,25 @@ $(document).ready(function() { table.button( 4 ).enable( selectedRows > 0 ); } ); }); + +const urlEnable = '@Url.Action("EnableChannels","PlaylistApi")'; +const urlDisable = '@Url.Action("DisableChannels","PlaylistApi")'; + + function setEnabledForChannels(url, ids) { + const options = {}; + options.url = url; + options.type = "PUT"; + options.data = JSON.stringify(ids); + options.contentType = "application/json"; + options.dataType = "html"; + options.success = function (msg) { + $("#msg").html(msg); + }; + options.error = function () { + $("#msg").html("Error while calling the Web API!"); + }; + $.ajax(options); + }