yavsc/Yavsc/Controllers/BlogspotController.cs

205 lines
6.4 KiB
C#

8 years ago
using System;
using System.Linq;
8 years ago
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNet.Authorization;
using Microsoft.Data.Entity;
using Microsoft.Extensions.OptionsModel;
8 years ago
using Yavsc.Models;
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace Yavsc.Controllers
{
[ServiceFilter(typeof(LanguageActionFilter))]
public class BlogspotController : Controller
{
ILogger _logger;
private ApplicationDbContext _context;
private SiteSettings _siteSettings;
private IAuthorizationService _authorizationService;
public BlogspotController(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
ILoggerFactory loggerFactory,
IAuthorizationService authorizationService,
IOptions<SiteSettings> siteSettings)
{
_context = context;
_logger = loggerFactory.CreateLogger<AccountController>();
_authorizationService = authorizationService;
_siteSettings = siteSettings.Value;
}
// GET: Blog
[AllowAnonymous]
public IActionResult Index(string id)
{
if (!string.IsNullOrEmpty(id))
return UserPosts(id);
return View(_context.Blogspot.Include(
b => b.Author
8 years ago
).Where(p => p.Visible));
}
[Route("/Title/{id?}")]
[AllowAnonymous]
public IActionResult Title(string id)
{
return View("Index", _context.Blogspot.Include(
b => b.Author
8 years ago
).Where(x => x.Title == id).ToList());
}
[Route("/Blog/{id?}")]
[AllowAnonymous]
public IActionResult UserPosts(string id)
{
if (string.IsNullOrEmpty(id))
return View("Index",_context.Blogspot.Include(
b => b.Author
8 years ago
).Where(p => p.Visible));
if (User.IsSignedIn())
return View("Index", _context.Blogspot.Include(
b => b.Author
).Where(x => x.Author.UserName == id).ToList());
return View("Index", _context.Blogspot.Include(
b => b.Author
8 years ago
).Where(x => x.Author.UserName == id && x.Visible).ToList());
}
// GET: Blog/Details/5
[AllowAnonymous]
public IActionResult Details(long? id)
{
if (id == null)
{
return HttpNotFound();
}
Blog blog = _context.Blogspot.Include(
b => b.Author
).Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
return View(blog);
}
// GET: Blog/Create
8 years ago
[Authorize()]
public IActionResult Create()
{
return View();
}
// POST: Blog/Create
8 years ago
[HttpPost, Authorize(), ValidateAntiForgeryToken]
public IActionResult Create(Blog blog)
{
8 years ago
blog.Modified = blog.Posted = DateTime.Now;
blog.Rate = 0;
blog.AuthorId = User.GetUserId();
_logger.LogWarning($"Post from: {blog.AuthorId}");
ModelState.ClearValidationState("AuthorId");
if (ModelState.IsValid)
{
8 years ago
blog.Posted = DateTime.Now;
_context.Blogspot.Add(blog);
_context.SaveChanges();
return RedirectToAction("Index");
}
_logger.LogWarning("Invalid Blog posted ...");
return View(blog);
}
8 years ago
[Authorize()]
// GET: Blog/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if (id == null)
{
return HttpNotFound();
}
Blog blog = _context.Blogspot.Include(x => x.Author).Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
if (await _authorizationService.AuthorizeAsync(User, blog, new EditRequirement()))
{
return View(blog);
}
else
{
return new ChallengeResult();
}
}
// POST: Blog/Edit/5
[HttpPost]
8 years ago
[ValidateAntiForgeryToken,Authorize()]
public IActionResult Edit(Blog blog)
{
if (ModelState.IsValid)
{
var auth = _authorizationService.AuthorizeAsync(User, blog, new EditRequirement());
if (auth.Result)
{
8 years ago
blog.Modified = DateTime.Now;
_context.Update(blog);
_context.SaveChanges();
ViewData["StatusMessage"] = "Post modified";
return RedirectToAction("Index");
} // TODO Else hit me hard
else
{
ViewData["StatusMessage"] = "Access denied ...";
}
}
return View(blog);
}
// GET: Blog/Delete/5
8 years ago
[ActionName("Delete"),Authorize()]
public IActionResult Delete(long? id)
{
if (id == null)
{
return HttpNotFound();
}
Blog blog = _context.Blogspot.Include(
b => b.Author
).Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
return View(blog);
}
// POST: Blog/Delete/5
8 years ago
[HttpPost, ActionName("Delete"), Authorize()]
[ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(long id)
{
Blog blog = _context.Blogspot.Single(m => m.Id == id);
var auth = _authorizationService.AuthorizeAsync(User, blog, new EditRequirement());
if (auth.Result)
{
_context.Blogspot.Remove(blog);
_context.SaveChanges();
}
return RedirectToAction("Index");
}
}
}