This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
RootController.cs
237 lines (198 loc) · 6.73 KB
/
RootController.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using WilderBlog.Data;
using WilderBlog.Models;
using WilderBlog.Services;
using WilderMinds.RssSyndication;
namespace WilderBlog.Controllers
{
[Route("")]
public class RootController : Controller
{
readonly int _pageSize = 25;
private IMailService _mailService;
private IWilderRepository _repo;
private IMemoryCache _memoryCache;
private ILogger<RootController> _logger;
public RootController(IMailService mailService,
IWilderRepository repo,
IMemoryCache memoryCache,
ILogger<RootController> logger)
{
_mailService = mailService;
_repo = repo;
_memoryCache = memoryCache;
_logger = logger;
}
[HttpGet("")]
public IActionResult Index()
{
return Pager(1);
}
[HttpGet("blog/{page:int?}")]
public IActionResult Pager(int page)
{
return View("Index", _repo.GetStories(_pageSize, page));
}
[HttpGet("{year:int}/{month:int}/{day:int}/{slug}")]
public IActionResult Story(int year, int month, int day, string slug)
{
var fullSlug = $"{year}/{month}/{day}/{slug}";
try
{
var story = _repo.GetStory(fullSlug);
// Try with other slug if it doesn't work
if (story == null) story = _repo.GetStory($"{year:0000}/{month:00}/{day:00}/{slug}");
if (story != null) return View(story);
}
catch
{
_logger.LogWarning($"Couldn't find the ${fullSlug} story");
}
return Redirect("/");
}
[HttpGet("about")]
public IActionResult About()
{
return View();
}
[HttpGet("contact")]
public IActionResult Contact()
{
return View();
}
[HttpPost("contact")]
//[ValidateAntiForgeryToken]
public async Task<IActionResult> Contact([FromBody]ContactModel model)
{
try
{
if (ModelState.IsValid)
{
await _mailService.SendMail("ContactTemplate.txt", model.Name, model.Email, model.Subject, model.Msg);
return Ok(new { Success = true, Message = "Message Sent" });
}
else
{
ModelState.AddModelError("", "Failed to send email");
return BadRequest(ModelState);
}
}
catch (Exception ex)
{
_logger.LogError("Failed to send email from contact page", ex);
return BadRequest(new { Reason = "Error Occurred" });
}
}
[HttpGet("rss")]
public IActionResult Rss()
{
return RedirectPermanent("http://feeds.feedburner.com/ShawnWildermuth");
}
[HttpGet("Error/{code:int}")]
public IActionResult Error(int errorCode)
{
if (Response.StatusCode == (int)HttpStatusCode.NotFound ||
errorCode == (int)HttpStatusCode.NotFound ||
Request.Path.Value.EndsWith("404"))
{
return View("NotFound");
}
return View();
}
[HttpGet("Exception")]
public IActionResult Exception()
{
var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
var request = HttpContext.Features.Get<IHttpRequestFeature>();
if (exception != null && request != null)
{
var message = $@"RequestUrl: ${request.Path}
Exception: ${exception.Error}";
_mailService.SendMail("logmessage.txt", "Shawn Wildermuth", "shawn@wildermuth.com", "[WilderBlog Exception]", message);
}
return View();
}
[HttpGet("testerror")]
public IActionResult TestError()
{
throw new InvalidOperationException("Failure");
}
[HttpGet("feed")]
public IActionResult Feed()
{
var feed = new Feed()
{
Title = "Shawn Wildermuth's Blog",
Description = "My Favorite Rants and Raves",
Link = new Uri("http://wildermuth.com/feed"),
Copyright = "© 2016 Wilder Minds LLC"
};
var license = @"<div>
<div style=""float: left;"">
<a rel=""license"" href=""http://creativecommons.org/licenses/by-nc-nd/3.0/"">
<img alt=""Creative Commons License"" style=""border-width: 0"" src=""http://i.creativecommons.org/l/by-nc-nd/3.0/88x31.png"" /></a></div>
<div>
This work by <a xmlns:cc=""http://creativecommons.org/ns#"" href=""http://wildermuth.com""
property=""cc:attributionName"" rel=""cc:attributionURL"">Shawn Wildermuth</a> is
licensed under a <a rel=""license"" href=""http://creativecommons.org/licenses/by-nc-nd/3.0/"">
Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License</a>.<br />
Based on a work at <a xmlns:dct=""http://purl.org/dc/terms/"" href=""http://wildermuth.com""
rel=""dct:source"">wildermuth.com</a>.</div>
</div>";
var ad = @"<hr/><div>If you liked this article, see Shawn's courses on <a href=""http://shawnw.me/psauthor"">Pluralsight</a>.</div>";
var entries = _repo.GetStories(25);
foreach (var entry in entries.Stories)
{
var item = new Item()
{
Title = entry.Title,
Body = string.Concat(entry.Body, license, ad),
Link = new Uri(new Uri(Request.GetEncodedUrl()), entry.Slug),
Permalink = entry.Slug,
PublishDate = entry.DatePublished,
Author = new Author() { Name = "Shawn Wildermuth", Email = "shawn@wildermuth.com" }
};
foreach (var cat in entry.Categories.Split(','))
{
item.Categories.Add(cat);
}
feed.Items.Add(item);
}
return File(Encoding.UTF8.GetBytes(feed.Serialize()), "text/xml");
}
[HttpGet("calendar")]
public IActionResult Calendar()
{
return View();
}
[HttpGet("psstats")]
public async Task<IActionResult> PsStats()
{
var CACHEKEY = "PSSTATS";
string cached;
if (!_memoryCache.TryGetValue(CACHEKEY, out cached))
{
var client = new HttpClient();
cached = await client.GetStringAsync(new Uri("http://www.pluralsight.com/data/Courses/Popular"));
_memoryCache.Set(CACHEKEY, cached,new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2) });
}
var converter = new ExpandoObjectConverter();
dynamic courses = JsonConvert.DeserializeObject<IEnumerable<ExpandoObject>>(cached, converter);
return View(courses);
}
}
}