Skip to content

Commit 2e450a9

Browse files
authored
Merge pull request #688 from thousandtyone/main
Implements static pages.
2 parents 70b5df3 + 57a4943 commit 2e450a9

File tree

10 files changed

+122
-1
lines changed

10 files changed

+122
-1
lines changed

source/DasBlog.Web.Repositories/BlogManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public Entry GetBlogPost(string posttitle, DateTime? dt)
5959
}
6060
}
6161

62+
public StaticPage GetStaticPage(string posttitle)
63+
{
64+
return dataService.GetStaticPage(posttitle);
65+
}
66+
6267
public Entry GetBlogPostByGuid(Guid postid)
6368
{
6469
return dataService.GetEntry(postid.ToString());

source/DasBlog.Web.Repositories/Interfaces/IBlogManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace DasBlog.Managers.Interfaces
88
public interface IBlogManager
99
{
1010
Entry GetBlogPost(string posttitle, DateTime? postDate);
11-
11+
StaticPage GetStaticPage(string posttitle);
1212
Entry GetBlogPostByGuid(Guid postid);
1313

1414
Entry GetEntryForEdit(string postid);

source/DasBlog.Web.UI/Controllers/BlogPostController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class BlogPostController : DasBlogBaseController
4141
private readonly IExternalEmbeddingHandler embeddingHandler;
4242
private readonly IRecaptchaService recaptcha;
4343

44+
4445
public BlogPostController(IBlogManager blogManager, IHttpContextAccessor httpContextAccessor, IDasBlogSettings dasBlogSettings,
4546
IMapper mapper, ICategoryManager categoryManager, IFileSystemBinaryManager binaryManager, ILogger<BlogPostController> logger,
4647
IBlogPostViewModelCreator modelViewCreator, IMemoryCache memoryCache, IExternalEmbeddingHandler embeddingHandler, IRecaptchaService recaptcha)
@@ -96,6 +97,14 @@ public IActionResult Post(string posttitle, string day, string month, string yea
9697
}
9798
else
9899
{
100+
// Post was not found. Let's see if it's a static page before we route user to home page.
101+
var sp = blogManager.GetStaticPage(posttitle);
102+
if(sp != null)
103+
{
104+
var spvm = mapper.Map<StaticPageViewModel>(sp);
105+
return View("LoadStaticPage", spvm);
106+
107+
}
99108
return RedirectToAction("index", "home");
100109
}
101110
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using AutoMapper;
2+
using DasBlog.Web.Models.BlogViewModels;
3+
using DasBlog.Core.Common;
4+
using DasBlog.Core.Extensions;
5+
using newtelligence.DasBlog.Runtime;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using DasBlog.Services;
9+
using DasBlog.Core.Common.Comments;
10+
using DasBlog.Web.Models.AdminViewModels;
11+
12+
namespace DasBlog.Web.Mappers
13+
{
14+
public class ProfileStaticPage : Profile
15+
{
16+
public ProfileStaticPage()
17+
{
18+
CreateMap<StaticPage, StaticPageViewModel>();
19+
}
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc.Rendering;
6+
using Newtonsoft.Json;
7+
8+
namespace DasBlog.Web.Models.BlogViewModels
9+
{
10+
public class StaticPageViewModel
11+
{
12+
public string Name {get; set; }
13+
14+
[DataType(DataType.MultilineText)]
15+
public string Content { get; set; }
16+
17+
}
18+
}

source/DasBlog.Web.UI/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public void ConfigureServices(IServiceCollection services)
232232
mapperConfig.AddProfile(new ProfileDasBlogUser(serviceProvider.GetService<ISiteSecurityManager>()));
233233
mapperConfig.AddProfile(new ProfileSettings());
234234
mapperConfig.AddProfile(new ProfileActivityPub());
235+
mapperConfig.AddProfile(new ProfileStaticPage());
235236
})
236237
.AddMvc()
237238
.AddXmlSerializerFormatters();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@model DasBlog.Web.Models.BlogViewModels.StaticPageViewModel
2+
@using DasBlog.Core.Common
3+
@Html.Raw(Model.Content)
4+

source/newtelligence.DasBlog.Runtime/BlogDataService.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,5 +1421,21 @@ DateTime IBlogDataService.GetLastCommentUpdate()
14211421

14221422
return data.lastCommentUpdate;
14231423
}
1424+
1425+
public StaticPage GetStaticPage( string pagename )
1426+
{
1427+
StaticPage page = new StaticPage();
1428+
page.Name = pagename;
1429+
string staticPathLocation = this.contentBaseDirectory + "\\static\\" + pagename + ".html";
1430+
if (File.Exists(staticPathLocation))
1431+
{
1432+
page.Content = File.ReadAllText(staticPathLocation);
1433+
}
1434+
else
1435+
{
1436+
return null;
1437+
}
1438+
return page;
1439+
}
14241440
}
14251441
}

source/newtelligence.DasBlog.Runtime/IBlogDataService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,7 @@ EntryCollection GetEntries(
233233
/// </summary>
234234
/// <returns></returns>
235235
DateTime GetLastCommentUpdate();
236+
237+
StaticPage GetStaticPage( string pagename );
236238
}
237239
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NodaTime;
2+
using System;
3+
using System.Collections;
4+
using System.Globalization;
5+
using System.IO;
6+
using System.Net;
7+
using System.Text;
8+
using System.Xml;
9+
using System.Xml.Serialization;
10+
using System.Collections.Generic;
11+
12+
namespace newtelligence.DasBlog.Runtime
13+
{
14+
[Serializable]
15+
[XmlRoot(Namespace=Data.NamespaceURI)]
16+
[XmlType(Namespace=Data.NamespaceURI)]
17+
public class StaticPage
18+
{
19+
string _content;
20+
string _name;
21+
22+
public string Name
23+
{
24+
get
25+
{
26+
return _name;
27+
}
28+
set
29+
{
30+
_name = value;
31+
}
32+
}
33+
public string Content
34+
{
35+
get
36+
{
37+
return _content;
38+
}
39+
set
40+
{
41+
_content = value;
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)