From 803b93fb635bbe57badcfd5243e2848ad2ed89d4 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Fri, 13 Jan 2017 18:18:49 -0800 Subject: [PATCH] Simplify feed controller (move serialization to ToString method) --- src/Podsync/Controllers/FeedController.cs | 14 +------------- src/Podsync/Services/Rss/Feed/Rss.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Podsync/Controllers/FeedController.cs b/src/Podsync/Controllers/FeedController.cs index 5372078..337965a 100644 --- a/src/Podsync/Controllers/FeedController.cs +++ b/src/Podsync/Controllers/FeedController.cs @@ -4,7 +4,6 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; -using System.Xml.Serialization; using Microsoft.AspNetCore.Mvc; using Podsync.Helpers; using Podsync.Services; @@ -12,7 +11,6 @@ using Podsync.Services.Links; using Podsync.Services.Resolver; using Podsync.Services.Rss; using Podsync.Services.Rss.Feed; -using Podsync.Services.Rss.Feed.Internal; using Podsync.Services.Storage; using Shared; @@ -28,8 +26,6 @@ namespace Podsync.Controllers ["audio/mp4"] = "m4a" }; - private readonly XmlSerializer _serializer = new XmlSerializer(typeof(Rss)); - private readonly IRssBuilder _rssBuilder; private readonly ILinkService _linkService; private readonly IStorageService _storageService; @@ -111,15 +107,7 @@ namespace Podsync.Controllers item.DownloadLink = new Uri(selfHost, $"download/{feedId}/{item.Id}.{ext}"); }); - // Serialize feed to string - string body; - using (var writer = new Utf8StringWriter()) - { - _serializer.Serialize(writer, rss); - body = writer.ToString(); - } - - return Content(body, "application/rss+xml; charset=UTF-8"); + return Content(rss.ToString(), "application/rss+xml; charset=UTF-8"); } } } \ No newline at end of file diff --git a/src/Podsync/Services/Rss/Feed/Rss.cs b/src/Podsync/Services/Rss/Feed/Rss.cs index 8d223b0..61c9eca 100644 --- a/src/Podsync/Services/Rss/Feed/Rss.cs +++ b/src/Podsync/Services/Rss/Feed/Rss.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; +using Podsync.Services.Rss.Feed.Internal; using Shared; namespace Podsync.Services.Rss.Feed @@ -43,5 +44,17 @@ namespace Podsync.Services.Rss.Feed var serializer = new XmlSerializer(typeof(Channel)); Channels.ForEach(channel => serializer.Serialize(writer, channel)); } + + public override string ToString() + { + var serializer = new XmlSerializer(typeof(Rss)); + + // Serialize feed to XML string + using (var writer = new Utf8StringWriter()) + { + serializer.Serialize(writer, this); + return writer.ToString(); + } + } } } \ No newline at end of file