WIP: adds first steps to use datatables for mass updates and search / sort

This commit is contained in:
2020-02-08 01:05:48 +01:00
parent d4badf7f9e
commit 796c5516f9
3 changed files with 66 additions and 41 deletions

View File

@@ -33,22 +33,6 @@ namespace Tv7Playlist.Controllers
return View(model);
}
[HttpPost]
public async Task<IActionResult> DisableSelectedEntries([FromForm] HomeModel model)
{
if (ModelState.IsValid) await UpdateEnabledForItems(model, false);
return Redirect("/");
}
[HttpPost]
public async Task<IActionResult> 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();
}
}
}

View File

@@ -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<HomeController> _logger;
private readonly IPlaylistBuilder _playlistBuilder;
private readonly PlaylistContext _playlistContext;
public PlaylistApiController(ILogger<HomeController> logger, IPlaylistSynchronizer playlistSynchronizer,
IPlaylistBuilder playlistBuilder, IAppConfig appConfig)
public PlaylistApiController(ILogger<HomeController> 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<IActionResult> DisableChannels([FromBody]ICollection<Guid> ids)
{
await UpdateEnabledForItems(ids, false);
return Ok();
}
[HttpPut]
[Route("enable")]
public async Task<IActionResult> EnableChannels([FromBody]ICollection<Guid> ids)
{
await UpdateEnabledForItems(ids, true);
return Ok();
}
private async Task<IActionResult> GetPlaylistInternal(bool useProxy)
{
var playlistStream = await _playlistBuilder.GeneratePlaylistAsync(useProxy);
@@ -67,5 +87,18 @@ namespace Tv7Playlist.Controllers
return downloadFileName;
}
private async Task UpdateEnabledForItems(IEnumerable<Guid> 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();
}
}
}

View File

@@ -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);
}
</script>
</form>