94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using SkyArtShop.Models;
|
||
|
|
using SkyArtShop.Services;
|
||
|
|
|
||
|
|
namespace SkyArtShop.Controllers;
|
||
|
|
|
||
|
|
[Route("admin/blog")]
|
||
|
|
[Authorize(Roles = "Admin,MasterAdmin")]
|
||
|
|
public class AdminBlogController : Controller
|
||
|
|
{
|
||
|
|
private readonly PostgreSQLService _pgService;
|
||
|
|
|
||
|
|
private readonly SlugService _slugService;
|
||
|
|
|
||
|
|
private readonly string _blogCollection = "BlogPosts";
|
||
|
|
|
||
|
|
public AdminBlogController(PostgreSQLService pgService, SlugService slugService)
|
||
|
|
{
|
||
|
|
_pgService = pgService;
|
||
|
|
_slugService = slugService;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("")]
|
||
|
|
public async Task<IActionResult> Index()
|
||
|
|
{
|
||
|
|
return View((await _pgService.GetAllAsync<BlogPost>(_blogCollection)).OrderByDescending((BlogPost p) => p.CreatedAt).ToList());
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("create")]
|
||
|
|
public IActionResult Create()
|
||
|
|
{
|
||
|
|
return View(new BlogPost());
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("create")]
|
||
|
|
public async Task<IActionResult> Create(BlogPost post)
|
||
|
|
{
|
||
|
|
if (!base.ModelState.IsValid)
|
||
|
|
{
|
||
|
|
return View(post);
|
||
|
|
}
|
||
|
|
post.CreatedAt = DateTime.UtcNow;
|
||
|
|
post.UpdatedAt = DateTime.UtcNow;
|
||
|
|
post.PublishedDate = DateTime.UtcNow;
|
||
|
|
post.Slug = _slugService.GenerateSlug(post.Title);
|
||
|
|
await _pgService.InsertAsync(_blogCollection, post);
|
||
|
|
base.TempData["SuccessMessage"] = "Blog post created successfully!";
|
||
|
|
return RedirectToAction("Index");
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("edit/{id}")]
|
||
|
|
public async Task<IActionResult> Edit(string id)
|
||
|
|
{
|
||
|
|
BlogPost blogPost = await _pgService.GetByIdAsync<BlogPost>(_blogCollection, id);
|
||
|
|
if (blogPost == null)
|
||
|
|
{
|
||
|
|
return NotFound();
|
||
|
|
}
|
||
|
|
return View(blogPost);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("edit/{id}")]
|
||
|
|
public async Task<IActionResult> Edit(string id, BlogPost post)
|
||
|
|
{
|
||
|
|
if (!base.ModelState.IsValid)
|
||
|
|
{
|
||
|
|
return View(post);
|
||
|
|
}
|
||
|
|
post.Id = id;
|
||
|
|
post.UpdatedAt = DateTime.UtcNow;
|
||
|
|
post.Slug = _slugService.GenerateSlug(post.Title);
|
||
|
|
await _pgService.UpdateAsync(_blogCollection, id, post);
|
||
|
|
base.TempData["SuccessMessage"] = "Blog post updated successfully!";
|
||
|
|
return RedirectToAction("Index");
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("delete/{id}")]
|
||
|
|
public async Task<IActionResult> Delete(string id)
|
||
|
|
{
|
||
|
|
await _pgService.DeleteAsync<BlogPost>(_blogCollection, id);
|
||
|
|
base.TempData["SuccessMessage"] = "Blog post deleted successfully!";
|
||
|
|
return RedirectToAction("Index");
|
||
|
|
}
|
||
|
|
|
||
|
|
private string GenerateSlug(string text)
|
||
|
|
{
|
||
|
|
return _slugService.GenerateSlug(text);
|
||
|
|
}
|
||
|
|
}
|