forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogController.cs
194 lines (167 loc) · 7.93 KB
/
BlogController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Grand.Core;
using Grand.Core.Domain.Blogs;
using Grand.Core.Domain.Customers;
using Grand.Framework.Controllers;
using Grand.Framework.Mvc;
using Grand.Framework.Mvc.Filters;
using Grand.Framework.Mvc.Rss;
using Grand.Framework.Security.Captcha;
using Grand.Services.Blogs;
using Grand.Services.Localization;
using Grand.Services.Security;
using Grand.Services.Seo;
using Grand.Services.Stores;
using Grand.Web.Commands.Models;
using Grand.Web.Features.Models.Blogs;
using Grand.Web.Models.Blogs;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Grand.Web.Controllers
{
public partial class BlogController : BasePublicController
{
#region Fields
private readonly IMediator _mediator;
private readonly IBlogService _blogService;
private readonly IStoreContext _storeContext;
private readonly ILocalizationService _localizationService;
private readonly IWebHelper _webHelper;
private readonly IWorkContext _workContext;
private readonly BlogSettings _blogSettings;
private readonly CaptchaSettings _captchaSettings;
#endregion
#region Constructors
public BlogController(
IMediator mediator,
IBlogService blogService,
IStoreContext storeContext,
ILocalizationService localizationService,
IWebHelper webHelper,
IWorkContext workContext,
BlogSettings blogSettings,
CaptchaSettings captchaSettings)
{
_mediator = mediator;
_blogService = blogService;
_storeContext = storeContext;
_localizationService = localizationService;
_webHelper = webHelper;
_blogSettings = blogSettings;
_captchaSettings = captchaSettings;
_workContext = workContext;
}
#endregion
#region Methods
public virtual async Task<IActionResult> List(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var model = await _mediator.Send(new GetBlogPostList() { Command = command });
return View("List", model);
}
public virtual async Task<IActionResult> BlogByTag(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var model = await _mediator.Send(new GetBlogPostList() { Command = command });
return View("List", model);
}
public virtual async Task<IActionResult> BlogByMonth(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var model = await _mediator.Send(new GetBlogPostList() { Command = command });
return View("List", model);
}
public virtual async Task<IActionResult> BlogByCategory(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var model = await _mediator.Send(new GetBlogPostList() { Command = command });
return View("List", model);
}
public virtual async Task<IActionResult> BlogByKeyword(BlogPagingFilteringModel command)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var model = await _mediator.Send(new GetBlogPostList() { Command = command });
return View("List", model);
}
public virtual async Task<IActionResult> ListRss(string languageId)
{
var feed = new RssFeed(
string.Format("{0}: Blog", _storeContext.CurrentStore.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id)),
"Blog",
new Uri(_webHelper.GetStoreLocation()),
DateTime.UtcNow);
if (!_blogSettings.Enabled)
return new RssActionResult(feed, _webHelper.GetThisPageUrl(false));
var items = new List<RssItem>();
var blogPosts = await _blogService.GetAllBlogPosts(_storeContext.CurrentStore.Id);
foreach (var blogPost in blogPosts)
{
string blogPostUrl = Url.RouteUrl("BlogPost", new { SeName = blogPost.GetSeName(_workContext.WorkingLanguage.Id) }, _webHelper.IsCurrentConnectionSecured() ? "https" : "http");
items.Add(new RssItem(blogPost.Title, blogPost.Body, new Uri(blogPostUrl), String.Format("urn:store:{0}:blog:post:{1}", _storeContext.CurrentStore.Id, blogPost.Id), blogPost.CreatedOnUtc));
}
feed.Items = items;
return new RssActionResult(feed, _webHelper.GetThisPageUrl(false));
}
public virtual async Task<IActionResult> BlogPost(string blogPostId,
[FromServices] IStoreMappingService storeMappingService,
[FromServices] IPermissionService permissionService)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var blogPost = await _blogService.GetBlogPostById(blogPostId);
if (blogPost == null ||
(blogPost.StartDateUtc.HasValue && blogPost.StartDateUtc.Value >= DateTime.UtcNow) ||
(blogPost.EndDateUtc.HasValue && blogPost.EndDateUtc.Value <= DateTime.UtcNow))
return RedirectToRoute("HomePage");
//Store mapping
if (!storeMappingService.Authorize(blogPost))
return InvokeHttp404();
var model = await _mediator.Send(new GetBlogPost() { BlogPost = blogPost });
//display "edit" (manage) link
if (await permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) && await permissionService.Authorize(StandardPermissionProvider.ManageBlog))
DisplayEditLink(Url.Action("Edit", "Blog", new { id = blogPost.Id, area = "Admin" }));
return View(model);
}
[HttpPost, ActionName("BlogPost")]
[AutoValidateAntiforgeryToken]
[FormValueRequired("add-comment")]
[ValidateCaptcha]
public virtual async Task<IActionResult> BlogCommentAdd(string blogPostId, BlogPostModel model, bool captchaValid,
[FromServices] IWorkContext workContext)
{
if (!_blogSettings.Enabled)
return RedirectToRoute("HomePage");
var blogPost = await _blogService.GetBlogPostById(blogPostId);
if (blogPost == null || !blogPost.AllowComments)
return RedirectToRoute("HomePage");
if (workContext.CurrentCustomer.IsGuest() && !_blogSettings.AllowNotRegisteredUsersToLeaveComments)
{
ModelState.AddModelError("", _localizationService.GetResource("Blog.Comments.OnlyRegisteredUsersLeaveComments"));
}
//validate CAPTCHA
if (_captchaSettings.Enabled && _captchaSettings.ShowOnBlogCommentPage && !captchaValid)
{
ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
}
if (ModelState.IsValid)
{
await _mediator.Send(new InsertBlogCommentCommandModel() { Model = model, BlogPost = blogPost });
//The text boxes should be cleared after a comment has been posted
//That' why we reload the page
TempData["Grand.blog.addcomment.result"] = _localizationService.GetResource("Blog.Comments.SuccessfullyAdded");
return RedirectToRoute("BlogPost", new { SeName = blogPost.GetSeName(_workContext.WorkingLanguage.Id) });
}
//If we got this far, something failed, redisplay form
model = await _mediator.Send(new GetBlogPost() { BlogPost = blogPost });
return View(model);
}
#endregion
}
}