From 20f0b767e849dfa7fbc8351528d2c6304f0bd93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sat, 29 Dec 2018 00:44:47 +0100 Subject: [PATCH] added first result table with containing the tracks (prototype) --- Tv7Playlist.Core/IPlaylistLoader.cs | 2 +- Tv7Playlist.Core/PlaylistLoader.cs | 14 +++++--------- Tv7Playlist/Controllers/HomeController.cs | 18 +++++++++++------- Tv7Playlist/Models/HomeModel.cs | 16 ++++++++++++++++ Tv7Playlist/Tv7Playlist.csproj | 2 +- Tv7Playlist/Views/Home/Index.cshtml | 16 +++++++++++++++- 6 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 Tv7Playlist/Models/HomeModel.cs diff --git a/Tv7Playlist.Core/IPlaylistLoader.cs b/Tv7Playlist.Core/IPlaylistLoader.cs index 390856e..f26041f 100644 --- a/Tv7Playlist.Core/IPlaylistLoader.cs +++ b/Tv7Playlist.Core/IPlaylistLoader.cs @@ -6,6 +6,6 @@ namespace Tv7Playlist.Core { public interface IPlaylistLoader { - Task> LoadPlaylistFromUrl(); + Task> LoadPlaylistFromUrl(string url); } } \ No newline at end of file diff --git a/Tv7Playlist.Core/PlaylistLoader.cs b/Tv7Playlist.Core/PlaylistLoader.cs index e0fce6e..e9a4fff 100644 --- a/Tv7Playlist.Core/PlaylistLoader.cs +++ b/Tv7Playlist.Core/PlaylistLoader.cs @@ -12,27 +12,23 @@ namespace Tv7Playlist.Core { private readonly ILogger _logger; private readonly IPlaylistParser _playlistParser; - private readonly string _tv7Url; - public PlaylistLoader(ILogger logger, IAppConfig appConfig, IPlaylistParser playlistParser) + public PlaylistLoader(ILogger logger, IPlaylistParser playlistParser) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _playlistParser = playlistParser ?? throw new ArgumentNullException(nameof(playlistParser)); - - _tv7Url = appConfig.TV7Url; - } - public async Task> LoadPlaylistFromUrl() + public async Task> LoadPlaylistFromUrl(string url) { using (var httpClient = new HttpClient()) { - _logger.LogInformation(LoggingEvents.Playlist, "Downloading playlist from {tv7url}", _tv7Url); + _logger.LogInformation(LoggingEvents.Playlist, "Downloading playlist from {tv7url}", url); - var tv7Response = await httpClient.GetAsync(_tv7Url); + var tv7Response = await httpClient.GetAsync(url); if (!tv7Response.IsSuccessStatusCode) { - _logger.LogWarning(LoggingEvents.PlaylistNotFound, "Could not download playlist from {tv7url}. The StatusCode was: {StatusCode}", _tv7Url, tv7Response.StatusCode); + _logger.LogWarning(LoggingEvents.PlaylistNotFound, "Could not download playlist from {tv7url}. The StatusCode was: {StatusCode}", url, tv7Response.StatusCode); } return await ParseTracksFromResponseAsync(tv7Response); diff --git a/Tv7Playlist/Controllers/HomeController.cs b/Tv7Playlist/Controllers/HomeController.cs index 7292c0e..617e533 100644 --- a/Tv7Playlist/Controllers/HomeController.cs +++ b/Tv7Playlist/Controllers/HomeController.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using Tv7Playlist.Core; using Tv7Playlist.Models; namespace Tv7Playlist.Controllers @@ -13,19 +14,22 @@ namespace Tv7Playlist.Controllers public class HomeController : Controller { private readonly ILogger _logger; + private readonly IAppConfig _appConfig; + private readonly IPlaylistLoader _playlistLoader; - public HomeController(ILogger logger, IConfiguration configuration) + public HomeController(ILogger logger, IAppConfig appConfig, IPlaylistLoader playlistLoader) { - if (configuration == null) throw new ArgumentNullException(nameof(configuration)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - - var appConfig = configuration.Get(); - + _appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig)); + _playlistLoader = playlistLoader ?? throw new ArgumentNullException(nameof(playlistLoader)); } - public IActionResult Index() + public async Task Index() { - return View(); + var tracks = await _playlistLoader.LoadPlaylistFromUrl(_appConfig.TV7Url); + var model = new HomeModel(tracks); + + return View(model); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] diff --git a/Tv7Playlist/Models/HomeModel.cs b/Tv7Playlist/Models/HomeModel.cs new file mode 100644 index 0000000..7a96690 --- /dev/null +++ b/Tv7Playlist/Models/HomeModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using Tv7Playlist.Core.Parsers; + +namespace Tv7Playlist.Models +{ + public class HomeModel + { + public IReadOnlyCollection Tracks { get; } + + public HomeModel(IReadOnlyCollection tracks) + { + Tracks = tracks ?? throw new ArgumentNullException(nameof(tracks)); + } + } +} \ No newline at end of file diff --git a/Tv7Playlist/Tv7Playlist.csproj b/Tv7Playlist/Tv7Playlist.csproj index c109f63..87d6c7d 100644 --- a/Tv7Playlist/Tv7Playlist.csproj +++ b/Tv7Playlist/Tv7Playlist.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.2 diff --git a/Tv7Playlist/Views/Home/Index.cshtml b/Tv7Playlist/Views/Home/Index.cshtml index 7a4e4d6..144ba1c 100644 --- a/Tv7Playlist/Views/Home/Index.cshtml +++ b/Tv7Playlist/Views/Home/Index.cshtml @@ -1,4 +1,5 @@ -@{ +@model HomeModel; +@{ ViewData["Title"] = "TV7 Playlist"; } @@ -6,3 +7,16 @@

Welcome

Learn about building Web apps with ASP.NET Core.

+ + +@{ + foreach (var track in Model.Tracks) + { + + + + + + } +} +
@track.Id@track.Name@track.Url
\ No newline at end of file