Skip to content

Commit

Permalink
support atom by category on api level
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiWang committed Sep 18, 2023
1 parent 9e4214c commit d7483bb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Moonglade.Core/BlogCachePartition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum BlogCachePartition
PostCountCategory,
PostCountTag,
PostCountFeatured,
RssCategory
RssCategory,
AtomCategory
}
7 changes: 5 additions & 2 deletions src/Moonglade.Syndication/GetAtomStringQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Moonglade.Syndication;

public record GetAtomStringQuery : IRequest<string>;
public record GetAtomStringQuery(string CategoryName = null) : IRequest<string>;

public class GetAtomStringQueryHandler : IRequestHandler<GetAtomStringQuery, string>
{
Expand All @@ -31,7 +31,10 @@ public GetAtomStringQueryHandler(IBlogConfig blogConfig, ISyndicationDataSource

public async Task<string> Handle(GetAtomStringQuery request, CancellationToken ct)
{
_feedGenerator.FeedItemCollection = await _sdds.GetFeedDataAsync();
var data = await _sdds.GetFeedDataAsync(request.CategoryName);
if (data is null) return null;

_feedGenerator.FeedItemCollection = data;
var xml = await _feedGenerator.WriteAtomAsync();
return xml;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Moonglade.Web/Controllers/SubscriptionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ public async Task<IActionResult> Rss([MaxLength(64)] string routeName = null)
});
}

[HttpGet("atom")]
[HttpGet("atom/{routeName?}")]
public async Task<IActionResult> Atom([MaxLength(64)] string routeName = null)
{
bool hasRoute = !string.IsNullOrWhiteSpace(routeName);
var route = hasRoute ? routeName.ToLower().Trim() : null;

return await _cache.GetOrCreateAsync(BlogCachePartition.General.ToString(), "atom", async entry =>
return await _cache.GetOrCreateAsync(
hasRoute ? BlogCachePartition.AtomCategory.ToString() : BlogCachePartition.General.ToString(), route ?? "atom", async entry =>
{
entry.SlidingExpiration = TimeSpan.FromHours(1);

var xml = await _mediator.Send(new GetAtomStringQuery());
var xml = await _mediator.Send(new GetAtomStringQuery(routeName));
return Content(xml, "text/xml");
});
}
Expand Down

0 comments on commit d7483bb

Please sign in to comment.