2025-06-29 16:12:16 +01:00
|
|
|
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-05-04 13:56:22 +02:00
|
|
|
using Yavsc.Models;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2018-05-04 13:56:22 +02:00
|
|
|
using Yavsc.Models.Blog;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2025-06-29 16:12:16 +01:00
|
|
|
using Yavsc.Server.Exceptions;
|
2025-07-15 19:43:41 +01:00
|
|
|
using Yavsc.Server.Helpers;
|
2026-08-03 02:16:57 +01:00
|
|
|
using Yavsc.Blogspot;
|
2018-11-28 13:53:26 +00:00
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
|
|
2026-07-12 01:17:52 +01:00
|
|
|
namespace Yavsc.Org.Controllers
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
2026-06-04 22:04:20 +01:00
|
|
|
public class BlogSpotController : Controller
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
2020-09-12 01:11:30 +01:00
|
|
|
readonly ILogger _logger;
|
|
|
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
readonly RequestLocalizationOptions _localisationOptions;
|
2019-09-05 21:02:13 +01:00
|
|
|
|
2025-06-29 16:12:16 +01:00
|
|
|
readonly BlogSpotService blogSpotService;
|
2026-06-04 22:04:20 +01:00
|
|
|
public BlogSpotController(
|
2018-05-04 13:56:22 +02:00
|
|
|
ApplicationDbContext context,
|
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
|
IAuthorizationService authorizationService,
|
2025-06-29 16:12:16 +01:00
|
|
|
IOptions<RequestLocalizationOptions> localisationOptions,
|
|
|
|
|
BlogSpotService blogSpotService)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
|
|
|
|
_context = context;
|
2026-07-12 01:17:52 +01:00
|
|
|
_logger = loggerFactory.CreateLogger<BlogSpotController>();
|
2018-05-04 13:56:22 +02:00
|
|
|
_authorizationService = authorizationService;
|
2019-09-05 21:02:13 +01:00
|
|
|
_localisationOptions = localisationOptions.Value;
|
2025-06-29 16:12:16 +01:00
|
|
|
this.blogSpotService = blogSpotService;
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: Blog
|
|
|
|
|
[AllowAnonymous]
|
2025-06-29 16:12:16 +01:00
|
|
|
public async Task<IActionResult> Index(string id, int skip = 0, int take = 25)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
if (!string.IsNullOrEmpty(id))
|
2025-02-23 20:23:23 +00:00
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
return View("UserPosts",
|
|
|
|
|
await blogSpotService.UserPosts(id, User.GetUserId(),
|
|
|
|
|
skip, take));
|
2025-02-23 20:23:23 +00:00
|
|
|
}
|
2025-07-10 08:38:38 +01:00
|
|
|
IEnumerable<IBlogPost> index = await this.blogSpotService.Index(User, id, skip, take);
|
|
|
|
|
return View(index);
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-25 18:51:58 +00:00
|
|
|
[Route("~/Title/{id?}")]
|
2018-05-04 13:56:22 +02:00
|
|
|
[AllowAnonymous]
|
2026-02-09 01:03:33 +00:00
|
|
|
[HttpGet]
|
2018-05-04 13:56:22 +02:00
|
|
|
public IActionResult Title(string id)
|
|
|
|
|
{
|
2026-06-04 12:13:37 +01:00
|
|
|
ViewBag.Title = id;
|
2025-07-10 08:38:38 +01:00
|
|
|
return View("Title", blogSpotService.GetTitle(id));
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-29 16:12:16 +01:00
|
|
|
private async Task<IEnumerable<BlogPost>> UserPosts(string userName, int pageLen = 10, int pageNum = 0)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
return await blogSpotService.UserPosts(userName, User.GetUserId(), pageLen, pageNum);
|
|
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
// GET: Blog/Details/5
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> Details(long? id)
|
|
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
if (id == null) return this.NotFound();
|
2025-07-07 07:49:18 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var blog = await blogSpotService.Details(User, id.Value);
|
2026-08-10 21:48:03 +01:00
|
|
|
ViewBag.apicmtctlr = "/api/v1/blogcomments";
|
2026-06-04 12:13:37 +01:00
|
|
|
ViewBag.moderatoFlag = User.IsInMsRole(YavscConstants.BlogModeratorGroupName);
|
2018-05-04 13:56:22 +02:00
|
|
|
|
2025-07-07 07:49:18 +01:00
|
|
|
return View(blog);
|
2025-06-29 16:12:16 +01:00
|
|
|
|
2025-07-07 07:49:18 +01:00
|
|
|
}
|
2026-07-12 01:17:52 +01:00
|
|
|
catch (AuthorizationFailureException)
|
2025-07-07 07:49:18 +01:00
|
|
|
{
|
|
|
|
|
return Challenge();
|
|
|
|
|
}
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
2019-09-05 21:02:13 +01:00
|
|
|
void SetLangItems()
|
|
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
ViewBag.LangItems = _localisationOptions.SupportedUICultures?.Select
|
2019-09-05 21:02:13 +01:00
|
|
|
(
|
|
|
|
|
sc => new SelectListItem { Value = sc.IetfLanguageTag, Text = sc.NativeName, Selected = System.Globalization.CultureInfo.CurrentUICulture == sc }
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-05-04 13:56:22 +02:00
|
|
|
|
|
|
|
|
// GET: Blog/Create
|
|
|
|
|
[Authorize()]
|
|
|
|
|
public IActionResult Create(string title)
|
|
|
|
|
{
|
2026-02-22 19:54:10 +00:00
|
|
|
var result = new BlogPostEditViewModel
|
|
|
|
|
(new BlogPost
|
2025-06-29 16:12:16 +01:00
|
|
|
{
|
|
|
|
|
Title = title
|
2026-02-22 19:54:10 +00:00
|
|
|
}, true);
|
2019-09-05 21:02:13 +01:00
|
|
|
SetLangItems();
|
2024-11-10 23:12:02 +00:00
|
|
|
return View(result);
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Create
|
|
|
|
|
[HttpPost, Authorize, ValidateAntiForgeryToken]
|
2026-02-22 19:54:10 +00:00
|
|
|
public IActionResult Create(BlogPost blogInput)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
BlogPost post = blogSpotService.Create(User.GetUserId(),
|
2026-03-01 02:13:26 +00:00
|
|
|
blogInput, Request.Form.Files);
|
2018-05-04 13:56:22 +02:00
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
2025-06-29 16:12:16 +01:00
|
|
|
return View("Edit", blogInput);
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
2024-11-10 23:12:02 +00:00
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
[Authorize()]
|
|
|
|
|
// GET: Blog/Edit/5
|
|
|
|
|
public async Task<IActionResult> Edit(long? id)
|
|
|
|
|
{
|
|
|
|
|
if (id == null)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
2025-06-29 16:12:16 +01:00
|
|
|
try
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
2025-07-07 07:49:18 +01:00
|
|
|
var blog = await blogSpotService.GetPostForEdition(User, id.Value);
|
2025-06-29 16:12:16 +01:00
|
|
|
if (blog == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2026-02-22 19:54:10 +00:00
|
|
|
|
|
|
|
|
|
2019-09-05 21:02:13 +01:00
|
|
|
SetLangItems();
|
2025-07-07 07:49:18 +01:00
|
|
|
return View(blog);
|
2025-06-29 16:12:16 +01:00
|
|
|
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
2025-06-29 16:12:16 +01:00
|
|
|
catch (AuthorizationFailureException)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
|
|
|
|
return new ChallengeResult();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Edit/5
|
|
|
|
|
[HttpPost]
|
2025-06-29 16:12:16 +01:00
|
|
|
[ValidateAntiForgeryToken, Authorize()]
|
2025-02-23 20:23:23 +00:00
|
|
|
public async Task<IActionResult> Edit(BlogPostEditViewModel blogEdit)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
await blogSpotService.Modify(User, blogEdit);
|
2026-06-04 12:13:37 +01:00
|
|
|
ViewBag.StatusMessage = "Post modified";
|
2025-02-23 20:23:23 +00:00
|
|
|
return RedirectToAction("Index");
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
2025-02-23 20:23:23 +00:00
|
|
|
return View(blogEdit);
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: Blog/Delete/5
|
2025-06-29 16:12:16 +01:00
|
|
|
[ActionName("Delete"), Authorize()]
|
|
|
|
|
public async Task<IActionResult> Delete(long? id)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
|
|
|
|
if (id == null)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 07:49:18 +01:00
|
|
|
var blog = await blogSpotService.GetBlogPostAsync(id.Value);
|
2018-05-04 13:56:22 +02:00
|
|
|
if (blog == null)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return NotFound();
|
2018-05-04 13:56:22 +02:00
|
|
|
}
|
|
|
|
|
return View(blog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// POST: Blog/Delete/5
|
2025-06-29 16:20:37 +01:00
|
|
|
[HttpPost, ActionName("Delete"), Authorize("TheAuthor")]
|
2018-05-04 13:56:22 +02:00
|
|
|
[ValidateAntiForgeryToken]
|
2025-06-29 16:12:16 +01:00
|
|
|
public async Task<IActionResult> DeleteConfirmed(long id)
|
2018-05-04 13:56:22 +02:00
|
|
|
{
|
2025-06-29 16:12:16 +01:00
|
|
|
await blogSpotService.Delete(User, id);
|
2018-05-04 13:56:22 +02:00
|
|
|
return RedirectToAction("Index");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|